• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

function like cleardevice() in java

 
Ranch Hand
Posts: 79
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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......
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add to what SvH said, start your method like thisI think I have spelt it correctly.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 79
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
    Saloon Keeper
    Posts: 15510
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yep. Read the Oracle tutorials on Swing first. Then, when you get stuck, ask us again.
     
    Gursewak Singh
    Ranch Hand
    Posts: 79
    Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You're welcome . . . and when you face the enxt problem, we'll know what causes it and we'll be better able to help.
     
    He does not suffer fools gladly. But this tiny ad does:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic