File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes bouncing ball Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "bouncing ball" Watch "bouncing ball" New topic
Author

bouncing ball

oluwaseun akinmayowa
Greenhorn

Joined: Apr 04, 2011
Posts: 6

I need help to draw two oval, bouncing in different speed.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40041
    
  28
Welcome to the Ranch

Please show us what you have got already.

I think your discussion will be moved to a different forum.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31057
    
232

What technology are you using? Swing?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

I am moving this to 'Java in general'.

There are a couple of ways you could do this, starting with the technology in question. Java swing / HTML + JS / Applets etc etc.

What have you done so far ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
oluwaseun akinmayowa
Greenhorn

Joined: Apr 04, 2011
Posts: 6

Campbell Ritchie wrote:Welcome to the Ranch

Please show us what you have got already.

I think your discussion will be moved to a different forum.




import java.applet.*;
import java.awt.*;
public class Project6 extends Applet implements Runnable
{
private int index = 0;
int[] horiz = {70,70,70,70,};
int[] vert = {140,200,210,220};


int[] horiz1 = {170,170,170,170};
int[] vert1 = {140,200,210,220};

int oldx = horiz[0], oldy = vert[0];
int oldxx = horiz1[0], oldyy = vert1[0];

int x_pos = 100;
int y_pos = 100;


int y_speed = 1;

int radius = 10;


int appletsize_x = 200;
int appletsize_y = 100;

boolean firstTime = true;


private int sleep = 200;

private Image dbImage;
private Graphics dbg;


public void start()
{
index = 0;

Thread th = new Thread (this);

th.start ();
}

public void run ()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);


while (true)
{
if (y_pos > appletsize_y + radius || (x_pos < 0))
{

y_speed = -1;
}

else if (y_pos < radius)
{

y_speed = +1;
}


y_pos += y_speed;


repaint();

try
{

Thread.sleep (20);
}
catch (InterruptedException ex)
{

}


Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}

public void update(Graphics gr)
{
paint(gr);
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}


dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);


dbg.setColor (getForeground());
paint (dbg);

gr.drawImage (dbImage, 0, 0, this);
}

public void paint(Graphics gr)
{
if(firstTime)
{
gr.drawString("Yo - Yos for Sale!!!", 70,10);
gr.drawOval(80,30,80,80);
gr.drawLine(120,110,120,210);
gr.drawLine(120,210,70,310);
gr.drawLine(120,210,170,310);
gr.drawLine(70,140,120,140);
gr.drawLine(120,140,170,140);

firstTime = true;
}
gr.setColor(Color.blue);
gr.setColor(getBackground());
gr.drawLine(70,140,oldx,oldy);
gr.setColor(Color.green);
gr.setColor(getBackground());
gr.drawLine(170,140,oldxx,oldyy);

gr.setColor(Color.blue);
gr.setColor(getForeground());
gr.setColor(Color.blue);
gr.drawLine(70,140,horiz[index],vert[index]);
gr.setColor(Color.green);
gr.drawLine(170,140,horiz1[index],vert1[index]);



oldx = horiz[index];
oldy = vert[index];

oldxx = horiz1[index];
oldyy = vert1[index];

++index;
if(index == horiz.length)
index = 0;

gr.setColor (Color.red);

gr.fillOval (x_pos - radius, y_pos + radius, 2 * radius, 2 * radius);
try
{
Thread.sleep(sleep);
}
catch(InterruptedException e)
{
}
repaint();
}
}
oluwaseun akinmayowa
Greenhorn

Joined: Apr 04, 2011
Posts: 6

Deepak Bala wrote:I am moving this to 'Java in general'.

There are a couple of ways you could do this, starting with the technology in question. Java swing / HTML + JS / Applets etc etc.

What have you done so far ?


THIS IS WHAT I HAVE DONE BELLOW. I WANT IT TO FORM YO-YO.



import java.applet.*;
import java.awt.*;
public class Project6 extends Applet implements Runnable
{
private int index = 0;
int[] horiz = {70,70,70,70,};
int[] vert = {140,200,210,220};


int[] horiz1 = {170,170,170,170};
int[] vert1 = {140,200,210,220};

int oldx = horiz[0], oldy = vert[0];
int oldxx = horiz1[0], oldyy = vert1[0];

int x_pos = 100;
int y_pos = 100;


int y_speed = 1;

int radius = 10;


int appletsize_x = 200;
int appletsize_y = 100;

boolean firstTime = true;


private int sleep = 200;

private Image dbImage;
private Graphics dbg;


public void start()
{
index = 0;

Thread th = new Thread (this);

th.start ();
}

public void run ()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);


