aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes function like cleardevice() in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "function like cleardevice() in java" Watch "function like cleardevice() in java" New topic
Author

function like cleardevice() in java

Gursewak Singh
Ranch Hand

Joined: Jan 09, 2011
Posts: 79

i want to know the function in java which work like cleardevice().......

basically i want to move a 2-D object circle using for loop and tread,and
i want that this circle seems like a moving circle,so i want to clear it time to time in for loop.......
so suggest me for that......
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

You simply need to override the paintComponent() function of whatever component you want to draw on. Then, in that function, you can draw the circle at the location it should be at any given time.

Finally all you have to do is make a timer that calls the repaint() function of that component periodically.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
To add to what SvH said, start your method like thisI think I have spelt it correctly.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2171
    
    7
basically i want to move a 2-D object circle using for loop and tread,and


If you want animation then you should use a Swing Timer to schedule the movement of the circle.
Gursewak Singh
Ranch Hand

Joined: Jan 09, 2011
Posts: 79


thanks SvH and Ritchie ......
but i am not getting your idea......
actually i have not studied 2D in detail,so i don't know much about it.
so you can check my code so that you can get in better way what i want..

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class DrawTest extends Frame implements Runnable
{
int a=0,b=0;

//Shape circle = new Ellipse2D.Float(100.0f, 100.0f, 100.0f, 100.0f);
// Tread t;
//t=new Tread(this);
//t.start();
public void run()
{


}

public void paint(Graphics g)
{
Graphics2D ga = (Graphics2D)g;
// ga.draw(circle);
// ga.setPaint(Color.green);
// ga.fill(circle);
ga.setPaint(Color.red);
for(int i=500;i>0;i--)
{
try
{
Thread.sleep(10);
Shape square = new Rectangle2D.Double(50+a, 50,50+b, 50);
//setVisible(false);
ga.draw(square);
// repaint();


a++;
b++;


}
catch(Exception r)
{}

}

}

public static void main(String args[]) {
Frame frame = new DrawTest();
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
frame.setSize(500, 550);
frame.setVisible(true);
}
}


now you can run this code ......
and check output.
but i want that it look like a moving rectangle.not a mixed pattern.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
Gursewak Singh wrote:
thanks SvH and Ritchie ......
You're welcome

but i am not getting your idea......
Please explain what you don't understand; I thought SvH's post and mine were clear

actually i have not studied 2D in detail,so i don't know much about it.[
Have you looked in the Java™ Tutorials or anywhere?

so you can check my code so that you can get in better way what i want..
  • Why are you using AWT rather than Swing?
  • Don't expect repainting to work after 10 ms; try a longer delay.
  • Get rid of all the commented-out code, which makes it difficult to read.
  • Why are you not using the Event Dispatch Thread?
  • why have you got an empty run() method?
  • Why have you got an empty catch block? that is particularly bad if you have code whose correctness you doubt.
  • why are you using paint, not paintComponent?
  • I think you need to change to Swing, to put your code in the right method, to use the EDT and fill in those empty space. Then you will have a chance of understanding what is going on.
    Stephan van Hulst
    Bartender

    Joined: Sep 20, 2010
    Posts: 3611
        
      14

    Yep. Read the Oracle tutorials on Swing first. Then, when you get stuck, ask us again.
    Gursewak Singh
    Ranch Hand

    Joined: Jan 09, 2011
    Posts: 79

    OK Ritchie......
    thanks again for suggestion.
    i will will try with these suggestions......
    And
    if I again face any problem then I will ask again......
    so thank you again
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38334
        
      23
    You're welcome . . . and when you face the enxt problem, we'll know what causes it and we'll be better able to help.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: function like cleardevice() in java