26. februar 2004 - 14:25Der er
3 kommentarer og 1 løsning
Hjæælp har problemer med double-buffering
Skal have lavet det sådan at der indlæses nogle billeder oven på et standartbillede (som derved bliver til baggrundsbillede) som skal bruges til at ligge i baggrunden af min applet. Oven på den skal der så hele tiden tegnes et andet billede, som følger musens possiton. Når jeg så klikke med musen skal det billede som er ved musens possiton liggen ind på baggrundsbilledet. Hvordan gør man det. (der skal bruges double-buffering)
public class ImageApplet extends Applet implements MouseMotionListener,MouseListener {
// The object we will use to write with instead of the standard screen graphics Graphics bufferGraphics; // The image that will contain everything that has been drawn on // bufferGraphics. Image offscreen; Image background, someImg; // To get the width and height of the applet. Dimension dim; int curX, curY,clicks; ArrayList images;
public void init() { // We'll ask the width and height by this dim = getSize(); // We'll redraw the applet eacht time the mouse has moved. addMouseMotionListener(this); addMouseListener(this); setBackground(Color.black); // Create an offscreen image to draw on // Make it the size of the applet, this is just perfect larger // size could slow it down unnecessary. offscreen = createImage(dim.width,dim.height); // by doing this everything that is drawn by bufferGraphics // will be written on the offscreen image. bufferGraphics = offscreen.getGraphics(); background = loadImage("background.jpg"); someImg = loadImage("little.jpg"); images = new ArrayList();//used to store images }
public void paint(Graphics g) { // Wipe off everything that has been drawn before // Otherwise previous drawings would also be displayed. bufferGraphics.clearRect(0,0,dim.width,dim.width); bufferGraphics.setColor(Color.red);
// draw the rect at the current mouse position // to the offscreen image bufferGraphics.drawImage(background, 0, 0, null);//background for(Iterator i = images.iterator();i.hasNext();) { ImageData imgdata = (ImageData)i.next(); bufferGraphics.drawImage(someImg, imgdata.getX(),imgdata.getY(), null);//background } bufferGraphics.fillRect(curX,curY,20,20); //draw all images in list
// draw the offscreen image to the screen like a normal image. // Since offscreen is the screen width we start at 0,0. bufferGraphics.drawString(""+clicks,10,10); g.drawImage(offscreen,0,0,this); }
// Always required for good double-buffering. // This will cause the applet not to first wipe off // previous drawings but to immediately repaint. // the wiping off also causes flickering. // Update is called automatically when repaint() is called.
public void update(Graphics g) { paint(g); }
// Necessary for RealMedia to load your images public Image loadImage(String file) { return getImage(getCodeBase(),file); }
// Save the current mouse position to paint a rectangle there. // and request a repaint() public void mouseMoved(MouseEvent evt) { curX = evt.getX(); curY = evt.getY(); repaint(); }
// The necessary methods. public void mouseDragged(MouseEvent evt) { }
public void mouseClicked(MouseEvent e) { images.add(new ImageData(e.getX(),e.getY())); clicks++; repaint();
} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} }
//til at gemme koordinater
public class ImageData { private int x; private int y;
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.