Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Game Development
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Game Development
Need help with game
Krupa Patel
Greenhorn
Posts: 10
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I am trying to move the ball on the drawing panel. My code seems correct but when I run it, nothing is happening.
Can anyone please tell me what I am doing wrong?
import java.awt.*; public class Ball{ private int _x; private int _y; private int _d; private javax.swing.JPanel _panel; // peer object (and container) private javax.swing.Timer _timer; int _ballX,_ballY; public Ball(javax.swing.JPanel aPanel,int x, int y, int d){ _x = x; _y = y; _d = d; } public void paint(Graphics g){ g.setColor(Color.red); g.fillOval(_x,_y, _d, _d); g.setColor(Color.black); g.drawOval(_x,_y,_d,_d); } public void setX(int x){ _x = x; } public void setY(int y){ _y = y; } public int getX(){ return _x; } public int getY(){ return _y; } public int getD(){ return _d; } public void setLocation(int x, int y){ } public void move(int dx, int dy){ } }
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class GameBoard extends javax.swing.JPanel implements Mover{ private Paddle _paddle1; private Paddle _paddle2; private Ball _ball; Boolean _boolean; private Timer _timer; private final int INTERVAL = 100; public GameBoard(){ super(); this.setBackground(new Color(150,250,200)); this.setLayout(new FlowLayout()); this.setPreferredSize(new Dimension(550,550)); this.init(); _paddle1 = new Paddle(200,500,15,60); _paddle2 = new Paddle(270,500,15,60); _ball = new Ball(this,150,400,20); _timer = new Timer(INTERVAL,this); _timer.start(); //this.addMouseListener(); } public void paint(Graphics g){ _ball.paint(g); _paddle1.paint(g); _paddle2.paint(g); } public void move(){ _ball.getX(); _ball.getY(); _ball.move(2,3); _paddle1.move(2,3); _paddle2.move(2,3); this.repaint(); } }
Michael Dunn
Ranch Hand
Posts: 4632
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
perhaps the Ball class needs some code here
public void move(int dx, int dy){ }
Krupa Patel
Greenhorn
Posts: 10
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
It's still not working. please give me some hints.
_ballX=5; _ballY=0; public void move(int dx, int dy){ int nextX = (int)this.getX()+_ballX; int nextY = (int)this.getY()+_ballY; this.setLocation(nextX, nextY); }
Michael Dunn
Ranch Hand
Posts: 4632
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
honestly, you will have to develop some debugging skills.
I pointed you to the empty method move(), so you added some code to move() which included this line:
this.setLocation(nextX, nextY);
now take a look at setLocation() - what do you expect it to do?
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Self controlling component
When I use MediaTracker , the method "paint(Graphics)" will be invoked ceaselessly!!
noob needs paint help
Need help with making a game
can not move the paddle in this program
More...