jGuru Forums
Posted By:
Jonathan_Wolk
Posted On:
Wednesday, January 2, 2002 04:58 PM
I have been writing a client side application that's almost done but it has 3 errors that I do not know how to address. My problems are as follows
1.
I have Images in my buttons, other images around the application and when I bring up a dialog box that happens to "hide" i.e. cover the Image completely, the Image/ImageButton disappears and is repainted. I know that whenever a dialog box is over a component, the component's
paint()
is called, but how come my Image gets erased? Any ideas on how to stop this from clearing and repainting?
Problem
2.
I have Scrollpanes with large Images in them (the images are on top of canvases) and whenver I scroll fast enough there is large flickering. I've heard simple explanations like, "just double buffer." The problem is, how to implement this double buffering and when to realize to use it etc etc. A very detailed explanation would be greatly appreciated.
Problem
3.
I want a FileDialog to be able to get multiple files. Anyway to do this? (I can't use swing..so JFileChooser is out).
Thanks ahead of time...these things are driving me crazy.
-Jonathan
Re: Disappearing Images, Flickering and Multiple File Loadings (No Swing)
Posted By:
Jon_Morton
Posted On:
Thursday, January 3, 2002 12:04 AM
I've been working on games using AWT and have written some double buffering code.
Here is an example I just wrote to demo double buffering. Take a look at the code below, feel free to ask questions, I'm not sure how much of an explanation you want.
import java.awt.*;import java.awt.image.*;public class DoubleBuffer extends Frame implements Runnable { // Two important objects needed for double buffering. // The image buffer which you will be drawn, and second // the a reference to the BufferedImage Graphic object. private BufferedImage buffer; private Graphics graphics; // Used to position a rectangle for the demo. int x = 20; int y = 20; // The height and width of the frame/window. int width = 300; int height = 300; // Yep, the constructor... public DoubleBuffer() { buffer = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); graphics = (Graphics2D)buffer.getGraphics(); setBackground(Color.darkGray); setSize(width,height); setVisible(true); } public void update(Graphics g) { paint(g); // Draw whatever is in the buffer to the frame. g.drawImage(buffer, 0, 0, this); } public void paint(Graphics g) { // NOTICE WE AREN'T USING "g" , we use "graphics" declared above. // graphics is a reference to an offscreen buffer's graphics context. graphics.clearRect(0,0,width,height); graphics.setColor(Color.red); graphics.fillRect(x,y,10,10); } public void run() { try { while(true) { // Move the triangle. x+=10; y+=3; if(y>250) y = 10; if(x>250) x = 10; // Repaint everything. repaint(); // Wait 50 milliseconds. Thread.sleep(50); } } catch (InterruptedException e) { System.out.println("o->Thread interrupted."); } } public static void main(String[] args) { DoubleBuffer db = new DoubleBuffer(); Thread thread = new Thread(db); thread.start(); }}