Advertisement
|
This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
Draw llines and preserve them to see them later
Posted by Kishori Sharan on July 07, 2001 at 2:47 PM
Hi There are a few problems in your code. 1. When you call repaint() method from inside the loop in aa() method then all previously drawn lines will beersed in paint() method. This is why you see only one line. 2. In your for loop you are increasing counter by 100 ( i = i + 100 ). However, your canvas is only 200 in height. Therefore, you won't be able to see any lines except first two even if they are not erased. Solution: 1. Move your line drawing code from aa() method to paint() method. 2. Just call repaint() method from aa() which in turn will call paint() method , which will draw all your lines. 3. Change the loop counter increments so that all your lines will fit in 200 X 200 canvas. Or, increase the size of canvas to accomodate all your lines co-ordinates. I am attaching the modified code for your reference. Thanks Kishori /////////////// Test.java import java.awt.*; public class Test extends Canvas { Frame F = new Frame(); int x1,x2,y1,y2; public Test() { F.setSize(300,300); F.setVisible(true); setSize(200,200); setVisible(true); F.add(this); } public void aa() { repaint(); } public void paint(Graphics g) { x1=0; x2=100; for ( int i = 0 ; i < 100 ; i += 3 ) { y1 = i; y2 = i ; g.drawLine(x1,y1,x2,y2); } } public void update(Graphics g) { paint(g); } public static void main (String[] args) { Test t= new Test(); t.aa(); } }
Replies:
|