Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Panel, to Applet, to application?

Started by darkWang May 14, 2003 at 6:12 PM 8 replies 650+ views
Original Post
darkWang
darkWang
Hear it goes... this is starting off as a double buffering class... i found some code, and changed it to an abstract class since i want to always remeber what function i must override... although it would be cooler if the compiler just said hey you need this function, instead of class is abstract doesn't define this function... Any how here is the code
    
import java.awt.*;
import java.applet.*;

public abstract class DoubleBuffer extends Panel{

	//	class variables

	private int bufferWidth;
	private int bufferHeight;
	private Image bufferImage;
	private Graphics bufferGraphics;
	
	public DoubleBuffer(){
		super();
	}
	
	public void update(Graphics g){
		paint(g);
	}

	public void paint(Graphics g){
		if(bufferWidth!=getSize().width || bufferHeight!=getSize().height || bufferImage==null || bufferGraphics==null)
			resetBuffer();
			
		if(bufferGraphics!=null){
			//	this clears the offscreen image, not the onscreen one

			bufferGraphics.clearRect(0,0,bufferWidth,bufferHeight);

			//	calls the paintbuffer method with the offscreen graphics as a param

			paintBuffer(bufferGraphics);

			//	we finaly paint the offscreen image onto the onscreen image

			g.drawImage(bufferImage,0,0,this);
		}
	}

	public abstract void paintBuffer(Graphics g); //{

		//	in classes extended from this one, add something to paint here!

		//	always remember, g is the offscreen graphics

	//}


	private void resetBuffer(){
		// always keep track of the image size

		bufferWidth=getSize().width;
		bufferHeight=getSize().height;

		//	clean up the previous image

		if(bufferGraphics!=null){
			bufferGraphics.dispose();
			bufferGraphics=null;
		}
		if(bufferImage!=null){
			bufferImage.flush();
			bufferImage=null;
		}
		System.gc();

		//	create the new image with the size of the panel

		bufferImage=createImage(bufferWidth,bufferHeight);
		bufferGraphics=bufferImage.getGraphics();
	}
}
    
Now i want to make an applet that extends my Double Buffer Class... when i try it lets me but says applet not initialized can someone show me an example of makeing an applet that extends this panel? Eventually i want to be able to use this for a application or applet. The quick fix i did was DoubleBuffer extend Applet, but that defeats what i am trying to do in the first place. [edited by - darkWang on May 14, 2003 7:14:27 PM]
Psionic-X
Psionic-X
Hey i think i found the answer to yer question searching for an answer to mine. while i was researching.

An Applet is a Panel.
Because Applet is a subclass of the AWT Panel class, applets can contain other Components, just as any Panel can. Applets inherit Panel''s default layout manager: FlowLayout. As Panels (and thus Components), Applets participate in the AWT drawing and event hierarchy.

------------------------------
Exsiting in every essence of time I will find you! You will answer my Question!

-Psionic-X- Existing from a greater plane
------------------------------Exsiting in every essence of time I will find you! You will answer my Question!-Psionic-X- Existing from a greater plane
darkWang
darkWang
Hello?
CaptainJester
CaptainJester
Make DoubleBuffer an interface instead of a class. You can only extend one class. If you want to extend Applet then you cannot extend DoubleBuffer also. But you can implement DoubleBuffer if it is an interface.



First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
darkWang
darkWang
any links? to a how to on an interface.

ok well according to my java book an interface doesn't contain code, it just makes the compiler check to see if these functions are presnt.

I guees my question is... How can i write an applett that looks like this for its header and get it to work.

public class myNuts extends Panel {
blah bla blah
}

and then when i run my applet have it work?

or how can i cast a panel to an applet?

i think i figured it out...

make DoubleBuffer extend a panel, then make the new class extend DoubleBuffer.. then instead of useing an init() method, make a constructor.

ie, the init method becomes a constructor. it works, dont know if its correct.

[edited by - darkWang on May 15, 2003 11:34:50 PM]

[edited by - darkWang on May 15, 2003 11:44:27 PM]
CaptainJester
CaptainJester
Or have DoubleBuffer extend Applet. Since Applet extends Panel, you can use Applet anywhere that a Panel is required.

Then you can have your program extend DoubleBuffer.



First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
Psionic-X
Psionic-X
which is what i was trying to get at Captain heh.. guess my post wasnt straight forward enough or req someone to read it.. oh well.

------------------------------
Exsiting in every essence of time I will find you! You will answer my Question!

-Psionic-X- Existing from a greater plane
------------------------------Exsiting in every essence of time I will find you! You will answer my Question!-Psionic-X- Existing from a greater plane
darkWang
darkWang
i did extend applet, but applet has to extend from something...

when you make an application you extend from a frame? does an applet and a frame extend form the same family? i want to use my doublebuffer class for an appler or an application if this simply involves writing two classes, one extending applet, and one extending frame, then so be it. It would just be cleaner to me to have one class called double buffer that could handle both.

I also relized that java kinda sucks with its single inheritence way of doing things, but you cant have everything.
CaptainJester
CaptainJester
Well, I don''t think Java sucks at not having double inheritance. Its a feature I like.

Anyway, if you want to use it both ways, do this:

  
public class DoubleBuffer extends Applet{
...
}

public class MyGame extends DoubleBuffer {
public void init() {
...
}
...
}

public class MyApplication extends Frame {
MyGame mg=new MyGame();

public MyApplication() {
super("MyApp");
mg.init();
setLayout(new BorderLayout());
add(mg,BorderLayout.CENTER);
}
}


Since you are adding the MyGame calss to the center of a BorderLayout, with no other components, it will fill the entire screen.



First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
darkWang
darkWang
sweet, thats what i was looking for.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.