加public void paint(Graphics g){}

解决方案 »

  1.   

    加paint方法后,里面怎么写。
      

  2.   

    请问paint方法里怎么写,我不可能把鼠标移动过的位置都存起来,让paint方法来重画阿???
      

  3.   

    直接在paint里面画就得啦, help 里面有现成的。
      

  4.   

    我认为还是要将直线的两个端点保存起来,比如生成一个 Point对象,然后将各条
    直线的 Point对象保存在一个 Vector中(如:lineVector),在你的程序中加这样一个方法:
    public void paintComponent(Graphics g)
    {
          super.paintComponent(g);
          drawLine(g);
    }
    drawLine(g)实现画直线,可以这样写:
    Enumeration temp=lineVector.elements();
    while(temp.hasMoreElements())
    {
             Point p1=(Point)temp.nextElement();
             Point p2=(Point)temp.nextElement();
             g.drawLine(p1.x,p1.y,p2.x,p2.y);
    }
    你试试行不行
      

  5.   

    rongrongGsr() 我用你的方法试了一下,好像是可以的不过就是其中有一些小问题,我画的不是一条线,而是几条,在判断鼠标抬起是就不好处理。
     还有这样把所有的点都保存下来,是不是有一点耗资源,
     有没有什么方法,可以在程序调用paint时,先把图形保存起来,下一次重画时就直接把次图形repaint()一下。
    谢谢。
      

  6.   

    e_snow(呆呆) 这位朋友,你说help里有,我找不到,我用的时jb5。请详细一点好吗?谢谢
      

  7.   

    在内存里建一个与你的frame完全相同尺寸的image对象,然后在其上画就行了,然后在你的frame的paint事件里重画image对象就OK了.
      

  8.   

    谢谢haichuang,用你的方法我在Applet里面成功那,但是在Frame里面却报错
    java.lang.NullPointerException
    不知为什么???
      

  9.   

    我的意思是,在sample里面找个类似的source,可以借鉴一下!
      

  10.   

    贴出你的新源码,这个提示是说什么对象是null,你可能有地方写的有点错误.
      

  11.   

    public class Frame1 extends JFrame implements MouseListener,MouseMotionListener{
      int x1,y1,x2,y2;
      Canvas c=new Canvas();
      Image image=c.createImage(400,300);
      Graphics gg=image.getGraphics();
      public Frame1(){
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }}
      private void jbInit() throws Exception {    this.getContentPane().setLayout(xYLayout1);
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
        this.addMouseListener(new java.awt.event.MouseAdapter() {
          public void mouseExited(MouseEvent e) {
            this_mouseExited(e);
          }
        });
      }
      public void paint(Graphics g)
      {
        g.drawImage(image,0,0,this);
      }
      public void mouseMoved(MouseEvent evt)
      {
      }
      public void mouseDragged(MouseEvent evt)
      {
        x2=evt.getX();
        y2=evt.getY();
        gg.drawLine(x1,y1,x2,y2);
        x1=x2;
        y1=y2;
      }
      public void mousePressed(MouseEvent evt)
      {
        x1=evt.getX();
        y1=evt.getY();
      }
      public void mouseReleased(MouseEvent evt)
      {
      }
      public void mouseExited(MouseEvent evt)
      {}
      public void mouseEntered(MouseEvent evt)
      {}
      public void mouseClicked(MouseEvent evt)
      {
      }
      XYLayout xYLayout1 = new XYLayout();
      void this_mouseExited(MouseEvent e) {
      }
    public static void main(String[] args)
    {
      Frame1 frame=new Frame1();
      frame.setSize(400,300);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if (frameSize.height > screenSize.height)
          frameSize.height = screenSize.height;
        if (frameSize.width > screenSize.width)
          frameSize.width = screenSize.width;
        frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);    frame.setVisible(true);}  }
      

  12.   

    我误打误创的成功了。下面死我的代码:
    import java.awt.*;
    import javax.swing.*;
    import com.borland.jbcl.layout.*;
    import java.awt.event.*;
    /**
     * Title:
     * Description:
     * Copyright:    Copyright (c) 2003
     * Company:
     * @author
     * @version 1.0
     */public class Frame1 extends JFrame implements MouseListener,MouseMotionListener{
      int x1,y1,x2,y2;
      boolean flag=false;
      Canvas c=new Canvas();
      Image image=null;
      Graphics gg=null;
      FlowLayout flowLayout1 = new FlowLayout();
      public Frame1(){
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }}
      private void jbInit() throws Exception {
        this.getContentPane().setLayout(flowLayout1);
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
      }
     public void paint(Graphics g)
      {
        if(flag)
        g.drawImage(image,0,0,this);
      }
      public void mouseMoved(MouseEvent evt)
      {
      }
      public void mouseDragged(MouseEvent evt)
      {
        x2=evt.getX();
        y2=evt.getY();
        gg.drawLine(x1,y1,x2,y2);
        x1=x2;
        y1=y2;
        this.repaint();
      }
      public void mousePressed(MouseEvent evt)
      {
        flag=true;
        x1=evt.getX();
        y1=evt.getY();
        image=createImage(400,300);
        gg=image.getGraphics();
      }
      public void mouseReleased(MouseEvent evt)
      {
      }
      public void mouseExited(MouseEvent evt)
      {}
      public void mouseEntered(MouseEvent evt)
      {}
      public void mouseClicked(MouseEvent evt)
      {
      }
      void this_mouseExited(MouseEvent e) {
      }
      public void update(Graphics g)
      {
        paint(g);
      }
    public static void main(String[] args)
    {
      Frame1 frame=new Frame1();
      frame.setSize(400,300);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if (frameSize.height > screenSize.height)
          frameSize.height = screenSize.height;
        if (frameSize.width > screenSize.width)
          frameSize.width = screenSize.width;
        frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);    frame.setVisible(true);}  
    }
    程序中有什么需要改进的,请各位朋友不吝赐教,谢谢,我明天来结贴,谢谢
      

  13.   

    你不应该在MousePressed事件中建立Image对象,应该在初始化的时候就建立.
      

  14.   

    如果你只是想将文件保存成如 jpg等格式的图片,用以上的方法的确是不错的,但是如果你要在下次用你的程序打开你上次画的图形文件并且要继续编辑修改的话,我认为还是要设一些数组来保存你画的点、矩形、椭园等。这点资源对计算机来说并不算什么,我是这样认为的。
      

  15.   

    rongrongGsr()朋友说得问题正好是我又想解决的,怎样才能把已经化好的image保存出来,这已是另一个问题,希望大家有什么好方法告诉我。谢谢。
      

  16.   

    保存图片,处理图片可以使用sun的jai包.
    http://java.sun.com/products/java-media/jai/