Results 1 to 2 of 2
Thread: Need help moving a shape
- 12-04-2011, 02:19 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
Need help moving a shape
Ok, new to the forum. This question inspired me to sign up, hi everyone.
I'm trying to make a snake game. I have 4 classes.
1.SnakeRunner (main)
2.Enviroment
3.SnakeDraw
4.FoodDraw
The problem is within the SnakeDraw class. I created a test for loop to increment the x values for the x coordinate of the snake rectangle. and then declared the repaint() method
inherited from the JPanel class. It doesn't make the shape move though. What am I doing wrong here?
Thanks in advance for any and all help I get.
SnakeRunner
EnviromentJava Code:import java.awt.Color; import java.awt.Container; import javax.swing.JFrame; class snakeRunner { public static void main(String[] args) { //Creates Frame JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(600,100); int length = 700; int width = 700; frame.setSize(length, width); frame.setVisible(true); //Sets background Color Container cp = frame.getContentPane(); cp.setBackground(Color.black); //Adds Food and Snake shapes to the frame Enviroment envi = new Enviroment(); frame.getContentPane().add(envi,java.awt.BorderLayout.CENTER); } }
SnakeDrawJava Code:import java.awt.Graphics; import javax.swing.JComponent; class Enviroment extends JComponent { public Enviroment() { } public void paintComponent(Graphics g) { //Draw Food FoodDraw food = new FoodDraw(); food.drawFood(g); //Draw Snake SnakeDraw snake = new SnakeDraw(); snake.drawSnake(g); } }
FoodDrawJava Code:import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; class SnakeDraw extends JPanel { public void drawSnake(Graphics g) { int x_coordinate = 550; int y_coordinate = 295; int length = 12; int width = 12; g.drawRect (x_coordinate, y_coordinate, length, width); g.setColor(Color.GREEN); g.fillRect(x_coordinate, y_coordinate, length, width); for(int x = 0; x < 120; x++) { x_coordinate++; this.repaint(); } } public void translate(double dx, double dy) { x += dx; y += dy; } private int x; private int y; }
Java Code:import java.awt.Color; import java.awt.Graphics; import javax.swing.JComponent; class FoodDraw { public void drawFood(Graphics g) { int x_coordinate = 150; int y_coordinate = 295; int length = 12; int width = 12; g.drawRect (x_coordinate, y_coordinate, length, width); g.setColor(Color.pink); g.fillRect(x_coordinate, y_coordinate, length, width); } }
- 12-04-2011, 02:30 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Need help moving a shape
Unless I'm missing something you have a method translate() in the SnakeDraw class, but you don't call it at any point.It doesn't make the shape move though.
-----
A deeper problem (I think) is the way you create a SnakeDraw instance each time you paint the environment. The whole point about motion is that identity is preserved while location changes. If it were me I would have a Snake class, and an instance of this class would be part of the environment. The Snake class would have a draw() method (your current drawSnake() renamed). With this setup you would have an instance of Snake on which to call the translate() method.
The same thing applies to food which another part of the environment.
Similar Threads
-
Need help moving a shape!
By chrishans in forum AWT / SwingReplies: 3Last Post: 10-16-2011, 07:17 AM -
Help with creating a shape and moving it with mouse and keyboard!
By konhee93 in forum New To JavaReplies: 1Last Post: 06-08-2011, 10:22 PM -
Make shape rounding (moving in circle)
By mneskovic in forum New To JavaReplies: 8Last Post: 08-17-2010, 03:05 PM -
how to change the shape of the JFrame to a oval shape
By kiki2009 in forum Java 2DReplies: 1Last Post: 04-02-2010, 12:48 PM -
rezise shape
By frankenstein in forum Java 2DReplies: 5Last Post: 07-30-2009, 12:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks