jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes Moving graphic in relation to another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Moving graphic in relation to another" Watch "Moving graphic in relation to another" New topic
Author

Moving graphic in relation to another

JD Thompson
Greenhorn

Joined: Jun 29, 2006
Posts: 21
I am a soccer coach and want to develop a simple app which the players see how the team should move relative to the position of the ball. After reviewing several articles, my initial approach would be as follows:

1. Create a soccer field image and set as the background image of the frame or parent panel.

2. Create a class ball which allows itself to be moved by the mouse with the MouseListener interface and have public variables corresponding to it's x, y coordinates.

3. Create a player class which when a MouseListener mouseKeyDown event takes place, "looks" at the ball and retrieves the x, y. Then, depending upon position, will calculate a new position to be drawn to.

My questions are as follows:
1. Is my basic logic a good path to begin upon?
2. Where do I use threads (I have read a couple of articles and am confused about how to use them relative to several graphics, i.e. bouncing balls demo)?
3. Any other basic pointers before I begin coding?

Thanks in advance for your reply and time!

JD


New Developer<br />Happy to learn what I don't know<br />Happy to share what I do<br />
Nick Meverden
Ranch Hand

Joined: Jul 03, 2006
Posts: 59
public void run() {
long beforeTime;
long timeDiff;
long sleepTime;

beforeTime = System.currentTimeMillis();

running = true;

while (running) {
//do repainting here

int period = 10;
timeDiff = System.currentTimeMillis() - beforeTime;
sleepTime = period - timeDiff;

if (sleepTime <= 0) {
sleepTime = 5;
}

try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
System.out.println("Error: " + e);
}

beforeTime = System.currentTimeMillis();
}



This should keep your app around 100 FPS across all systems.
JD Thompson
Greenhorn

Joined: Jun 29, 2006
Posts: 21
Thanks Nick for the quick response and associated code.

so I would create the following?:

class Player implements Runnable
{
int xpos=50;
int ypos=50;
String name;

public Player(String n)
{
name=n;
//initial drawing of component here
}

public void run()
{
//your example code here
}
}

Is that the basic idea?

Thanks!

JD
Nick Meverden
Ranch Hand

Joined: Jul 03, 2006
Posts: 59
http://www.amazon.com/gp/product/0596007302/102-6463470-6497726?v=glance&n=283155

In this book, he creates some basic Parent classes to be extended.

Example: public class GamePanel extends JPanel implements Runnable

Then you extend GamePanel and add it to the Container you want, JFrame, JApplet, etc... Though I am die hard Factory fan http://www.swingwiki.org/best:using_a_factory
JD Thompson
Greenhorn

Joined: Jun 29, 2006
Posts: 21
Thanks for the pointers! I will order the book as it does look good.

Okay, the ball is moving, and I will build the rest of this simple application off this basic template/format. However, I would highly value a critique of the code, i.e. did I do anything that worked here but will set myself up for failure down the road, i.e. newbie logic errors?

Thanks tons for your help Nick!!!

code::

public class AnimateBall extends JPanel implements Runnable
{
private int x;
private int y;

public AnimateBall()
{
x=50;
y=50;
Thread t=new Thread(this);
t.start();
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.fillOval(x,y,40,40);
}

public void run()
{
long beforeTime;
long timeDiff;
long sleepTime;

beforeTime = System.currentTimeMillis();

boolean running = true;

while (running)
{
// do repainting here
x +=4;
y +=4;
repaint();

int period = 10;
timeDiff = System.currentTimeMillis() - beforeTime;
sleepTime = period - timeDiff;

if (sleepTime <= 0)
{
sleepTime = 5;
}

try
{
Thread.sleep(sleepTime);
}
catch (InterruptedException e)
{
System.out.println("Error: " + e);
}
beforeTime = System.currentTimeMillis();
}
}

public static void main(String [] args)
{
JFrame myWindow=new JFrame();
myWindow.setSize(500,500);
AnimateBall myBall=new AnimateBall();
WindowDestroyer myDestroyer=new WindowDestroyer();
myWindow.addWindowListener(myDestroyer);
myWindow.getContentPane().add(myBall);

myWindow.setVisible(true);
}
}
Nick Meverden
Ranch Hand

Joined: Jul 03, 2006
Posts: 59
If you're looking for improved performance I would recommend using double buffering. In stead of doing all your drawing in paintComponent() you have a second method that creates/paints to a buffered Image. Then after that method is called to update the buffer, paintComponent() just paints the buffered Image to screen.

- Nick
JD Thompson
Greenhorn

Joined: Jun 29, 2006
Posts: 21
Thanks, I will do a quick study on double buffering to an image.

In terms of approach, I have a question. For the ovals representing the seven players, the image representing the ball, and the field lines layout, do I paint each of these under the extended JPanel class with the paintComponent? Or do I create a separate Object class representing Player, Ball, and Field? I am a bit confused on how to approach at this point? I am guessing that for the field layout I establish that as the background image for the panel.

As usual, eager to hear your reply!

Take care!

JD
Nick Meverden
Ranch Hand

Joined: Jul 03, 2006
Posts: 59
In the "Killer Gaming" book, he creates "Sprites" which are the components within a game/app example: your Ball. He creates a Parent class that each component extends from this Sprite class. This class has methods like update(), moveRight(), moveLeft(), hit() which draw themselfs on top of the BufferedImage before it's painted to screen. It'll make more sense after you start reading the book.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Moving graphic in relation to another