import java.awt.*; public class BallFrame extends Frame implements MovableObj { int x = 0, y = 0; int sx = 15, sy = 15; int ux, uy; Image ball, backgr; String text; public Image loadImage(String imagePath, MediaTracker mt, int imageID) { Image img = getToolkit().getImage(imagePath); mt.addImage(img, imageID); try { mt.waitForID(imageID); } catch (Exception e) {} return img; } public BallFrame(String name) { super(name); setSize(FRAME_WIDTH, FRAME_HEIGHT); // Fenstergröße setzen setFont(new Font("TimesRoman", Font.PLAIN, 14)); // Font für Textausgaben MediaTracker tracker = new MediaTracker(this); // überwacht das Laden von Bildern ball = loadImage("images/logo.gif", tracker, 0); // Grafik mit Mediatracker laden sx = ball.getWidth(this); // Größe des Images merken sy = ball.getHeight(this); } public void moveAbs(int x, int y) { this.x = x; this.y = y; text = null; update(this.getGraphics()); } public void moveRel(int dx, int dy) { this.x += dx; this.y += dy; text = null; update(this.getGraphics()); } public void textOut(String text) { this.text = text; update(this.getGraphics()); } public void paint(Graphics g) { Dimension d = this.getSize(); // Größe des Fensters ermitteln ux = d.width / 2; // Mittelpunkt als Bezugspunkt setzen uy = d.height / 2; showText(g, d); // Textinformationen ausgeben showObj(g); // Objekt überlagernd zeichnen } public void showText(Graphics g, Dimension d) { g.setColor(this.getForeground()); g.drawString("U: (" + ux + ", " + uy + "); Pos: (" + x + ", " + y + ")", 10, d.height-10); if (text != null) g.drawString(text, 10, 40); // Text ausgeben } public void showObj(Graphics g) { g.translate(ux-sx/2, uy-sy/2); // Koordinatenursprung setzen if (ball != null) g.drawImage(ball, x, -y, this); else g.fillOval(x, y, sx, sy); g.translate(0, 0); } }