public class NewJFrame extends javax.swing.JFrame {    /** Creates new form NewJFrame */
     static Timer t=new Timer(40,new time());
     static int sx=0,sy=0,ex=0,ey=0;
     static int sx1=0,sy1=0,ex1=0,ey1=0;
   
    public NewJFrame() {
        initComponents();
        Graphics g=this.getGraphics();
        g.drawLine(0,0,1000,1000);
        this.getGraphics().drawLine(0, 0, 100,100);
        this.getGraphics().drawRect(0,0,100,100);
        this.setResizable(false);
        this.setVisible(true);
    }

解决方案 »

  1.   

    我记得绘制一些东西的时候应该在paint(Grahpics g)或者paintComponent(Grahpics g)中的,你可以在这两个方法内得到Graphics。
      

  2.   

    @Override
    public void paint(Graphics g) {
    // TODO Auto-generated method stub
    super.paint(g);
    g.drawLine(10, 100, 90, 100);
    }
      

  3.   

    Graphics g=this.getGraphics();
    g.drawLine(0,0,1000,1000);
    this.getGraphics().drawLine(0, 0, 100,100);
    this.getGraphics().drawRect(0,0,100,100);
    this.setResizable(false);
    this.setVisible(true); 我在奇怪楼主在g.drawLine(0,0,1000,1000)怎么不会出现null错误,是不是楼主在initComponents()用了show()。如果楼主的程序能跑,应该在运行时,可以看到红色线条一闪而过。要想线条一直显示,你要按1楼和3楼的方法实现画线才行。
      

  4.   

    楼主你的g.drawLine(0,0,1000,1000)会报空指针异常了,,,画图要在paint方法中
      

  5.   

    GUI程序在运行过程中很容易触发repaint事件,所以应该覆盖void paint(Graphics g),重写一个方法,这样每次repaint事件发生时,但能依照paint方法中画的那样