I am trying to make a display window with three buttons, "Drop", "Retrieve", and "Quit". When "Drop" is clicked, a ball must drop from the top of the screen and stop at the bottom. When "Retrieve" is clicked, a line must ascend from the top of the screen to retrieve the ball, and bring the ball back to the top of the screen. "Quit" should do the obvious. I was given this to start with:
/** * A class that puts a graphics window on your display */ import java.awt.*; import javax.swing.*; public class DisplayWindow extends JFrame { /** * Content pane that will hold the added Jpanel */ private Container c; /** * DisplayWindow constructor - no parameters */ public DisplayWindow() { super("Display"); c = this.getContentPane(); } /** * Adds panel to content pane * * @parameter the panel to be added */ public void addPanel(JPanel p) { c.add(p); } /** * consolidates the frame, makes it visible, causes program termination when * window is closed manually */ public void showFrame() { this.pack(); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
public class DropDriver { public static void main(String[] args) { DisplayWindow d = new DisplayWindow(); DropPanel b = new DropPanel(); d.addPanel(b); d.showFrame(); } }
And I must create the DropPanel class. This is what I have so far:
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class DropPanel extends JPanel implements ActionListener{ int width = 300; int height = 600; Timer ticker = new Timer(20, this); int x = 0; int y = 0; } public Drop(){ set preferred size(new Dimension (width,height)); ticker.start(); } public void paintComponent(Graphics g){ super.paintComponent(g); g.drawOval(x,y,50); } public void actionPerformed(ActionEvent e){ if(e.getSource() == ticker){ y = y-2; } repaint(); } }
any suggestions on where to go from here? I'm having a hard time.