• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

bouncing ball

 
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help to draw two oval, bouncing in different speed.
 
Marshal
Posts: 70248
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please show us what you have got already.

I think your discussion will be moved to a different forum.
 
author & internet detective
Posts: 40198
816
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What technology are you using? Swing?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
oluwaseun akinmayowa
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
oluwaseun akinmayowa
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic