generally you will override the paint method of a component. if yuou want to manaually draw something, you can get graphics objecct from canvas.

解决方案 »

  1.   

    一般来说,你可以不用实例化 Graphics, 直接 override 背景对象的 paint 方法 如class MyFrame extends Frame
    {
        ......................    public void paint(Graphics g)
        {
        super.paint(g);
                      g.drawLine( 1,2,3,4);
       }
    }
      

  2.   

    我就没有理解,为什么paint()方法被自动执行了呢?
      

  3.   

    或者这么说吧,我想自己写一个画点的函数,因为java里找不到,只好用drawLine来代替,把始末坐标设为一致。我应该怎么做?谢谢!
      

  4.   

    if you want to draw a point, its better you fill a cirle with small diameter.
      

  5.   

    但是由于我这个点是精确到象素的,所以用画圆和画直线的方法应该是一样的吧?不管用什么方法,我现在只想知道怎么才能使用Graphics类中提供的draw方法draw(new Line.2D(x,y,x,y));没有Graphics的示例我不可能调用这个啊? 而且draw的定义也是抽象的方法……请求帮忙!谢谢
      

  6.   

    你想在什么地方画点呢?
    Frame, Applet, Panel ...... 等等,都可以用    public void paint(Graphics g)
        {
        super.paint(g);
                      g.drawLine( x, y, x, y);
       }
    来画啊
      

  7.   

    我需要传入一个参数来画点,比如我需要在x,y画一个点,这个x,y又是根据用户操作而变化的,我不可能用paint方法来做吧?其次我想知道,paint(Graphics g)是怎么被调用的呢?这个参数g是哪里来的?
      

  8.   

    paint() 会自动被执行的~~  g 是所需的Graphics的对象。
    可以参考下这样public class MyFrame extends  Frame
    {
      int x, y;
      MyFrame ()
      {
        setLayout(null);
        setSize (500,400);
        setBackground(Color.white );
      }  public void paint(Graphics g)
      {
          super.paint(g);
          g.setColor(Color.black);
          getCoordinates();
          g.drawLine(x,y,x,y);
      }  public void getCoordinates()
      {
        // 在这里改变x,y 坐标。 比如:
        x=100;
        y=200;
      }  public static void main(String[] args)
      {
        MyFrame test = new MyFrame();
        test.show();
      }
    }
      

  9.   

    Graphics不能被抽象化是为了能够实现跨平台。
    具体对应每个系统,java自己会实现的吧
    我前几天也做图形。
    我尝试着声明一个类的Graphics属性。之后在paint方法中赋植。但是我做没成功
    Graphics gr;
    public void paint(Graphics g)
    {
         gr = g;
    }
    我认为,paint 方法系统会自己调用,应该在用gr之前应该可以复制了吧。另外我看书,书上说,没种组件都有一个getGraphics方法,可以得到句柄。不过我还没有尝试。:)
      

  10.   

    我尝试了一下,两种都可以。
    import java.awt.*;
    public class MyFrame extends Frame 
    {
    private Graphics gr;
    public MyFrame()
    {
    super("Test");
    } public boolean handleEvent(Event e)
    {
    if(e.id == Event.WINDOW_DESTROY)
    {
    hide();
    dispose();
    System.exit(0); } return super.handleEvent(e);
    } public void paint(Graphics g)
    {
    gr = g.create();
    g.drawString("Hello", 200, 100);
    } public void draw(int x)
    {
    gr.drawString("Test", x, x);
    }
    public static void main(String[] args) 
    {
    MyFrame frame = new MyFrame();
    frame.resize(500, 500);
    frame.show();
    frame.draw(200);
    }
    }
    另外一种
    import java.awt.*;public class MyFrame extends Frame 
    {
    private Graphics gr;
    public MyFrame()
    {
    super("Test");
    } public boolean handleEvent(Event e)
    {
    if(e.id == Event.WINDOW_DESTROY)
    {
    hide();
    dispose();
    System.exit(0); } return super.handleEvent(e);
    } public void paint(Graphics g)
    {
    //gr = g.create();
    g.drawString("Hello", 200, 100);
    } public void draw(int x)
    {
    gr = getGraphics();
    if(gr == null)
    System.out.println("Error");
    gr.drawString("Test", x, x);
    }
    public static void main(String[] args) 
    {
    MyFrame frame = new MyFrame();
    frame.resize(500, 500);
    frame.show();
    frame.draw(200);
    }
    }
    还有一些问题,当窗口重划等,还得重载repaint()方法。还有避免空指针问题等等。
      

  11.   

    谢谢owe朋友帮忙 我也尝试了getGraphics方法,但是获取Graphics实例的时候会出现null返回值,在Document中的解释说,当Component对象Displayable时,才能通过getGraphics方法获得一个实例,我不明白,什么时候才算Displayable呢?