B
Bryan R. Meyer
Hello All -
I am writing a program that puts HTML in a JEditorPane object which is
then placed in a JFrame object. I have a class (called TestCanvas)
which inherits from Canvas and which I use as the glasspane for the
JFrame. Using the mouse, I can draw lines on the glasspane. This
works beautifully. However, when the JFrame is not in focus or if I
set the glasspane to be invisible and then make it visible again, not
all of the markings that I made on the glasspane remain. Only the
last line I drew shows up when the JFrame is put back in focus or the
glasspane is set to be visible.
The code for the TestCanvas class is below. I suspect I have to save
the Graphics context. Perhaps to an offscreen image? My attempts at
this have been unsuccessful. I'd appreciate any insight. Thanks.
-Bryan
class TestCanvas extends JComponent implements
MouseListener,ActionListener {
boolean firstDraw = true;
JFrame f;
int x=0,y=0;
public TestCanvas(JFrame jfr) {
addMouseListener(this);
f = jfr;
}
public void paint(Graphics g) {
if(!firstDraw) {
g.setColor(Color.red);
g.drawLine(x+5,y+5,x+10,y+10);
System.out.println("x: " + x + " y: " + y);
}
} //End of paint method
public void repaint() {
System.out.println("repaint");
}
public void draw(int x1, int y1) {
x = x1;
y = y1;
paint(this.getGraphics().create());
}
public void actionPerformed(ActionEvent ae) {
this.setVisible(false);
}
public void mousePressed(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
Point cPPoint = SwingUtilities.convertPoint(this, e.getPoint(),
f.getContentPane());
Component cmp = SwingUtilities.getDeepestComponentAt(f.getContentPane(),(int)cPPoint.getX(),(int)cPPoint.getY());
String cmpName = cmp.getName();
if(cmpName==null) {
firstDraw = false;
this.draw(e.getX(),e.getY());
String xx = e.getX() + "";
String yy = e.getY() + "";
//System.out.println("mouse event! " + xx + " " + yy );
}
else {
this.setVisible(false);
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
} //End of HTMLCanvas class
I am writing a program that puts HTML in a JEditorPane object which is
then placed in a JFrame object. I have a class (called TestCanvas)
which inherits from Canvas and which I use as the glasspane for the
JFrame. Using the mouse, I can draw lines on the glasspane. This
works beautifully. However, when the JFrame is not in focus or if I
set the glasspane to be invisible and then make it visible again, not
all of the markings that I made on the glasspane remain. Only the
last line I drew shows up when the JFrame is put back in focus or the
glasspane is set to be visible.
The code for the TestCanvas class is below. I suspect I have to save
the Graphics context. Perhaps to an offscreen image? My attempts at
this have been unsuccessful. I'd appreciate any insight. Thanks.
-Bryan
class TestCanvas extends JComponent implements
MouseListener,ActionListener {
boolean firstDraw = true;
JFrame f;
int x=0,y=0;
public TestCanvas(JFrame jfr) {
addMouseListener(this);
f = jfr;
}
public void paint(Graphics g) {
if(!firstDraw) {
g.setColor(Color.red);
g.drawLine(x+5,y+5,x+10,y+10);
System.out.println("x: " + x + " y: " + y);
}
} //End of paint method
public void repaint() {
System.out.println("repaint");
}
public void draw(int x1, int y1) {
x = x1;
y = y1;
paint(this.getGraphics().create());
}
public void actionPerformed(ActionEvent ae) {
this.setVisible(false);
}
public void mousePressed(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
Point cPPoint = SwingUtilities.convertPoint(this, e.getPoint(),
f.getContentPane());
Component cmp = SwingUtilities.getDeepestComponentAt(f.getContentPane(),(int)cPPoint.getX(),(int)cPPoint.getY());
String cmpName = cmp.getName();
if(cmpName==null) {
firstDraw = false;
this.draw(e.getX(),e.getY());
String xx = e.getX() + "";
String yy = e.getY() + "";
//System.out.println("mouse event! " + xx + " " + yy );
}
else {
this.setVisible(false);
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
} //End of HTMLCanvas class