package dk.itu.oop.ballgame; import dk.itu.oop.lecture1.Ball; import java.awt.*; public class MovingBall extends Ball { private final Component myComponent; private Color col; public MovingBall(String color, Component c){ super(color); y = 20; myComponent = c; if ( color.equals("Red") ) col = Color.RED; if ( color.equals("Green") ) col = Color.GREEN; if ( color.equals("Blue") ) col = Color.BLUE; } protected int dx=5, dy=3; public void draw(){ Graphics g = myComponent.getGraphics(); g.setColor(col); g.fillOval(x,y,10,10); } public void move(){ int xmax = (int)( myComponent.getBounds().getWidth() ); int ymax = (int)( myComponent.getBounds().getHeight() ); if (x+dx < 0 || x+dx > xmax ) dx = -dx; if (y+dy < 20 || y+dy > ymax ) dy = -dy; x += dx; y += dy; } }