Computer Science Canada Pong and menu help |
Author: | LightfireStorm [ Sat Apr 08, 2017 9:40 am ] |
Post subject: | Pong and menu help |
hello I am having troble getting the paddle to go up and down... I don't know what else to try can some one help me? here is my code: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import static java.awt.event.KeyEvent.*; /** * This class implements a panel which can be inserted into a JFrame. * The panel implements the Pong game. */ public class Pong2 extends JPanel implements Runnable, ActionListener{ // ball positioning variables int ball_x, ball_y; // ball move pixel size int ball_dx=2, ball_dy=2; // ball size int ball_r = 20; // rendering area pixel boundary variables int x_left, x_right, y_top, y_bottom; // the actual frame JFrame frame; // the color of the ball Color ball_color = Color.blue; // the amount of time we wait when moving the ball long moveWait = 15; int paddle_y, paddle_x, paddle_w, paddle_h, paddle_dy; /** * Constructor */ public Pong2(final JFrame frame){ addKeyListener(new ImputKeyEvents()); this.frame = frame; //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI asynchronously to //ensure that all the swing resources are already set-up when we do this. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(frame); } }); } /** * this is where we set up the UI */ protected void createAndShowGUI(final JFrame frame){ // initialize the frame frame.setContentPane(this); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setJMenuBar(createAndInitMenuBar()); // register for window resize events frame.addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e) { // This is only called when the user releases the mouse button. x_right = frame.getContentPane().getWidth() - ball_r; y_top = frame.getContentPane().getHeight() - ball_r; } }); // size the frame and show frame.pack(); frame.setSize(500,500); frame.setVisible(true); getFocus(frame); // start the ball off in the middle ball_x = this.getWidth()/2; ball_y = this.getHeight()/2; // initialize the randering boundaries x_left = 0; x_right = this.getWidth() - ball_r; y_top = this.getHeight() - ball_r; y_bottom = 0; paddle_y = 225; paddle_x = 30; paddle_w = 20; paddle_h = 50; paddle_y += paddle_dy; } /** * creates the menu */ protected JMenuBar createAndInitMenuBar(){ JMenuBar menuBar; JMenu menu, submenu; JMenuItem menuItem; JRadioButtonMenuItem rbMenuItem; JCheckBoxMenuItem cbMenuItem; //Create the menu bar. menuBar = new JMenuBar(); //Build the first menu. menu = new JMenu("Speed"); menu.setMnemonic(VK_S); menu.getAccessibleContext().setAccessibleDescription("Set the speed of the ball"); menuBar.add(menu); //a group of JMenuItems menuItem = new JMenuItem("Increase", VK_I); menuItem.setAccelerator(KeyStroke.getKeyStroke(VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription("Increase the speed of the ball"); menuItem.addActionListener(this); menu.add(menuItem); menuItem = new JMenuItem("Decrease", VK_D); menuItem.setAccelerator(KeyStroke.getKeyStroke(VK_2, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription("Decrease the speed of the ball"); menuItem.addActionListener(this); menu.add(menuItem); //Build second menu in the menu bar. menu = new JMenu("Another Menu"); menu.setMnemonic(VK_N); menu.getAccessibleContext().setAccessibleDescription("This menu does nothing"); menuBar.add(menu); return menuBar; } /** * helper method which we use to get the focus */ public void getFocus(final JFrame frame) { EventQueue.invokeLater(new Runnable() { public void run() { frame.requestFocus(); } }); } /** * implementation of the Runnable interface to be able to move the ball, etc. */ public void run(){ while(true){ ball_x += ball_dx; if(ball_x <= x_left || ball_x >= x_right){ ball_dx *=-1; ball_x += (2*ball_dx); } ball_y += ball_dy; if(ball_y <= y_bottom || ball_y >= y_top){ ball_dy *=-1; ball_y += (2*ball_dy); } /* if (collision()) { ball_dx *= -1; ball_x += (2 * ball_dx); }*/ paddle_y += paddle_dy; if (paddle_y >= y_top) { paddle_dy -= 10; } paddle_y += paddle_dy; if (paddle_y <= y_bottom) { paddle_dy += 10; } repaint(); try{ Thread.sleep(moveWait); }catch(InterruptedException ex){ System.out.println(ex); } } } /** * all rendering occurs here */ public void paint(Graphics g){ g.setColor(Color.white); g.fillRect(0,0,this.getWidth(),this.getHeight()); g.setColor(ball_color); g.fillOval(ball_x,ball_y, ball_r, ball_r); g.setColor(Color.black); g.fillRect(paddle_x, paddle_y, paddle_w, paddle_h); } /** * entry point into the program */ public static void main(String[] args){ // create the class(es) JFrame frame = new JFrame("Pong #2"); Pong2 application = new Pong2(frame); new Thread(application).start(); } // Action Listener public void actionPerformed(ActionEvent e) { JMenuItem source = (JMenuItem) (e.getSource()); String s = "Action event detected." + "\n" + " Event source: " + source.getText() + " (an instance of " + getClassName(source) + ")"; System.out.println(s); if (source.getText() == "Increase") { moveWait -= 2; if (moveWait < 1) moveWait = 1; } if (source.getText() == "Decrease") { moveWait += 2; if (moveWait > 400) moveWait = 400; } } public class ImputKeyEvents extends KeyAdapter { @Override public void keyPressed(KeyEvent e) { super.keyPressed(e); int KeyCode = e.getKeyCode(); if (KeyCode == KeyEvent.VK_UP) { paddle_dy += 5; } else if (KeyCode == KeyEvent.VK_DOWN) { paddle_dy -= 5; } } @Override public void keyReleased(KeyEvent e) { int KeyCode = e.getKeyCode(); if (KeyCode == KeyEvent.VK_UP) { paddle_dy += 0; } else if (KeyCode == KeyEvent.VK_DOWN) { paddle_dy += 0; } } } // Returns just the class name -- no package info. protected String getClassName(Object o) { String classString = o.getClass().getName(); int dotIndex = classString.lastIndexOf("."); return classString.substring(dotIndex+1); } } |