Pick one of the following problems to do for your interclass problem.
1. Key Ball Bounce - Make an applet that draws a bouncing ball (using proper gravity style acceleration). When the ball hits the bottom of the applet it should reverse direction and start moving back up. Have the applet listen for keys that cause it to move from side to side. When it hits an edge it should turn around and go the other way.
public class KeyGravBounceBallApplet extends JApplet implements MouseListener,KeyListener,ActionListener {
private static final long serialVersionUID = 7715200702479534506L;
private int x,y,vx,vy;
private Timer timer;
public void init() {
timer=new Timer(50,this);
addMouseListener(this);
addKeyListener(this);
}
public void start() {
timer.start();
}
public void stop() {
timer.stop();
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.black);
g.fillOval(x-10,y-10,20,20);
}
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {
requestFocus();
}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_LEFT) {
vx--;
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
vx++;
}
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
/**
* The use of absolute value in this prevents things from getting into
* a state where things oscillate.
*/
@Override
public void actionPerformed(ActionEvent e) {
x+=vx;
y+=vy;
if(y>=getHeight()-10) {
vy=-Math.abs(vy);
} else {
vy++;
}
if(x<=10) {
vx=Math.abs(vx);
}
if(x>=getWidth()-10) {
vx=-Math.abs(vx);
}
repaint();
}
}
2. Mouse Ball Bounce - Making the same bouncing ball that you had for the first one, but instead of having it move side to side with the keys, make it so that any time you click, the ball moves to the location of the click.
public class MouseGravBounceBallApplet extends JApplet implements MouseListener,ActionListener {
private static final long serialVersionUID = 7715200702479534506L;
private int x,y,vy;
private Timer timer;
public void init() {
timer=new Timer(50,this);
addMouseListener(this);
}
public void start() {
timer.start();
}
public void stop() {
timer.stop();
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.black);
g.fillOval(x-10,y-10,20,20);
}
@Override
public void mouseClicked(MouseEvent e) {
x=e.getX();
y=e.getY();
}
@Override
public void mouseEntered(MouseEvent e) {
requestFocus();
}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
/**
* The use of absolute value in this prevents things from getting into
* a state where things oscillate.
*/
@Override
public void actionPerformed(ActionEvent e) {
y+=vy;
if(y>=getHeight()-10) {
vy=-Math.abs(vy);
} else {
vy++;
}
repaint();
}
}
3. Choices - Use the JOptionPane to let the user select a color and have something drawn in that color. The JOptionPane should pop up if the user clicks the mouse.
public class OptionApplet extends JApplet implements MouseListener {
private static final long serialVersionUID = 6771377688109219957L;
private Color color=Color.black;
private Color[] colors={Color.black,Color.red,Color.blue,Color.green,Color.orange,Color.cyan,Color.magenta};
public void init() {
addMouseListener(this);
}
public void paint(Graphics g) {
g.setColor(color);
g.fillRect(0,0,getWidth(),getHeight());
}
@Override
public void mouseClicked(MouseEvent e) {
color=(Color)JOptionPane.showInputDialog(this,"What color do you want to draw?","Pick a Color",JOptionPane.QUESTION_MESSAGE,null,colors,color);
repaint();
}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
}
4. Free Form - Write an applet that does something interesting. You can do whatever you want. Just make it include at least one listener either for animation or response to user input.
This one is whatever you write.