Drawing rectangles on a JPanel

14,649

Solution 1

still no idea,

for example

enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class CustomComponent extends JFrame {

    private static final long serialVersionUID = 1L;

    public CustomComponent() {
        setTitle("Custom Component Graphics2D");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void display() {
        add(new CustomComponents());
        pack();
        // enforces the minimum size of both frame and component        
        setMinimumSize(getSize());
        setVisible(true);
    }

    public static void main(String[] args) {
        CustomComponent main = new CustomComponent();
        main.display();
    }
}

class CustomComponents extends JComponent {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(100, 100);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 300);
    }

    @Override
    public void paintComponent(Graphics g) {
        int margin = 10;
        Dimension dim = getSize();
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
    }
}

Solution 2

instead of adding

contentPane.add(new DrawRectPanel());

you should do

contentPane.add(panel1);

Because you already have new DrawRectPanel in panel1. But in your code you are adding another instance of DrawRectPanel in contentPane. And never added panel1 in none of your container.

Share:
14,649
infoSyStem
Author by

infoSyStem

Updated on June 04, 2022

Comments

  • infoSyStem
    infoSyStem almost 2 years

    I have a JScrollPane and on top of it I have a JPanel named 'panel1'. I want some rectangles to be drawn on this JPanel.

    I have a class named DrawRectPanel which extends JPanel and does all the drawing stuff. The problem is that, I tried to draw the rectangles on panel1 by writing the following code :

    panel1.add(new DrawRectPanel());
    

    but nothing appeared on panel1 then I tried, just as a test to the class DrawRectPanel :

    JFrame frame = new JFrame();
    frame.setSize(1000, 500);
    Container contentPane =    frame.getContentPane();
    contentPane.add(new DrawRectPanel());
    frame.show();
    

    This worked, and produced the drawings but on a separate JFrame How can I draw the rectangles on panel1 ? Thanks in advance.

    EDIT : code for DrawRectPanel

    public class DrawRectPanel extends JPanel  {
    
        DrawRectPanel() {
            Dimension g = new Dimension(400,400);
            this.setPreferredSize(g);
            System.out.println("label 1");
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            System.out.println("label 2");
            g.setColor(Color.red);
            g.fillRect(20, 10, 80, 30);
        }
     }
    

    only label 1 is printed on the screen

  • infoSyStem
    infoSyStem over 12 years
    first, i should add panel1 (which overrides paintcomponent function) on a scrollpane, so i should write scrollpane.add(panel1) , if i do this, only the constructor of panel1 is called and not the paintcomponent function itself, do you know why ?
  • gtiwari333
    gtiwari333 over 12 years
    you can manually call paintcomponent by panel1.repaint();panel1.validate();
  • camickr
    camickr over 12 years
    +1, the key is that you need to provide a preferred size for the layout managers.