I just started using JFrames and such instead of Applets, and I'm having some trouble using a backbuffer like I use to. I made a smple program to demonstrate what I mean.
It kept telling me that I cannot make a non-static reference to something static. So I went about changing things to static, hoping that would solve the problem... but, I got to the createImage method and it says "Cannot make a static reference to the non-static method createImage(int, int) from the type Component" So yeah, that's rather unfortunate. Any tips? :/
public class SomeGraphics extends JFrame
public static int WIDTH = 300, HEIGHT = 300;
public static Image backbuffer = createImage(WIDTH, HEIGHT);
public static Graphics backg = backbuffer.getGraphics();
public static int speed = 20;
public static int x1 = 0, y1 = 0, x2 = 300, y2 = 300;
public static ActionListener timerAction = new ActionListener()
public void actionPerformed(ActionEvent evt)
public void paint(Graphics g)
g.drawImage(backbuffer, 0, 0, this);
public static void update()
backg.fillRect(0, 0, WIDTH, HEIGHT);
backg.drawLine(x1, y1, x2, y2);
public static void main(String args)
SomeGraphics SG = new SomeGraphics();
new Timer(speed, timerAction).start();
1) don't draw directly to the JFrame. Instead Draw to a JPanel (or JComponent) that either is the contentPane or has been added to the contentPane.
2) override paintComponent in this JPanel and draw in there.
3) Often the best first line of the paintComponent method is
4) why is almost everything static in your code? The only things that probably ought to be static are the main methods and constants (static final variables).
I explained why I made everything static... was rather out of desperation I suppose.
I'll try that out, thanks.
Sorry, but it's not a good reason. Better would be to leave most things non-static except for main and your constants and show us the error message you're receiving than to do this kluge of making everything static.
Originally Posted by AndrewM16921