The_Ant
JGO Visitor
Java games rock!
|
 |
«
Posted
2003-04-24 07:13:56 » |
|
Hi,
I have a problem with my animation. Or to be more precise when it's finished. I would like to see on the screen the last image but I simply got the background of the frame.
I have a tile map and each cycle, there is a loop that call the tiles to draw what is on itself. Here is the method that do the job:
public void displayImageAt(String imageType, int imageIndex, int x, int y) { ImageIcon imageIcon; Image image; int index; int width, height; image = imagesDescriptor.getImageByName(imageType,imageIndex); // instead of storing Images, I'd better store ImageIcon // to avoid this studip instance creation each time! imageIcon = new ImageIcon(image); width = imagesDescriptor.getElementSize(); height = imagesDescriptor.getElementSize(); if (jPanel==null) { // null for no Layout Manager // true for dubbleBuffered jPanel = new JPanel(null,true); jPanel.setOpaque(false); frame.setContentPane(jPanel); } Dimension dim = jPanel.getSize(); RepaintManager rm = RepaintManager.currentManager(jPanel); Image off = rm.getOffscreenBuffer(jPanel, dim.width, dim.height); Graphics og = off.getGraphics(); Graphics g = jPanel.getGraphics(); if (og!=null) { try { imageIcon.paintIcon(jPanel, og,y*height, x*width); g.setClip(y*width,x*height,width, height); g.drawImage(off,0,0,jPanel); } finally { og.dispose(); g.dispose(); } } frame.setVisible(true); }
Yes, it's not very clean (creation of ImageIcon each time, but that's not the problem here, I'll fix it later). My problem is that when the simulation cycle stops the frame doesn't keep the last tiles diplayed but the background of the frame.
What should I do to fix that?
The frame is created in the constructor of my class like this:
frame = new JFrame("Environment Display"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {
System.exit(0); } });
frame.setBounds(120,120, 450,520); frame.setVisible(true);
and jPanel is a JPanel attribute of my class.
All propositions accepted. :)
Greetings,
The Ant.
|