看了好多有关保存功能的代码还是不能把我这个程序完善,敬请各位高手帮下忙.
下面的程序只能执行画图操作,但是无法保存,能否提供保存画图的建议或者代码?谢谢了.package test;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */public class Frame1 extends JFrame {
  BorderLayout borderLayout1 = new BorderLayout();
  JMenuBar jMenuBar1 = new JMenuBar();
  JMenu jMenu1 = new JMenu();
  JMenuItem jMenuItem1 = new JMenuItem();
  JMenu jMenu2 = new JMenu();
  JMenuItem jMenuItem2 = new JMenuItem();
  JMenuItem jMenuItem3 = new JMenuItem();
  JMenuItem jMenuItem4 = new JMenuItem();
  JMenuItem jMenuItem5 = new JMenuItem();
  JPanel jPanel1 = new JPanel();  int X1, Y1;
  int X2, Y2;
  int nType;  public Frame1() {
    try {
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }  void jbInit() throws Exception {
    this.setForeground(Color.white);
    this.setJMenuBar(jMenuBar1);
    this.addMouseMotionListener(new Frame1_this_mouseMotionAdapter(this));
    this.getContentPane().setLayout(borderLayout1);
    jMenu1.setText("File");
    jMenuItem1.setText("Exit");
    jMenuItem1.addActionListener(new Frame1_jMenuItem1_actionAdapter(this));
    jMenu2.setText("Draw");
    jMenuItem2.setText("椭圆");
    jMenuItem2.addActionListener(new Frame1_jMenuItem2_actionAdapter(this));
    jMenuItem3.setText("矩形");
    jMenuItem3.addActionListener(new Frame1_jMenuItem3_actionAdapter(this));
    jMenuItem4.setText("直线");
    jMenuItem5.setText("随手画");
    borderLayout1.setHgap(0);
    jPanel1.setBackground(Color.lightGray);
    jPanel1.addMouseMotionListener(new Frame1_jPanel1_mouseMotionAdapter(this));
    jPanel1.addMouseListener(new Frame1_jPanel1_mouseAdapter(this));
    jMenuBar1.add(jMenu1);
    jMenuBar1.add(jMenu2);
    jMenu1.add(jMenuItem1);
    jMenu2.add(jMenuItem2);
    jMenu2.add(jMenuItem3);
    jMenu2.add(jMenuItem4);
    jMenu2.add(jMenuItem5);
    this.getContentPane().add(jPanel1, BorderLayout.CENTER);
  }  public static void main(String[] args) {
    Frame1 frame1 = new Frame1();
    frame1.setSize(500,400);
    frame1.show();
  }  void jMenuItem1_actionPerformed(ActionEvent e) {
    System.exit(0);
  }  void Draw(Graphics g, int x11, int Y12,int X21,int Y22,boolean bDraw)
  {
    Graphics2D g_2d=(Graphics2D)g;
    g_2d.setXORMode(Color.RED);
    g_2d.setColor(Color.blue);
    switch(nType)
    {
        case 1:
           g_2d.drawOval(X1,Y1,X2-X1,Y2-Y1);break;
        case 2:
          g_2d.drawRect(X1,Y1,X2-X1,Y2-Y1);break;
    }
    if(bDraw)
      g_2d.setPaintMode();
    X2=X21;Y2=Y22;
    switch(nType)
    {
        case 1:
           g_2d.drawOval(X1,Y1,X2-X1,Y2-Y1);break;
        case 2:
          g_2d.drawRect(X1,Y1,X2-X1,Y2-Y1);break;
    }
  }
  void jPanel1_mousePressed(MouseEvent e) {
    X1=X2=e.getX();
    Y1=Y2=e.getY();
    Draw(jPanel1.getGraphics(),X1,Y1,X2,Y2,false);
  }  void jPanel1_mouseReleased(MouseEvent e) {
    Draw(jPanel1.getGraphics(),X1,Y1,e.getX(),e.getY(),true);
  }  void this_mouseDragged(MouseEvent e) {
  }  void jMenuItem2_actionPerformed(ActionEvent e) {
    nType=1;
  }  void jMenuItem3_actionPerformed(ActionEvent e) {
    nType=2;
  }  void jPanel1_mouseDragged(MouseEvent e) {
    if(e.getModifiers ()==InputEvent.BUTTON1_MASK)
    Draw(jPanel1.getGraphics(),X1,Y1,e.getX(),e.getY(),false);
   }
}class Frame1_jMenuItem1_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;  Frame1_jMenuItem1_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jMenuItem1_actionPerformed(e);
  }
}class Frame1_jPanel1_mouseAdapter extends java.awt.event.MouseAdapter {
  Frame1 adaptee;  Frame1_jPanel1_mouseAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    adaptee.jPanel1_mousePressed(e);
  }
  public void mouseReleased(MouseEvent e) {
    adaptee.jPanel1_mouseReleased(e);
  }
}class Frame1_this_mouseMotionAdapter extends java.awt.event.MouseMotionAdapter {
  Frame1 adaptee;  Frame1_this_mouseMotionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void mouseDragged(MouseEvent e) {
    adaptee.this_mouseDragged(e);
  }
}class Frame1_jMenuItem2_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;  Frame1_jMenuItem2_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jMenuItem2_actionPerformed(e);
  }
}class Frame1_jMenuItem3_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;  Frame1_jMenuItem3_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jMenuItem3_actionPerformed(e);
  }
}class Frame1_jPanel1_mouseMotionAdapter extends java.awt.event.MouseMotionAdapter {
  Frame1 adaptee;  Frame1_jPanel1_mouseMotionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void mouseDragged(MouseEvent e) {
    adaptee.jPanel1_mouseDragged(e);
  }
}

解决方案 »

  1.   

    不是我说~~你连重画的功能都没有,更不要说保存了~~
    只要改变窗口大小,图形就没了~~你应该用存点Poly()的方法,或者存图像对象存入数组的方法来进行重画~~~~
    还有就是没有四面可画`~~只能用鼠标拖右下角画图~~~~`~~然后才可以做到保存图像~~~~~~
      

  2.   

    建议可以采用记录历史操作命令的方式来解决重画的问题。保存图像,可以看看helpall(was jl)的帖子:
    // Try this:
    // import com.sun.image.codec.jpeg.*;     Component component = panel;
         BufferedImage bi =(BufferedImage)component.createImage(component.getWidth(),component.getHeight());
         component.paint(bi.getGraphics());      BufferedOutputStream out =new BufferedOutputStream(new FileOutputStream("c:/test1.jpg"));
         JPEGImageEncoder encoder =  JPEGCodec.createJPEGEncoder(out);
         JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
         param.setQuality(1.0f, false);
         encoder.setJPEGEncodeParam(param);
         encoder.encode(bi);
         out.flush();
         out.close();// or
    Component c = panel;
    BufferedImage bi =(BufferedImage)c.createImage(c.getWidth(),c.getHeight());
    c.paint(bi.getGraphics()); 
    javax.imageio.ImageIO.write(bi,"jpg",new File("c:/x1.jpg"));
      

  3.   

    原贴地址
    http://community.csdn.net/Expert/topic/3983/3983574.xml?temp=.8125421