Drawing rectangles on a JPanel?

Still no idea for example 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); } }.

Still no idea, for example 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); } }.

1, the key is that you need to provide a preferred size for the layout managers. – camickr Oct 11 at 14:54.

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.

I already did this, it is in the DrawRectPanel class – infoSyStem Oct 11 at 9:06 I edited the answer. Now this should work. – gt_ebuddy Oct 11 at 9:10 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? – infoSyStem Oct 11 at 9:34 you can manually call paintcomponent by panel1.repaint();panel1.validate(); – gt_ebuddy Oct 11 at 9:49 1 please post your full code. Then after I can debug.

– gt_ebuddy Oct 11 at 11:10.

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.

How can I draw the rectangles on panel1? Thanks in advance.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions