Marks you are required to complete the following

This preview shows page 26 - 28 out of 28 pages.

Question 12 (10 marks) You are required to complete the following JPanel which uses a Timer object. The Timer object is created with a delay of 300 milliseconds and the Timer starts as soon as the JPanel is created. The JPanel displays two lines at right angles to each other and a blue ball of size 40 pixels (given by the constant, SIZE ) which initially is placed with its centre at the bottom of the vertical line. With each tick of the Timer the ball moves 40 pixels (given by the constant, SIZE ) up the vertical line until it reaches the top of the vertical line. Then the ball moves along the horizontal line until it reaches the end of the horizontal line. When the ball reaches the end of the horizontal line it is no longer able to move. The user controls the JPanel using the arrow keys in the following way: The DOWN arrow key At any stage the user can stop/restart the Timer by pressing the DOWN arrow key, i.e. if the Timer is running the Timer will stop, otherwise the Timer will start again. The UP arrow key Whenever the user presses the UP arrow key, the ball will be reset back to its initial position with its centre at the bottom of the vertical line. If the Timer is running then the ball will immediately start moving up the vertical line. If the Timer is not running the ball remains stationary until the user presses the DOWN arrow key. When the ball starts moving again it will move up the vertical line. The screenshot on the right shows the JPanel when it is first displayed. Below are some screenshots of the JPanel in action. ... ... ... Notes : 1. The variables, centreX and centreY , represent the centre of the blue ball. 2. You MUST use the variables and constants given in the code. import javax.swing.*; import java.awt.*; import java.awt.event.*; The ball starts moving up the vertical line. After reaching the top the ball moves to the right along the vertical line until it can no longer move. The user has pressed the UP arrow key and ball has started moving from the bottom of the vertical line again.
Image of page 26

Subscribe to view the full document.

Question/Answer Sheet - 30 - CompSci 101 ID: ........................................................... public class AJPanel extends JPanel implements ActionListener, KeyListener { //The size of the blue ball private final int SIZE = 40; //The left and right values of the horizontal line private final int LINE_LEFT = 120; private final int LINE_RIGHT = LINE_LEFT + 150; //The top and bottom values of the vertical line private final int LINE_TOP = 40; private final int LINE_BOTTOM = LINE_TOP + 200; //The two directions in which the ball travels public static final int UP = 0; public static final int RIGHT = 1; private int centreX, centreY; private int direction; private Timer t; public AJPanel() { centreX = LINE_LEFT; centreY = LINE_BOTTOM; direction = UP; addKeyListener(this); t = new Timer(300, this); t.start(); } public void actionPerformed(ActionEvent e) { if(direction == RIGHT) { centreX = centreX + SIZE; if (centreX >= LINE_RIGHT) { centreX = LINE_RIGHT; } } else if(direction == UP) { centreY = centreY - SIZE; if (centreY <= LINE_TOP) { centreY = LINE_TOP; direction = RIGHT; } } repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawLine(LINE_LEFT, LINE_TOP, LINE_RIGHT, LINE_TOP); g.drawLine(LINE_LEFT, LINE_TOP, LINE_LEFT, LINE_BOTTOM); g.setColor(Color.BLUE); g.fillOval(centreX - SIZE / 2,
Image of page 27
Question/Answer Sheet - 30 - CompSci 101 ID: ........................................................... centreY - SIZE / 2, SIZE, SIZE); } public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_DOWN ) { if (t.isRunning()) { t.stop(); } else { t.start(); } } else if (e.getKeyCode() == KeyEvent.VK_UP ) { centreX = LINE_LEFT; centreY = LINE_BOTTOM; direction = UP; repaint(); } } public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} } (10 marks)
Image of page 28
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern