Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes help with oval Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "help with oval" Watch "help with oval" New topic
Author

help with oval

melkor leprichaun
Greenhorn

Joined: Oct 31, 2003
Posts: 3
i am trying to get the oval to increase in size but i can only get one side to grow what am i doing wrong please help here is my code

mport java.awt.Graphics;
import java.awt.Color;
public class GoRect extends java.applet.Applet implements Runnable
{
Thread mythread;
int width;
int height;


public void update(Graphics g)
{
paint(g);
}

public void start()
{
if (mythread==null)
{
mythread=new Thread(this);
mythread.start();
}
}
public void run()
{
height=70;
width=180;

while(true)
{
if (width>size().width)
if (height>size().height)

{
width=1;
height=1;

}
height=height-1;
width=width+1;

repaint();
try {Thread.sleep(50);}
catch (Exception e) { }
}
}
public void stop()
{
if (mythread!=null)
{
mythread.stop();
mythread=null;
}
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(90,height,width,180);

}
}
[ November 03, 2003: Message edited by: melkor leprichaun ]
[ November 03, 2003: Message edited by: melkor leprichaun ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
melkor leprichaun,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Swing / JFC / AWT forum...
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

The parameters to the fillOval() method are x, y, width, and height, in order. In your code you call it like this -


Here is what this means... The oval is drawn it is drawn 90 pixels from the left side of the applet. The y location is represented by the variable named 'height'. 'height' starts at 70 and decreaes, so the oval starts 70 pixels from the top of the screen and moves up the screen. Width is represented by the variable named 'width', which starts at 180 and increases, so the width of the oval will increase to the right. The height of the oval stays at 180 pixels. The reason the oval seems to be 'increasing' on one side is because you overrode update() to not clear the screen before doing a new paint() call... this isn't good to do without adding double buffering because the draw operations aren't persistant. If anything moves over the applet then it will clear the area that was covered and what you think is being drawn will get messed up.

To actually make the oval increase on all sides around a center point you will need to keep track of both x and y values in addition to width and height. Whenever you increase width you will need to decrease x by half of that amount, and whenever you increase height you will need to decrease y by half of that amount. Since you are using integers and increasing by one, you won't see any change because you can't decrease an integer value by 0.5. You'll need to use decimal values (floats or doubles) to keep track of the x, y, width and height values and use Math.round() to cast them to integers to paint the oval.

Here's your code with the changes (plus using backbuffer, and a main method to test it out without AppletViewer, plus fixes for some of the deprecated stuff you were using... Thread.stop() and size() calls (though you may be using these to stay compatible with JDK 1.1) ) -



-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
I also made this work. I'm not sure how, but I was playing around with it. Thanks for the challenge!


David Crossett
-nothing important to say, but learnin' plenty-
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with oval