Java, fairly basic programming question

Koggit

CAGiversary!
Feedback
3 (100%)
My professor wrote this DrawingPanel.java thing we use to draw images. I created an animation, having it draw several layers, sleep at the end, then draw the next frame, sleep, next frame, etc.

The problem I'm having is that it's drawing each step before sleeping, so I experience some pop-in -- for example, the first step is to draw the green background, the last step is to draw a vehicle, but for a brief millisecond the background will be drawn on top of the truck, so the truck blinks in and out. I want it to draw an entire frame and then display it.

I asked my TA how I could do that, he gave me a bit of guidance -- he said "Essentially, you need to draw everything onto a BufferedImage, then draw the BufferedImage onto the DrawingPanel. That means you’ll need a few Graphics objects. You need one that talks to the DrawingPanel like usual, and you need one that talks to the BufferedImage. You’ll also need to convert these into Graphics2D objects, because only they know how to deal with BufferedImage. You do this with a cast, as in Graphics2D g2 = (Graphics2D)g"

I understand the cast part, but not really the BufferedImage stuff. This is only the third week of my first quarter of programming, so it'd be nice if anyone could translate this to plain English. I can PM my program if that'd help.
 
Last edited by a moderator:
I think I can help you, but I am a little confused on your problem. I dealt a lot with the BufferedImage class in my Computer Science II class so I should be able to help. As an aside, me and my group made this program that would take in a picture from the user and then separate it into like colors. It is pretty cool.

Let me get this straight. You are trying to draw a background and then draw a truck on top of it? Then your problem is that you can see the program doing the "magic" when it should be behind the scenes? I just need some clarity on the issue.
 
[quote name='Razzuel']I think I can help you, but I am a little confused on your problem. I dealt a lot with the BufferedImage class in my Computer Science II class so I should be able to help. As an aside, me and my group made this program that would take in a picture from the user and then separate it into like colors. It is pretty cool.

Let me get this straight. You are trying to draw a background and then draw a truck on top of it? Then your problem is that you can see the program doing the "magic" when it should be behind the scenes? I just need some clarity on the issue.[/QUOTE]

So, for my program I have..

Code:
DrawingPanel p = new DrawingPanel(1000, 600);
Graphics g = p.getGraphics();
p.setBackground(new Color(102, 153, 0));

Followed by a bunch of g. lines. Like g.drawLine, g.fillRect, etc.

My program is something along the lines of...

Code:
for loop {
   erase previous frame by re-drawing solid background
   draw lake
   draw background trees
   draw midground trees
   draw foreground trees
   draw road
   draw truck
   sleep
}

and it produces this:

t1.gif


(kept short to keep file size down)

This is fine, but when I run it directly you can sometimes see it draw each method, if only for a couple milliseconds. For example, the trees and lake will all turn green when it erases the previous frame, or the truck might disappear when it draws the road.

I can leave it as it is for my assignment, I'm not required to buffer it -- since it won't affect my grade, it's not too important, but I'd like to fix it if I could.

In the first code box I posted, my TA said to add to it, for a start...

Code:
      Graphics2D g2 = (Graphics2D)g;
      BufferedImage frame = new BufferedImage(1000, 600, BufferedImage.TYPE_INT_RGB);
      Graphics2D bufferedGraphics = frame.createGraphics();

If I add that and try to compile, I get "cannot find symbol" errors everywhere there's a "BufferedImage".

[quote name='ihadFG']This might be what you need:

http://www.javacooperation.gmxhome.de/BildschirmflackernEng.html

Without double buffering, your screen would keep flickering.

Use the code at the end.[/QUOTE]

That seems even more complicated =\
 
[quote name='ihadFG']To use bufferedimage it looks like you would have to import java.awt.Image.[/quote]

Sorry I haven't been able to give you a great response yet, but I have been busy with some of my classes!

Yeah he is right though you need to import BufferedImage.

Actually, it should look like this: import java.awt.image.BufferedImage;

I good thing to note when using classes you aren't familiar with is to google them, taking note of the class's purpose, constructors, and methods.
 
