当前位置: 代码迷 >> Java相关 >> Graphics 急~~~~ 高手快来!!!
  详细解决方案

Graphics 急~~~~ 高手快来!!!

热度:714   发布时间:2012-01-30 12:43:24.0
Graphics 急~~~~ 高手快来!!!
小弟想请问一下 怎么样才能不在 paint(Graphics g)这个函数中画图!!!
我的意思就是说 我想画图,但是 不想只在这个函数中画图,


class lll extends Panel{  
    public lll(String as)
    {   
    }
     public void paint(Graphics g)
        {
         g.drawLine(10, 10, 400, 400);
        }
}

class ooo extends Thread{
     lll l;
     Graphics g;
     public ooo(lll l)
     {
         this.l=l;
         g= l.getGraphics();
     }
     public void run()
     {
         g.drawLine(300, 200, 400, 400);
     }
}
public class kkk extends Frame{
     lll as=new lll("123");
     ooo o=new ooo(as);
     public kkk()
     {
         show();
        
         add(as);
         o.start();
             addWindowListener(new WindowAdapter()
         {public void windowClosing(WindowEvent e)
                 {System.exit(0);
                 }
     });
     }
     public static void main(String args[])
       {  
         kkk chess=new kkk();
           }
}

为何第一句在paint中的执行了 ,但是 后面的  g.drawLine(300, 200, 400, 400);没有执行?
怎么样才能在paint 方法外也 画图?
搜索更多相关的解决方案: class  画图  public  

----------------解决方案--------------------------------------------------------
高手呢??  没人会吗??  急呀~~~~~~~~~~~~~~~~~
----------------解决方案--------------------------------------------------------
不是没有画!是画了的  但是画了过后立马有给 public void paint(Graphics g)
         {
          g.drawLine(10, 10, 400, 400);
         }
方法给覆盖了!
----------------解决方案--------------------------------------------------------
给你一个代码
import javax.swing.*;
import java.awt.*;
import java.util.concurrent.*;

class MyPanel extends JPanel
{
    private Point    Begin;
    private    Point    End;
   
    public MyPanel( )
    {
        Begin = new Point( 10, 10 );
        End = new Point( 400, 400 );
    }
    public void set( Point begin, Point end )
    {
        Begin = begin;
        End = end;
        paintComponent( this.getGraphics() );
    }
    public void paintComponent( Graphics g )
    {
        super.paintComponents( g );
        
        g.drawLine( Begin.x, Begin.y, End.x, End.y );
        this.repaint();
    }
}

class MyThread extends Thread
{
    private MyPanel     panel;
   
    public MyThread( MyPanel panel )
    {
        this.panel = panel;
    }
   
    public void run()
    {
        
        for( int i = 0; i < 400; i++ )
        {
            try
            {
                Thread.sleep( 100 );
            }
            catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            panel.set( new Point( i, i), new Point( 400 - i, 400 - i ));
        }
    }
}

public class MyFrame extends JFrame
{
    private MyPanel        panel;
    private MyThread    thread;
   
    public MyFrame()
    {
        panel = new MyPanel();
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        add( panel );
        setVisible( true );
        thread = new MyThread( panel );
        thread.start();
    }
   
    public static void main( String[] args )
    {
        MyFrame chess = new MyFrame();
        
    }
}
----------------解决方案--------------------------------------------------------
  相关解决方案