Results 1 to 4 of 4

Thread: Backbuffer

  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Backbuffer

    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? :/

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    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)
    		{
    			update();
    		}
     	};
    	public SomeGraphics()
    	{
    	    setVisible(true);
    	    setSize(WIDTH, HEIGHT);
    	}
    	public void paint(Graphics g)
    	{
    		g.drawImage(backbuffer, 0, 0, this);
    	}
    	public static void update()
    	{
    		x1++;
    		x2--;
    		
    		backg.setColor(Color.white);
    		backg.fillRect(0, 0, WIDTH, HEIGHT);
    		backg.setColor(Color.red);
    		backg.drawLine(x1, y1, x2, y2);
    	}
    	public static void main(String args[])
    	{
    		SomeGraphics SG = new SomeGraphics(); 
    		
    		new Timer(speed, timerAction).start();
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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
    Java Code:
    super.paintComponent(g);
    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).

  3. #3
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default

    I explained why I made everything static... was rather out of desperation I suppose.

    I'll try that out, thanks.
    Last edited by AndrewM16921; 04-12-2009 at 10:54 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by AndrewM16921 View Post
    I explained why I made everything static... was rather out of desperation I suppose.
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •