Results 1 to 5 of 5
- 05-27-2010, 02:44 PM #1
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
Help drawing images for pong game
Hi, my friend and I are working on our final project for the end of the year in Java. We really haven't worked outside of the topics in regular AP Computer Science A, so we need a little help. We want to create a basic Pong game with basic functionality. Right now we are trying to get the images to show up in our window, everything compiles correctly, but we are unable to get the paddle nor the ball drawn into our window. Any suggestions would be greatly appreciated. This is what we have so far:
Java Code://Ball Class import java.awt.*; public class Ball { protected int x = 400; protected int y = 300; protected int height = 20; protected int width = 20; protected int xdir = 2; protected int ydir = 0; public Ball() { } public void draw(Graphics g) { g.setColor(Color.GREEN); g.fillOval(x,y,width,height); } public void move() { x +=xdir; y +=ydir; if(x > 570 ){ setXDir(-8); setYDir(randomNum()); } if(x < 0 ){ setXDir(8); setYDir(randomNum()); } if(y < 2){ setYDir(8); setXDir(randomNum()); } if(y > 520){ setYDir(-8); setXDir(randomNum()); } } public int randomNum() { double r = Math.random(); int myNumber = (int)(r*2f); int num = 0; if(myNumber==0)num = -8; if(myNumber==1)num = 8; return num; } public void resetState() { x =400; y = 300; xdir = 0; ydir = 0; } public void setYDir(int y) { ydir = y; } public void setXDir(int x) { xdir = x; } public int getYDir() { return ydir; } public int getXDir() { return xdir; } } //GamePaddle class. import java.awt.*; public class GamePaddle { protected int x = 575; protected int y = 300; protected int height = 110; protected int width = 15; int num; public GamePaddle () { } public void moveRight() { if (x > 500) { x += 0; } else x+=40; } public void moveLeft() { if (x < 15) { x -= 0; } else x-=40; } public Rectangle getRect() { return new Rectangle(x, y,width, height); } public void draw(Graphics block) { block.setColor(Color.YELLOW); block.fillRect(x,y,width,height); } } //Pong Class import java.awt.*; import java.awt.event.*; import javax.swing.*; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ public class Pong extends JFrame { private JPanel jContentPane = null; // This is the panel of the game class private PongGame panel = null; private PongGame getPanel() { if (panel == null) { // This creates a new game panel panel = new PongGame(); } return panel; } /** * This is the default constructor */ public Pong() { initialize(); // Listens for the keyboard buttons and controls this.addKeyListener(new KeyAdapter() { //The button that is pressed @Override public void keyPressed(KeyEvent evt) { formKeyPressed(evt); } // the button that is released @Override public void keyReleased(KeyEvent evt) { formKeyReleased(evt); } }); } public void formKeyPressed(KeyEvent evt) { panel.keyPressed(evt); } public void formKeyReleased(KeyEvent evt) { panel.keyReleased(evt); } // Sends which keys were pressed and release to the game class //This creates the frame and its dimensions private void initialize() { this.setResizable(false); this.setSize(300, 300); this.setContentPane(getJContentPane()); this.setTitle("Pong"); } //intialize jContentPane private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(new BorderLayout()); jContentPane.add(getPanel(), BorderLayout.CENTER); } return jContentPane; } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { Pong thisClass = new Pong(); thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); thisClass.setVisible(true); } }); } } //PongGame Class import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.event.KeyEvent.*; public class PongGame { private Ball ball; private GamePaddle Player1; private GamePaddle Player2; public PongGame() { Ball ball = new Ball(); GamePaddle Player1 = new GamePaddle(); GamePaddle Player2 = new GamePaddle(); } public void keyPressed(KeyEvent evt) { if(evt.getKeyChar() == VK_A) Player1.moveLeft(); else if(evt.getKeyChar() == VK_D) Player1.moveRight(); else if(evt.getKeyChar() == VK_KP_LEFT) Player2.moveLeft(); else if(evt.getKeyChar() == VK_KP_RIGHT) Player2.moveRight(); } }
-
Your current code as posted doesn't compile for me. Is it missing anything?
Much luck!
- 05-27-2010, 07:55 PM #3
I've found many problems before being able to see the paddle and ball.
First you need to get the program to compile.
Suggestion on use of literals (300, 500, etc) for sizes and initial displacements and offsets etc. Don't hardcode values deep in the code. Its better to use variables defined at the start of the program that are easy to see and change.
Make the frame large enough so the images are shown. 300 is too small for some.
How does the program draw? It needs to use the what I call the AWT engine to get access to the Graphics object to draw with. That's done by overriding a paint method somewhere and then calling your draw methods.
-
Agrees with Norm. Also, since it appears that you're using Swing, you should instead override a JPanel's paintComponent method and do drawing in there.
- 05-28-2010, 02:10 PM #5
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Pong game
By hannes in forum Threads and SynchronizationReplies: 4Last Post: 02-03-2011, 10:52 AM -
Ping pong game
By adam405 in forum New To JavaReplies: 8Last Post: 10-20-2010, 03:52 PM -
The bug in the Pong game
By Phoeenix in forum New To JavaReplies: 4Last Post: 07-21-2009, 07:13 PM -
drawing images
By diggitydoggz in forum New To JavaReplies: 4Last Post: 01-02-2009, 03:15 AM -
Help with pong game
By Eric in forum New To JavaReplies: 2Last Post: 07-03-2007, 07:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks