Making a screen to draw on
Okay im having a lot of problems just making a JFrame to draw on so im just going to step back and look for the simplest way to do this. I have my application and id like to create a JFrame in which I can draw images and whatever. However, I do not want to extend my application to JPanel because it is already extended to my Core class. How do i go about doing this?
Re: Making a screen to draw on
You need to have a class that extends a JComponent or subclass like JPanel that can be added to the JFrame.
Your component class could contain the Core class.
Re: Making a screen to draw on
so i should make my Core class extend to JPanel/JComponent?
Re: Making a screen to draw on
That would be a solution if the Core class is where the override is for the paintComponent method where you will do the drawing.
Re: Making a screen to draw on
Quote:
Originally Posted by
Norm
That would be a solution if the Core class is where the override is for the paintComponent method where you will do the drawing.
Great that works now. Also how would I make it double buffer?
I tried this and it still flickering
Code:
public void update(long timeElapsed){
backbuffer = new BufferedImage(getScreenWidth(), getScreenHeight(), BufferedImage.TYPE_INT_RGB);
g2d = backbuffer.createGraphics();
g2d.setBackground(Color.BLACK);
draw(g2d);
g2d.drawImage(Main.getImage(0), 0, 0, frame); //frame is the JFrame as a variable
}
Re: Making a screen to draw on
I think Swing components that extend JComponent are already double buffered.
If you're having problems with flickering, make a small program that compiles and executes to demonstrate your problem. If you searched on the forum you'd probably find several past posts on this topic.
Re: Making a screen to draw on
There is no need to override an update() method when using Swing. That looks like old AWT code.
Read Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) for Swing examples.
Re: Making a screen to draw on
The update method shown is not an override. Wrong args.
Re: Making a screen to draw on
I guess my point was he is trying to make it look like AWT by creating an update(...) method. Presumably somewhere that method gets manually invoked. You should not to that.
Custom painting is done in the paintComponent() method, which is explained in the tutorial.
Re: Making a screen to draw on
For double buffering, you can create the image in some method and then later draw it in the paintComponent method.
I have no idea what the OP is doing with only the small bits of code shown.