I have the following simple code in a JFrame constructor
super(name);
setBounds(0,0,1100,750);
setLayout(null);
setVisible(true);
g = this.getGraphics();
int[] x =new int[]{65, 122, 77, 20, };
int[] y =new int[]{226, 258, 341, 310};
g.setColor(Color.RED);
g.drawPolygon (x, y, x.length);
System.out.println(g);
I get the output on console as:
sun.java2d.SunGraphics2D[font=java.awt.Font[family=Dialog,name=Dialog,style=plain,size=12],color=java.awt.Color[r=255,g=0,b=0]]
But no red polygon drawn on JFrame but just the blank JFrame.
Why ??
2 Answers 2
Dont override
paint(..)inJFrameRather add custom
JPanelwith overriddenpaintComponent(Graphics g)toJFrameDont use
Null/AbsoluteLayoutuse an appropriateLayoutManagerDont call
setBounds(..)onJFrameinstance (not that its not allowed but cant see it being relevant in this application)Dont forget to use EDT for creating and changing GUI components:
javax.swing.SwingUtilities.invokeLater(new Runnable() { @Override public void run() { Test test = new Test(); } });
you would then do something like this:
public class Test {
/**
* Default constructor for Test.class
*/
public Test() {
initComponents();
}
public static void main(String[] args) {
/**
* Create GUI and components on Event-Dispatch-Thread
*/
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Test test = new Test();
}
});
}
/**
* Initialize GUI and components (including ActionListeners etc)
*/
private void initComponents() {
JFrame jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.add(new MyPanel());
//pack frame (size JFrame to match preferred sizes of added components and set visible
jFrame.pack();
jFrame.setVisible(true);
}
}
class MyPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int[] x = new int[]{65, 122, 77, 20};
int[] y = new int[]{226, 258, 341, 310};
g.setColor(Color.RED);
g.drawPolygon(x, y, x.length);
}
//so our panel is the corerct size when pack() is called on Jframe
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
}
which produces:
enter image description here
6 Comments
setBounds(..) I have no concrete evidence, but I see it as an amalgamation of setSize() and setLocation(). I dont have a problem with setLocation() (besides the fact that the position could be off screen), but setSize() has known issues such as components being hidden due to incorrect size given etc and the same can happen when setting the height and width using setBounds(x,y,width,height). Though I am open for criticism :)int[] x = new int[]{65, 122, 77, 20}; int[] y = new int[]{226, 258, 341, 310}; g.setColor(Color.RED); g.drawPolygon(x, y, x.length); The polygon should coincide with building GER but is a little bit offset. My proff says the co-ordinates are okay. Is there anything wrong with how i put the image position.You should better override paint(Graphics g) or paintComponent(Graphics g) than the approach you are trying. Add the lines below and remove the lines after setVisible in your code.
public void paint(Graphics g) {
int[] x =new int[]{65, 122, 77, 20};
int[] y =new int[]{226, 258, 341, 310};
g.setColor(Color.RED);
g.drawPolygon (x, y, x.length);
}
paintComponent(Graphics), and do the painting on the graphics object passed there.