Is it possible to call repaint() inside the main()?I have tried the code below.But its not working

 public mypanel() 
             protected void paintComponent(final Graphics g) 
                final Graphics2D g2 = (Graphics2D)jLabel2.getGraphics();       
            //above code doesnt provide any output
            private void formMouseClicked(java.awt.event.MouseEvent evt)

this code  draws the rectangle.But I want to draw it when the program execution starts.Please help to solve this(rect

angle drawn on an image placed in jLabel).

4 Years
Discussion Span
Last Post by JamesCherrill

The problem is probably in the code you didn't post!

Eg: If you don't set a size and/or minimum size for your mypanel then the layout manager for its parent will look at mypanel's children (none) and decide that 0x0 pixels is a big enough size for it.

final Graphics2D g2 = (Graphics2D)jLabel2.getGraphics();
No. You MUST draw to the Graphics you wre passed
final Graphics2D g2 = (Graphics2D) g;
Swing is double-buffered, and if you just draw to some random Graphics it will almost certainly be ignored and/or overwritten by the buffering.

ps Java naming conventions: MyPanel, not mypanel for a class name
add @Override annotations to your paintComponent/actionPerformed etc so the compiler will pick up any mis-spelling in your method signatures -this prevents a very commion class of problem where nothing happens because the methods didn't override properly.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.