Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

draw rectangle in Jpanel

Tags:

java

swing

I am trying to get my hands on GUI programming in java and wanted to draw a rectangle in Jpanel. Code does not give any error but I cannot get rectangle in the GUI. Can somebody please tell me what I am missing in the following code. I am sure it is pretty simple so please be gentle.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HelloWorldGUI2 {

    private static class ButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        System.exit(0);
        }
        }
    private static class RectDraw extends JPanel {
        public void paintComponent(Graphics g) {
        super.paintComponent(g);  
         g.drawRect(230,80,10,10);  
         g.setColor(Color.RED);  
         g.fillRect(230,80,10,10);  
        }
        }
    public static void main(String[] args) {
        JPanel content = new JPanel();
        RectDraw newrect= new RectDraw();
        JButton okButton= new JButton("OK");
        JButton clearButton= new JButton("Clear");
        ButtonHandler listener= new ButtonHandler();
        okButton.addActionListener(listener);
        clearButton.addActionListener(listener);
        content.add(okButton);
        content.add(clearButton);
        content.add(newrect);
        JFrame window = new JFrame("GUI Test");
        window.setContentPane(content);
        window.setSize(250,100);
        window.setLocation(100,100);
        window.setVisible(true);
        }

    }
like image 451
Jack_of_All_Trades Avatar asked Sep 18 '13 16:09

Jack_of_All_Trades


People also ask

How do you draw a rectangle on a panel in Java?

In Java, to draw a rectangle (outlines) onto the current graphics context, we can use the following methods provided by the Graphics/Graphics2D class: drawRect(int x, int y, int width, int height) draw3DRect(int x, int y, int width, int height, boolean raised) draw(Rectangle2D)

Can you draw on a JPanel?

Instead, in Swing, we usually draw on a JPanel. Turns out, you can draw on most Swing components, but are not advised to draw on top-level components like JFrame. The all-important technique of overriding paintComponent() (and, sometimes, repaint()): Like AWT, Swing does not maintain "bit-mapped memory".

How do you draw and fill a rectangle in Java?

To draw a solid (filled) rectangle, fillRect () method is used. This method also takes four parameters similar to the drawRect () method. A rounded outlined rectangle can be drawn by using the drawRoundRect () method. This method takes the six parameters.

How do I draw a circle in a JPanel?

Solution: Write a method, drawCircle() , which has parameters for the coordinates of the center point and a radius. It then calls drawOval with transformed parameters. To make a drawing, define a new component by subclassing JPanel and overriding the paintComponent() method.


1 Answers

Your newrect RectDraw's size is likely going to be quite small, probably [0, 0], since it has been added to a FlowLayout using JPanel and doesn't have a preferredSize set. Consider overriding its getPreferredSize() method and returning a suitable Dimension so that its drawing can be seen.

private static class RectDraw extends JPanel {
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);  
    g.drawRect(230,80,10,10);  
    g.setColor(Color.RED);  
    g.fillRect(230,80,10,10);  
  }

  public Dimension getPreferredSize() {
    return new Dimension(PREF_W, PREF_H); // appropriate constants
  }
}

Also,

  • The paintComponent method should be protected, not public
  • Don't forget to use @Override before your method overrides.
  • Aim for much less code in the main method and more code in the "instance" world, not the static world.
  • Take care with your code formatting. Poor code formatting, especially misleading indentations, leads to silly errors.
like image 147
Hovercraft Full Of Eels Avatar answered Sep 28 '22 16:09

Hovercraft Full Of Eels