oluwaseun akinmayowa

Greenhorn
+ Follow
since Apr 04, 2011
oluwaseun likes ...
Java
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by oluwaseun akinmayowa

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?
12 years ago

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:
12 years ago

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();
}
}


12 years ago

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();
}
}
12 years ago
I need help to draw two oval, bouncing in different speed.
12 years ago