• Post Reply Bookmark Topic Watch Topic
  • New Topic

Graphics2D

 
pankaj agarwal
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/*Hi! I want that after each line is drawn, it gets REMOVED before the transform is applied and the next line is drawn. repaint() won't work because it will run at its own will :-( I have made a call to setPaint(), and then fill() functions of Graphics2D object. Is this the right approach? I am confused...please help me!!! :-(*/

import java.awt.*;
import java.awt.geom.*;
public class MyFrame extends Frame{
MyFrame(){
setSize(400,400);
setVisible(true);
}
public static void main(String[]args){
new MyFrame();
}

public void paint(Graphics g){
Graphics2D g2D=(Graphics2D)g;
g2D.setStroke(new BasicStroke(3.0f));
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
Line2D.Double line=new Line2D.Double(0,0,0,50);
AffineTransform at=new AffineTransform();
at.translate(200,200);
for(int i=0; {
g2D.setPaint(Color.black);
at.rotate(Math.toRadians(6));
g2D.setTransform(at);
g2D.draw(line);
if((i+=6)==360)
i=0;
try{
Thread.sleep(1000);
}catch(InterruptedException e){}
g2D.setPaint(Color.white);
g2D.fillRect(0,0,getWidth(),getHeight());
}
}
}
 
Amit Agarwal
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi pankaj,
the other option you have is to redraw the same line before applying a new affine transform. this should be easy if you have just a single line. it will also improve the performance since only a very small area is affected.
also, you can avoid the for loop by using the timer class to generate repaint events for you at a specified interval. your paint method should simply erase the old line and draw a new one in this case.
-------
Amit
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!