12. oktober 2001 - 12:46Der er
8 kommentarer og 1 løsning
java2d og double buffer
Jeg er ved at lave en skabelon til noget java2d men har lidt problemer. Jeg vil gerne have skiftet \"g2d.drawString(\"TEST\", posX, posY)\" ud med et billede (pic.gif) der bliver vist på posX, posY. Billedet skal ikke \"blinke\" ligesom teksten gør(prøv programmet..) Jeg ønsker helst noget kode jeg kan copy and paste direkte ind i kode... men et link eller gode hints er også ok.
Min kode: import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class Gui extends JFrame implements WindowListener, KeyListener, MouseMotionListener, Runnable{
private int frameWidth = 640; private int frameHeight = 480; private int posX = 40; private int posY = 40; private Thread internalThread; private boolean running = true;
public Gui(){ createFrame(); internalThread = new Thread(this); internalThread.start(); }
jeg bruger denne her til at dobbeltbuffere med, og det virker :)
import java.awt.*; import java.applet.*;
public class DoubleBufferApplet extends Applet { // The width and height of our offscreen image private int width=-1; private int height=-1;
// The offscreen image private Image offscreen;
// switch: are we double buffering or not? private boolean dbon = true;
// Use this to turn double buffering on and off protected void setDoubleBuffering( boolean dbon ) { this.dbon = dbon; if (!dbon) { offscreen = null; } }
// Depending on the value of our switch, we either call our // special code, or just call the default code public void update( Graphics g ) { if (dbon) { updateDoubleBufffered( g ); } else { super.update( g ); } }
// Do the drawing to an offscreen buffer -- maybe private void updateDoubleBufffered( Graphics g ) {
// Let\'s make sure we have an offscreen buffer, and that // it\'s the right size. If the applet has been resized, // our buffer will be the wrong size and we need to make // a new one Dimension d = getSize(); if (offscreen == null || width!=d.width || height!=d.height || offscreen==null) { width = d.width; height = d.height; if (width>0 || height>0) { offscreen = createImage( width, height ); } else { offscreen = null; } }
// If we still don\'t have one, give up if (offscreen == null) return;
// Get the off-screen graphics object Graphics gg = offscreen.getGraphics();
/** This function handles everything about the player. * That is collision control, checking to see if a pellet or a fruit has been eaten, * updating the score, and se if a ghost has catched the player. */ private void handlePlayer() { }
public void keyReleased(java.awt.event.KeyEvent e) { }
public void keyPressed(java.awt.event.KeyEvent e) { handleKey(e); }
public void keyTyped(java.awt.event.KeyEvent e) { }
private void handleKey(KeyEvent e) { switch(e.getKeyCode()) { case java.awt.event.KeyEvent.VK_LEFT: x-=2; break; case java.awt.event.KeyEvent.VK_RIGHT: x+=2; break; case java.awt.event.KeyEvent.VK_DOWN: y+=2; break; case java.awt.event.KeyEvent.VK_UP: //up y-=2; break; } e.consume(); } }
Og den sidste fil:
/* * Ghost.java * * Created on 18. februar 2001, 18:55 */
Jeg får en: java.lang.StackOverflowError at sun.java2d.loops.ICRFillRectRasterContext.invoke(IntDiscreteRenderer.java:261) at sun.awt.image.BufferedImageGraphics2D.fillRect(BufferedImageGraphics2D.java:653) at sun.java2d.pipe.ValidatePipe.fillRect(ValidatePipe.java:42) at sun.java2d.SunGraphics2D.fillRect(SunGraphics2D.java:1560) at Gui.updateDoubleBufffered(Gui.java:70) at Gui.paint(Gui.java:50) at Gui.updateDoubleBufffered(Gui.java:72) at Gui.paint(Gui.java:50) at Gui.updateDoubleBufffered(Gui.java:72)
med følgende kode: import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class Gui extends JFrame implements WindowListener, KeyListener, MouseMotionListener, Runnable{
private int frameWidth = 640; private int frameHeight = 480; private int posX = 40; private int posY = 40; private Thread internalThread; private boolean running = true; private boolean doubleBuffered = true; private Image offscreen; private int width=-1; private int height=-1;
public Gui(){ createFrame(); internalThread = new Thread(this); internalThread.start(); }
Det virkede... Mange tak For en god ordens skyld poster jeg lige det stykke kode der virker, hvis andre skulle være interesseret.. import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class Gui extends JFrame implements WindowListener, KeyListener, MouseMotionListener, Runnable{
private int frameWidth = 640; private int frameHeight = 480; private int posX = 40; private int posY = 40; private Thread internalThread; private boolean running = true; private boolean doubleBuffered = true; private Image offscreen; private int width=-1; private int height=-1;
public Gui(){ createFrame(); internalThread = new Thread(this); internalThread.start(); }
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.