0
votes

Why can not I see the following output?

g2.drawString(new Date().toString(), 0, 150);

(i used g2 (global variable) for using g in paint method in inner class).

Thanks a lot in advance!

    public class RedRect extends Frame
    {
public static void main(String[] args){ SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new RedRect(); } }); } public static Point p; RedRect() { super("RedRect"); try{ addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e){System.exit(0);}}); setSize (800, 600); add("Center", new CvRedRect()); show(); } catch(Exception ex){ } } } class CvRedRect extends Canvas { Vector<Point> v=new Vector<Point>(); CvRedRect(){ addMouseListener((new MouseAdapter() { public void mousePressed(MouseEvent evt){ if(v.size()<3){ v.add(evt.getPoint()); } else{ flag=false; //v.removeElementAt(0); //v.add(evt.getPoint()); } p=evt.getPoint(); repaint(); } })); } Point p=new Point(); boolean flag=true; int i=0; public static Graphics g2; public void paint(Graphics g) { try{ g2=g; Dimension d = getSize(); int maxX = d.width - 1, maxY = d.height - 1; g.setColor(Color.red); g.drawRect(0, 0, maxX, maxY); g.drawString("x ="+p.x, 10, 30); g.drawString("y = " +p.y, 10, 60); if(v.size()>2){ g2.drawLine(v.get(0).x,v.get(0).y,v.get(1).x,v.get(1).y); g2.drawLine(v.get(0).x,v.get(0).y,v.get(2).x,v.get(2).y); g2.drawLine(v.get(1).x,v.get(1).y,v.get(2).x,v.get(2).y); Thread t=new Thread(){ public void run(){ try{ while(flag){ ///The following comand////// g2.drawString(new Date().toString(), 0, 150); /////////////////////////// Thread.sleep(300); System.out.println (v.size()); Thread.sleep(300); } } catch(Exception ex){ System.out.println (ex.getMessage()); } } }; t.start(); } //System.out.println ("size="+v.size()); if(!flag){ g.clearRect(0,0,maxX,maxY); Thread.sleep(1000); g.drawString("FINISH", 5, 30); flag=true; } } catch(Exception ex){ } } }

1

1 Answers

3
votes

The reason your code isn't working is because you're using Graphics wrong. The Graphics object should not be a class field much less a static one. It is not a long-lived object, and so if you try to have it persist in this way, you'll end up with either a null reference or a non-null reference that doesn't work.

Instead if your program is an AWT program, then use the Graphics object inside of the paint method where the JVM supplies you with it when it calls paint. Otherwise if Swing do likewise with the paintComponent method.

Suggestions:

  • First and foremost, don't code AWT unless this is an absolute requirement for a class. Instead use Swing.
  • Next read the Swing graphics tutorials as they'll teach you all this and more.
  • Next do your drawing within the paintComponent(...) overload of a JPanel or other component that extends JComponent.
  • Don't forget to call the super.paintComponent(g) method within your override.
  • Don't forget to annotate your override with the @Override annotation.
  • Use a Swing Timer to do what you're currently using a while loop and Thread.sleep(...) to do.