jGrasp.

I already have import java.awt.*; at the top of my program, which should import java.awt.Image as well. I added import java.awt.Image; just in case, but still get the same "cannot find symbol" error at each "BufferedImage".

Here's the full error:

Code:
 ----jGRASP exec: javac -g ...\Doodle2.java

Doodle2.java:13: cannot find symbol
symbol  : class BufferedImage
location: class Doodle2
      BufferedImage frame = new BufferedImage(1000, 600, BufferedImage.TYPE_INT_RGB);
      ^
Doodle2.java:13: cannot find symbol
symbol  : class BufferedImage
location: class Doodle2
      BufferedImage frame = new BufferedImage(1000, 600, BufferedImage.TYPE_INT_RGB);
                                ^
Doodle2.java:13: cannot find symbol
symbol  : variable BufferedImage
location: class Doodle2
      BufferedImage frame = new BufferedImage(1000, 600, BufferedImage.TYPE_INT_RGB);
                                                         ^
3 errors

I think this is because my code is incomplete.

Right now I have...

Code:
DrawingPanel p = new DrawingPanel(1000, 600);
Graphics g = p.getGraphics();
Graphics2D g2 = (Graphics2D)g;
BufferedImage frame = new BufferedImage(1000, 600, BufferedImage.TYPE_INT_RGB);
Graphics2D bufferedGraphics = frame.createGraphics();

Followed by a lot of g.drawRect, g.drawOval, etc. That's my whole main method, which I'm pretty sure is incomplete, I just don't know what else it needs.


Edit: import java.awt.image.BufferedImage; worked, no more compiler errors, hooray! I guess I misunderstood import java.awt.*;, I thought the asterisk acted like a search engine's wildcard, importing all java.awt files.

Okay, it's still not buffering, but at least it's compiling.
 
1) I know this has nothing really much to do with the problem at hand, but I'd reccomend checking out the Eclipse IDE. It's what I and everyone I know do Java development in. It does real-time compiling, so as you make changes, it adds red squiggles under stuff that won't compile (like MSWord does for misspelled words). It also an autocomplete feature for methods and stuff. There's also some other little stuff that comes in handy...for example, Crtl+Shift+M automatically imports stuff you need.

2) More on topic. You're getting a BufferedImage, grabbing its graphics pane, but you never seem to be using it. As per your TA's suggestions, instead of "g.drawCircle" and so on, you need to do bufferedGraphics.drawCircle, and then at the end do a "g.drawImage(frame)" and such. This is what he meant by draw everything to a bufferedimage. Behind the scenes, it's essentially preparing the canvas, and then on the drawImage command, it paints everything in one fell swoop.
 
[quote name='Koggit']import java.awt.image.BufferedImage; worked, no more compiler errors, hooray! I guess I misunderstood import java.awt.*;, I thought the asterisk acted like a search engine's wildcard, importing all java.awt files.

Okay, it's still not buffering, but at least it's compiling.[/quote]

Glad to hear that the compiling errors went away.

[quote name='Salamando3000']1) I know this has nothing really much to do with the problem at hand, but I'd reccomend checking out the Eclipse IDE. It's what I and everyone I know do Java development in. It does real-time compiling, so as you make changes, it adds red squiggles under stuff that won't compile (like MSWord does for misspelled words). It also an autocomplete feature for methods and stuff. There's also some other little stuff that comes in handy...for example, Crtl+Shift+M automatically imports stuff you need.[/quote]

I agree Eclipse is amazing. The real time compiling is great and I always made use of the autocomplete feature if I ever forgot the name of a method or something.

I wish I could help more, but I cannot really think of how to help since I haven't dealt with drawing a moving image. The best I could do for now is look through Sun's website. I found this section about double buffering. Which is you write to a buffer and then write to the screen so that you don't see the stuff being drawn on the screen, creating that image flickering problem.

Here is the link: http://java.sun.com/docs/books/tutorial/extra/fullscreen/doublebuf.html

I will try and look more into it tomorrow if I can.
 
bread's done
Back
Top