The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
July 2001

Advertisement

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:

Store the line co-ordinates in a vector

Posted by Kishori Sharan on July 16, 2001 at 10:47 PM

Hi
The only way to keep all lines on screen is we store all lines co-ordinates somewhere and we draw all lines in paint() method. The following Line.java file content does the same. I have added a Vector in Line class and now it also has an inner class which is used to represent a line co-ordinates. In paint() method first I am checking if the line current being drawn is in vector then don't add this line in vector else add the line in vector and then draw all lines. Note that I haven't overridden update method. This way you have all your lines even if you resize you frame or minimize and maximize it.
Thanks
Kishori
//////////// Line.java
import java.awt.*;
import java.awt.event.*;
import java.util.* ;
public class Line extends Frame implements MouseListener{
int x1=-1;
int x2=-1;
int y1=-1;
int y2=-1;

private Vector lines = new Vector ( ) ;

// Start of inner class
private class ALine {
int x1 ;
int x2 ;
int y1 ;
int y2 ;

public ALine ( int x1, int y1, int x2, int y2 ) {
this.x1 = x1 ;
this.x2 = x2 ;
this.y1 = y1 ;
this.y2 = y2 ;
}
public int getX1 ( ) {
return x1;
}
public int getY1 ( ) {
return y1;
}
public int getX2 ( ) {
return x2;
}
public int getY2 ( ) {
return y2;
}
public void draw ( Graphics g ) {
g.drawLine ( x1, y1, x2, y2 );
}

public boolean equals ( ALine al ) {
return ( x1 == al.getX1() && y1 == al.getY1() && x2 == getX2() && y2 == getY2() ) ;
}
}
// end of inner class


// this method will draw all lines
private void draw ( Graphics g ) {
Enumeration e = lines.elements() ;
ALine ll ;
while ( e.hasMoreElements ( ) ) {
ll = ( ALine )e.nextElement() ;
ll.draw ( g ) ;
}
}
public Line() {
setVisible(true);
setSize(200,200);
addMouseListener(this);
aa();
}

public void aa() {
x1=0;
x2=100;
for(int i =0; i < 100; i = i + 10 ){
y1 = i;
y2 = i;
repaint();
}
}

public void mousePressed (MouseEvent e) {
x1=e.getX();
y1=e.getY();
}

public void mouseReleased(MouseEvent e) {
x2=e.getX();
y2=e.getY();
repaint();
}

public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}


public void paint(Graphics g) {
System.out.println("paint : " + new Date ( ) );
// If the last line in vector stored is not the same being drawn then
// append this line in vector and then draw all lines

if ( lines.size ( ) > 0 ) {
ALine a2 = new ALine (x1, y1, x2, y2 ) ;
if ( !lines.contains( a2 ) ) {
lines.add ( a2 ) ;
}
}
else {
// First time there won't be any element
lines.add ( new ALine ( x1, y1, x2, y2 ) ) ;
}
// Draw all lines
this.draw ( g ) ;
}

public static void main(String[] args) {
Line l = new Line();
}
}





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us