5 Replies - 261 Views - Last Post: 06 October 2012 - 12:14 PM Rate Topic: -----

#1 curlyfries1999  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 22-December 11

DrawOval making a smudge

Posted 06 October 2012 - 09:54 AM

Hello. I am making a program that makes a pattern with moving circles at different speeds. I have made a circle but it just smudges as it moves. This is my code:
package org.CurlyFries.HelloWorld;

import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JApplet;

public class HelloWorld extends JApplet {

	final int radius = 40;
	int x = 0;
	int y = 65;

	public final int width = 100 + (radius / 2);
	public final int height = 100 + (radius / 2);

	public void paint(Graphics gr) {
		setBackground(Color.WHITE);
		gr.setColor(Color.red);
		while (true) {
			gr.fillOval(x, y, radius, radius);
			x++;

			try {
				Thread.sleep(50);
			} catch (Exception e) {
			}
			;
		}
	}
}



Any help is appreciated!
Thanks in advance :D

Is This A Good Question/Topic? 0
  • +

Replies To: DrawOval making a smudge

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: DrawOval making a smudge

Posted 06 October 2012 - 10:26 AM

First off you do NOT need the endless while loop. The reason why the ball wont move when you don't have it is because you did not call repaint();

I'll try to work on a solution. This kind of stuff is my weak point.

This post has been edited by Kinaces: 06 October 2012 - 10:40 AM

Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1408
  • View blog
  • Posts: 3,123
  • Joined: 05-April 11

Re: DrawOval making a smudge

Posted 06 October 2012 - 10:41 AM

Ouch a while(true) loop in your event dispatch thread.

You do not want to be doing any sleep in your paint method
The paint method should return as quick as possible

Save your ovals in an array, and paint them all up again in your paint method
It is not the paint method's job to be adding new ovals, it should only paint the existing ones

Use a swing timer to update and repaint :)
Was This Post Helpful? 0
  • +
  • -

#4 curlyfries1999  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 22-December 11

Re: DrawOval making a smudge

Posted 06 October 2012 - 11:09 AM

View PostCasiOo, on 06 October 2012 - 10:41 AM, said:

Ouch a while(true) loop in your event dispatch thread.

You do not want to be doing any sleep in your paint method
The paint method should return as quick as possible

Save your ovals in an array, and paint them all up again in your paint method
It is not the paint method's job to be adding new ovals, it should only paint the existing ones

Use a swing timer to update and repaint :)

Thanks. I get what you mean but I am new to Java so I am not too sure on how to do a lot of that
Was This Post Helpful? 0
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 858
  • View blog
  • Posts: 2,612
  • Joined: 29-July 11

Re: DrawOval making a smudge

Posted 06 October 2012 - 12:08 PM

In addition to the other problems the others mentioned, your problem is a simple one, basic to animation. Imagine how your ball moves across the screen - you draw the ball in one place, wait a set period, then draw it further along. Your problem is that you're not erasing the ball in the previous position, so it creates a smudge as you call it as it moves. You do that by redrawing an empty background every iteration of your paint loop.
public void paint(Graphics g) {

    g.setBackground(Color.white);
    g.setColor(getBackground());
    g.fillRect(0, 0,  appletWidth, appletHeight); //redraw the background.
    g.setColor(Color.red);
    g.fillOval(x, y, radius, radius);
}


Was This Post Helpful? 1
  • +
  • -

#6 curlyfries1999  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 22-December 11

Re: DrawOval making a smudge

Posted 06 October 2012 - 12:14 PM

View Postfarrell2k, on 06 October 2012 - 12:08 PM, said:

In addition to the other problems the others mentioned, your problem is a simple one, basic to animation. Imagine how your ball moves across the screen - you draw the ball in one place, wait a set period, then draw it further along. Your problem is that you're not erasing the ball in the previous position, so it creates a smudge as you call it as it moves. You do that by redrawing an empty background every iteration of your paint loop.
public void paint(Graphics g) {

    g.setBackground(Color.white);
    g.setColor(getBackground());
    g.fillRect(0, 0,  appletWidth, appletHeight); //redraw the background.
    g.setColor(Color.red);
    g.fillOval(x, y, radius, radius);
}



Thank you very much. It worked!
:D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1