while (true)
{
if (y_pos > appletsize_y + radius || (x_pos < 0))
{

y_speed = -1;
}

else if (y_pos < radius)
{

y_speed = +1;
}


y_pos += y_speed;


repaint();

try
{

Thread.sleep (20);
}
catch (InterruptedException ex)
{

}


Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}

public void update(Graphics gr)
{
paint(gr);
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}


dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);


dbg.setColor (getForeground());
paint (dbg);

gr.drawImage (dbImage, 0, 0, this);
}

public void paint(Graphics gr)
{
if(firstTime)
{
gr.drawString("Yo - Yos for Sale!!!", 70,10);
gr.drawOval(80,30,80,80);
gr.drawLine(120,110,120,210);
gr.drawLine(120,210,70,310);
gr.drawLine(120,210,170,310);
gr.drawLine(70,140,120,140);
gr.drawLine(120,140,170,140);

firstTime = true;
}
gr.setColor(Color.blue);
gr.setColor(getBackground());
gr.drawLine(70,140,oldx,oldy);
gr.setColor(Color.green);
gr.setColor(getBackground());
gr.drawLine(170,140,oldxx,oldyy);

gr.setColor(Color.blue);
gr.setColor(getForeground());
gr.setColor(Color.blue);
gr.drawLine(70,140,horiz[index],vert[index]);
gr.setColor(Color.green);
gr.drawLine(170,140,horiz1[index],vert1[index]);



oldx = horiz[index];
oldy = vert[index];

oldxx = horiz1[index];
oldyy = vert1[index];

++index;
if(index == horiz.length)
index = 0;

gr.setColor (Color.red);

gr.fillOval (x_pos - radius, y_pos + radius, 2 * radius, 2 * radius);
try
{
Thread.sleep(sleep);
}
catch(InterruptedException e)
{
}
repaint();
}
}


oluwaseun akinmayowa
Greenhorn

Joined: Apr 04, 2011
Posts: 6

Jeanne Boyarsky wrote:What technology are you using? Swing?


Yes. Below is what I did so far, I want to draw a man with yo-yo in both hand ant different speed:
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

No need to post all that code three times. There is an button in the right-hand corner of your post, if you made a mistake you can click that to edit your post. When you use code tags, your code will be nicely formatted to make it more readable on screen.

It looks like you have the basics working already. Can you explain exactly what part you now have a question about?

A small remark about your code: Never "eat" exceptions like you're doing in lines 153 - 155. Always at least print the stack trace inside a catch-block, otherwise you'll never know that an exception occurred:

You are calling Thread.sleep() in the paint() method in line 151. That will probably make your program not respond the the UI. The paint() method is running on Swing's event dispatch thread, and you should not block that thread by making it go to sleep - if you do that, Swing cannot handle other UI tasks to make it respond to the UI. Put the Thread.sleep() and repaint() calls in another thread, not inside the paint() method.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
oluwaseun akinmayowa
Greenhorn

Joined: Apr 04, 2011
Posts: 6

Jesper de Jong wrote:No need to post all that code three times. There is an button in the right-hand corner of your post, if you made a mistake you can click that to edit your post. When you use code tags, your code will be nicely formatted to make it more readable on screen.

It looks like you have the basics working already. Can you explain exactly what part you now have a question about?

A small remark about your code: Never "eat" exceptions like you're doing in lines 153 - 155. Always at least print the stack trace inside a catch-block, otherwise you'll never know that an exception occurred:

You are calling Thread.sleep() in the paint() method in line 151. That will probably make your program not respond the the UI. The paint() method is running on Swing's event dispatch thread, and you should not block that thread by making it go to sleep - if you do that, Swing cannot handle other UI tasks to make it respond to the UI. Put the Thread.sleep() and repaint() calls in another thread, not inside the paint() method.


I never get how the program need to look like. I tried to attach the picture of the animation, but it didn't attach. Do you have any e-mail I can send the attachment to for you to see?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: bouncing ball