Kapitel 10: "Appletprogrammierung"

"MausBild.java"


import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class MausBild extends Applet {
int width, height, x, y, mx, my;
Image img, img2, img_ghost;
Graphics g_ghost;
boolean isInBox = false;

public void init() {
width = getSize().width;
height = getSize().height;
setBackground(Color.black);

addMouseListener(new MouseHelper());
addMouseMotionListener(new MouseMotionHelper());

img = getImage(getDocumentBase(), "schweden.jpg");
img2 = getImage(getDocumentBase(), "elch.jpg");

img_ghost = createImage(width, height);
g_ghost = img_ghost.getGraphics();

// mittige Position für den Elch zu Beginn
x = width/2 - 62;
y = height/2 - 55;
}

// *************************************************************
private class MouseHelper extends MouseAdapter{
public void mousePressed(MouseEvent e) {
mx = e.getX();
my = e.getY();
// ist die Maus innerhalb des Elch-Bildes?
if (x < mx && mx < x+124 && y < my && my < y+111) {
isInBox = true;
}
e.consume();
}

public void mouseReleased(MouseEvent e) {
isInBox = false;
e.consume();
}
}
// *************************************************************

// *************************************************************
private class MouseMotionHelper extends MouseMotionAdapter{
public void mouseDragged(MouseEvent e) {
if (isInBox) {
int new_mx = e.getX();
int new_my = e.getY();

// Offset ermitteln
x += new_mx - mx;
y += new_my - my;
mx = new_mx;
my = new_my;

repaint();
e.consume();
}
}
}
// *************************************************************

public void update( Graphics g ) {
g_ghost.drawImage(img, 0, 0, this);
g_ghost.drawImage(img2, x, y, this);
g.drawImage(img_ghost, 0, 0, this);
}

public void paint( Graphics g ) {
update(g);
}
}

Picksel Media Marco Block © 2006-2009 – ImpressumKontakt
Gestaltung und Umsetzung Tobias Losch, www.picksel-media.de