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:

Paint method is not synchronized

Posted by Kishori Sharan on July 22, 2001 at 1:11 PM

Hi
Last time, I didn't pay attention for lines being drawn in loop. I am sorry for that. We were calling repaint() method in for loop. The repaint method calls paint method. However, the call to paint is not synchronized. In other words, you can say that the repaint method dosn't trigger the paint method rather it posts it. It means there is no guarantee that your paint() method will be executed at the moment you call repaint. This was causing the problem. In for loop, we were calling repaint method. By the time paint method was getting executed the for lop was getting finished. So, when paint method was getting called the last co-ordinayes stored in x1, y1, x2, y2 were drawn and the previous lines were not drawn and stored in vector at all. Now, what I am doing is before you call repaint from for loop I am callingh addLine method which stores the line co-ordinates in vector . This way your lines are always stores and at hwatever later time paint method is called all lines will be drawn. I am also calling addLine method from mouseReleased method. This solves the problem.
There is equals method in ALine class. This method is called whne we say line.contains ( a2 ). So, the contains method of vector will call the equals method of ALine class to determine whether two lines are same or not. I had made a mistake in previous version of equals. In, fact I intended to override the equals method of Object class. So, I should have given the method argument as Object 01 and not as ALine a1. This time it is ok. Because, we are checking that if a particular line is already there in vector, we need equals method in ALine class.
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
public 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 ( Object o1 ) {
if ( o1 == null ) {
return false ;
}

ALine temp = ( ALine ) o1 ;

if ( x1 == temp.getX1() && y1 == temp.getY1() &&
x2 == temp.getX2() && y2 == temp.getY2() ) {
return true ;
}
else {
return false ;
}
}

}
// 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 ) ;
}
}

// This method adds a line's co-ordinates if that line is not in vector
public void addLine ( ) {
ALine a2 = new ALine (x1, y1, x2, y2 ) ;
if ( !lines.contains( a2 ) ) {
lines.add ( a2 ) ;
}
}

public Line() {
setVisible(true);
setSize(400,400);
addMouseListener(this);
aa();
}

public void aa() {
x1=0;
x2=100;
for(int i = 30; i <= 300; i = i + 30 ){
y1 = i;
y2 = i;
// First add the line in vector and then try repaint the screen
this.addLine ( ) ;
repaint();
}
}

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

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

// First add the line and then repaint
this.addLine ( ) ;
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 ( ) );

// Draw all lines
this.draw ( g ) ;
}

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





Replies:
  • Thanks Theesan July 26, 2001 at 1:09 AM (0)

Sponsored Links



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