The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Help me understand javax.swing/java.awt

DelzhandDelzhand Registered User, Transition Team regular
edited November 2006 in Help / Advice Forum
I've done a fair bit of programming in other languages, but I've never done graphical work in straight up Java. Here's what I need help understanging:
public class sGraphics extends JFrame{
	private int w, h;
	public sGraphics(int x, int y){
		w = x;
		h = y;
		setVisible(true);
		setSize(w,h);
		setResizable(false);
		setUndecorated(true);
	}
	
	public void paint(Graphics g){
		g.translate(0,22);
		g.setColor(Color.black);
		g.fillRect(10,10,(w-20),(h-42));
	}
	

	public static void main(String[] args){
		sGraphics sg = new sGraphics(640, 480+22);
		sg.repaint();
	}
}

Okay - this displays a window with a black box (except for the border). But paint() is never called, instead, I see repaint(). I see that paint() should be called with a Graphics object, but I never specify any Graphics, and this class extends JFrame, not Graphics.

I'm trying to figure out double buffering. This seems like the beginnings, drawing a black backbuffer then flipping it to the screen, but how would I draw other stuff to the buffer first?

Delzhand on

Posts

  • Bob SappBob Sapp Registered User regular
    edited November 2006
    Are you familiar with callbacks and events?

    Basically, the Java VM will call the paint method and will supply the Graphics object.

    I haven't done graphics programming in Java, but have done tons of swing. So I can't help too much on the specifics of what you're trying to do outside of theoretical. (I've done it in Windows programming).

    Edit: java.sun.com is your friend. Try this tutorial: http://java.sun.com/docs/books/tutorial/uiswing/14painting/index.html

    Edit 2: You need to call update() on the JFrame to force a call to paint(). This is your bible: http://java.sun.com/j2se/1.5.0/docs/api/

    Bob Sapp on
    fizzatar.jpg
Sign In or Register to comment.