`

Applet画图程序

阅读更多

双击右键才显示功能菜单。。。

 

 import java.applet.Applet;
 import java.awt.BasicStroke;
 import java.awt.Button;
 import java.awt.Canvas;
 import java.awt.Choice;
 import java.awt.Color;
 import java.awt.Container;
 import java.awt.Cursor;
 import java.awt.Dimension;
 import java.awt.FileDialog;
 import java.awt.FlowLayout;
 import java.awt.Frame;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.Image;
 import java.awt.Label;
 import java.awt.MediaTracker;
 import java.awt.MenuItem;
 import java.awt.Panel;
 import java.awt.PopupMenu;
 import java.awt.Toolkit;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.ItemEvent;
 import java.awt.event.ItemListener;
 import java.awt.event.KeyAdapter;
 import java.awt.event.KeyEvent;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.awt.event.MouseMotionAdapter;
 import java.awt.event.MouseMotionListener;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 import java.awt.geom.Ellipse2D;
 import java.awt.geom.Ellipse2D.Double;
 import java.awt.geom.Line2D;
 import java.awt.geom.Rectangle2D;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.PrintStream;
 import java.io.Serializable;
 import java.util.Vector;
 import javax.imageio.ImageIO;
 import javax.swing.JColorChooser;
 import javax.swing.JPanel;
 
 public class DrawImage extends Applet
   implements ItemListener, MouseListener, MouseMotionListener, ActionListener
 {
   private static final long serialVersionUID = 1L;
   int count = 6;
   Vector v = new Vector();
   Image[] img = new Image[this.count];
 
   int myWidth = 0; int myHeight = 0;
   int widthcount = 0;
 
   boolean[] b = new boolean[this.count];
   int[] width = new int[this.count]; int[] height = new int[this.count]; int[] w = new int[this.count];
   int[] h = new int[this.count]; int[] wid = new int[this.count]; int[] hei = new int[this.count];
   int[] ww = new int[this.count]; int[] hh = new int[this.count];
 
   int startX = 0; int startY = 0; int endX = 0; int endY = 0;
   Canvas startObj = null; Canvas endObj = null;
   String drawclear = "";
   int clickX = 0; int clickY = 0; int moveX = 0; int moveY = 0;
 
   DrawImage.canvas[] can = null;
   DrawImage.myCanvas[] mycan = new DrawImage.myCanvas[this.count];
   Canvas curcan = null;
   Canvas curdelcan = null;
   Image curimg = null;
 
   JPanel jp = new JPanel(null);
   MediaTracker mt = new MediaTracker(this);
 
   PopupMenu popMenu2 = new PopupMenu();
   MenuItem menuItem1 = null;
   MenuItem menuItem2 = null;
   MenuItem menuItem3 = null;
   MenuItem menuItem4 = null;
   MenuItem menuItem5 = null;
 
   int x = -1; int y = -1;
   int con = 1;
   int Econ = 5;
 
   int toolFlag = 0;
 
   Color c = new Color(0, 0, 0);
 
   BasicStroke size = new BasicStroke(this.con, 0, 
     2);
   DrawImage.Point cutflag = new DrawImage.Point(-1, -1, this.c, 6, this.con);
 
   Vector paintInfo = null;
   int n = 1;
 
   FileInputStream picIn = null;
   FileOutputStream picOut = null;
 
   ObjectInputStream VIn = null;
   ObjectOutputStream VOut = null;
   Panel toolPanel;
   Button eraser;
   Button drLine;
   Button drCircle;
   Button drRect;
   Button clear;
   Button pen;
   Choice ColChoice;
   Choice SizeChoice;
   Choice EraserChoice;
   Button colchooser;
   Label 颜色;
   Label 大小B;
   Label 大小E;
   Button openPic;
   Button savePic;
   FileDialog openPicture;
   BufferedImage bi = null;
   Graphics2D g2d = null;
 
   public void init()
   {
     int width2 = Toolkit.getDefaultToolkit().getScreenSize().width;
     int height2 = Toolkit.getDefaultToolkit().getScreenSize().height;
 
     this.bi = new BufferedImage(width2, height2, 1);
 
     this.img[0] = getImage(getCodeBase(), "image/1.gif");
     this.img[1] = getImage(getCodeBase(), "image/2.gif");
     this.img[2] = getImage(getCodeBase(), "image/3.gif");
     this.img[3] = getImage(getCodeBase(), "4.gif");
     this.img[4] = getImage(getCodeBase(), "5.gif");
     this.img[5] = getImage(getCodeBase(), "6.gif");
 
     resize(800, 600);
     setLayout(null);
 
     for (int i = 0; i < this.img.length; ++i) {
       this.mt.addImage(this.img[i], 0);
     }
     try
     {
       this.mt.waitForAll();
     }
     catch (Exception ex)
     {
       System.err.println(ex.toString());
     }
 
     add(this.jp);
     this.jp.setBounds(0, 0, 800, 80);
 
     for (int i = 0; i < this.img.length; ++i)
     {
       this.mycan[i] = new DrawImage.myCanvas(this.img[i]);
       this.width[i] = this.img[i].getWidth(this);
       this.height[i] = this.img[i].getHeight(this);
       this.jp.add(this.mycan[i]);
       this.mycan[i].setBounds(100 + 5 * (i + 1) + this.widthcount, 5, 
         this.img[i].getWidth(this), this.img[i].getHeight(this));
      this.widthcount += this.img[i].getWidth(this);
     }
 
     this.menuItem1 = new MenuItem("隐藏图像工具栏");
     this.menuItem2 = new MenuItem("显示图像工具栏");
     this.menuItem3 = new MenuItem("清空画面");
 
     this.menuItem4 = new MenuItem("显示绘图工具栏");
     this.menuItem5 = new MenuItem("隐藏绘图工具栏");
 
     this.popMenu2.add(this.menuItem1);
     this.popMenu2.add(this.menuItem2);
     this.popMenu2.add(this.menuItem3);
     this.popMenu2.addSeparator();
     this.popMenu2.add(this.menuItem4);
     this.popMenu2.add(this.menuItem5);
 
     add(this.popMenu2);
 
     this.menuItem1.addActionListener(this);
     this.menuItem2.addActionListener(this);
     this.menuItem3.addActionListener(this);
     this.menuItem4.addActionListener(this);
     this.menuItem5.addActionListener(this);
 
     this.toolPanel = new Panel(null);
     this.toolPanel.setLayout(new FlowLayout());
 
     this.paintInfo = new Vector();
 
     this.ColChoice = new Choice();
     this.ColChoice.add("black");
     this.ColChoice.add("red");
     this.ColChoice.add("blue");
     this.ColChoice.add("green");
     this.ColChoice.addItemListener(this);
 
     this.SizeChoice = new Choice();
     this.SizeChoice.add("1");
     this.SizeChoice.add("3");
     this.SizeChoice.add("5");
     this.SizeChoice.add("7");
     this.SizeChoice.add("9");
     this.SizeChoice.addItemListener(this);
 
     this.EraserChoice = new Choice();
     this.EraserChoice.add("5");
     this.EraserChoice.add("9");
     this.EraserChoice.add("13");
     this.EraserChoice.add("17");
     this.EraserChoice.addItemListener(this);
 
     this.clear = new Button("清除");
     this.eraser = new Button("橡皮");
     this.pen = new Button("画笔");
     this.drLine = new Button("画直线");
     this.drCircle = new Button("画圆形");
     this.drRect = new Button("画矩形");
 
     this.openPic = new Button("打开图画");
     this.savePic = new Button("保存图画");
 
    this.colchooser = new Button("显示调色板");
 
     this.clear.addActionListener(this);
     this.eraser.addActionListener(this);
     this.pen.addActionListener(this);
     this.drLine.addActionListener(this);
     this.drCircle.addActionListener(this);
     this.drRect.addActionListener(this);
     this.openPic.addActionListener(this);
     this.savePic.addActionListener(this);
     this.colchooser.addActionListener(this);
 
     this.颜色 = new Label("画笔颜色", 1);
     this.大小B = new Label("画笔大小", 1);
     this.大小E = new Label("橡皮大小", 1);
 
     this.toolPanel.add(this.openPic);
     this.toolPanel.add(this.savePic);
 
     this.toolPanel.add(this.pen);
     this.toolPanel.add(this.drLine);
     this.toolPanel.add(this.drCircle);
     this.toolPanel.add(this.drRect);
 
     this.toolPanel.add(this.颜色);
     this.toolPanel.add(this.ColChoice);
     this.toolPanel.add(this.大小B);
     this.toolPanel.add(this.SizeChoice);
     this.toolPanel.add(this.colchooser);
 
     this.toolPanel.add(this.eraser);
     this.toolPanel.add(this.大小E);
     this.toolPanel.add(this.EraserChoice);
 
     this.toolPanel.add(this.clear);
 
     add(this.toolPanel);
     this.toolPanel.setBounds(0, 0, 900, 80);
     this.toolPanel.setBackground(Color.black);
     this.toolPanel.setVisible(false);
 
     addMouseListener(this);
     addMouseMotionListener(this);
   }
 
   public void itemStateChanged(ItemEvent e)
   {
     if (e.getSource() == this.ColChoice)
     {
       String name = this.ColChoice.getSelectedItem();
 
       if (name == "black")
       {
         this.c = new Color(0, 0, 0);
       }
       else if (name == "red")
       {
         this.c = new Color(255, 0, 0);
       }
       else if (name == "green")
       {
         this.c = new Color(0, 255, 0);
       } else {
         if (name != "blue")
           return;
         this.c = new Color(0, 0, 255);
       }
     }
     else if (e.getSource() == this.SizeChoice)
     {
       String selected = this.SizeChoice.getSelectedItem();
 
       if (selected == "1")
       {
         this.con = 1;
       }
       else if (selected == "3")
       {
         this.con = 3;
       }
       else if (selected == "5")
       {
         this.con = 5;
       }
       else if (selected == "7")
       {
         this.con = 7;
       }
       else if (selected == "9")
       {
         this.con = 9;
       }
       this.size = 
         new BasicStroke(this.con, 0, 
         2);
     } else {
       if (e.getSource() != this.EraserChoice)
         return;
       String Esize = this.EraserChoice.getSelectedItem();
 
       if (Esize == "5")
       {
         this.Econ = 10;
       }
       else if (Esize == "9")
       {
         this.Econ = 18;
       }
       else if (Esize == "13")
       {
         this.Econ = 26;
       } else {
         if (Esize != "17")
           return;
         this.Econ = 51;
       }
     }
   }
 
   public void actionPerformed(ActionEvent e)
   {
     if (e.getSource() == this.menuItem1)
     {
       this.jp.setVisible(false);
       this.toolPanel.setVisible(true);
       repaint();
     }
     else if (e.getSource() == this.menuItem2)
     {
       this.jp.setVisible(true);
       this.toolPanel.setVisible(false);
       repaint();
     }
     else if (e.getSource() != this.menuItem3)
     {
       if (e.getSource() == this.menuItem4)
       {
         this.toolPanel.setVisible(true);
         repaint();
       }
       else if (e.getSource() == this.menuItem5)
       {
         this.toolPanel.setVisible(false);
         repaint();
       }
     }
 
     if (e.getSource() == this.pen)
     {
       this.toolFlag = 0;
     }
 
     if (e.getSource() == this.eraser)
     {
      this.toolFlag = 1;
     }
 
     if (e.getSource() == this.clear)
     {
       this.toolFlag = 2;
       this.paintInfo.removeAllElements();
       repaint();
     }
 
     if (e.getSource() == this.drLine)
     {
       this.toolFlag = 3;
     }
 
     if (e.getSource() == this.drCircle)
     {
       this.toolFlag = 4;
     }
 
     if (e.getSource() == this.drRect)
     {
       this.toolFlag = 5;
     }
 
     if (e.getSource() == this.colchooser)
     {
       Color newColor = JColorChooser.showDialog(this, "调色板", this.c);
       this.c = newColor;
     }
 
     if (e.getSource() == this.openPic)
     {
    	 Frame f = new DrawImage.fileDialog("File Dialog Demo!");
       f.setVisible(false);
       f.setSize(100, 100);
       FileDialog fd1 = new FileDialog(f, "File Dialog", 0);
       fd1.setVisible(true);
       try
       {
         this.curimg = ImageIO.read(new File(fd1.getDirectory(), fd1.getFile()));
 
         Canvas ca = new DrawImage.canvas(this.curimg);
         add(ca);
         ca.setBounds(100, 200, this.curimg.getWidth(this), 
           this.curimg.getHeight(this));
         validate();
         this.curcan = null;
       }
       catch (IOException e1)
       {
         e1.printStackTrace();
       }
 
     }
 
     if (e.getSource() != this.savePic)
       return;
     Frame f = new DrawImage.fileDialog("File Dialog Demo!");
     f.setVisible(false);
     f.setSize(100, 100);
 
     FileDialog fd = new FileDialog(f, "File Dialog", 1);
     fd.setVisible(true);
     try
     {
       if (fd.getFile() == null)
         return;
       String pathStr = fd.getDirectory();
 
       pathStr = pathStr.replace("\\", "\\\\");
       String path = pathStr + fd.getFile();
 
       ImageIO.write(this.bi, "jpg", new File(path));
     }
     catch (IOException e2)
     {
       e2.printStackTrace();
     }
   }
 
   public void mouseClicked(MouseEvent e)
   {
     clickhere(e);
   }
 
   public void mouseEntered(MouseEvent e)
   {
   }
 
   public void mouseExited(MouseEvent e)
   {
   }
 
   public void update(Graphics g)
   {
     paint(g);
   }
 
   public void mousePressed(MouseEvent e)
   {
     DrawImage.Point p2;
     switch (this.toolFlag)
     {
     case 3:
       this.x = e.getX();
       this.y = e.getY();
       p2 = new DrawImage.Point(this.x, this.y, this.c, this.toolFlag, this.con);
       this.paintInfo.addElement(p2);
       break;
     case 4:
       this.x = e.getX();
       this.y = e.getY();
       p2 = new DrawImage.Point(this.x, this.y, this.c, this.toolFlag, this.con);
       this.paintInfo.addElement(p2);
       break;
     case 5:
       this.x = e.getX();
       this.y = e.getY();
       p2 = new DrawImage.Point(this.x, this.y, this.c, this.toolFlag, this.con);
       this.paintInfo.addElement(p2);
     }
   }
 
   public void mouseReleased(MouseEvent e)
   {
     if ((e.getButton() == 3) && (e.getClickCount() == 2))
     {
       this.popMenu2.show(e.getComponent(), e.getX(), e.getY());
     }
     DrawImage.Point p3;
     switch (this.toolFlag)
     {
     case 0:
       this.paintInfo.addElement(this.cutflag);
       break;
     case 1:
       this.paintInfo.addElement(this.cutflag);
       break;
     case 3:
       this.x = e.getX();
       this.y = e.getY();
       p3 = new DrawImage.Point(this.x, this.y, this.c, this.toolFlag, this.con);
       this.paintInfo.addElement(p3);
       this.paintInfo.addElement(this.cutflag);
       repaint();
       break;
     case 4:
       this.x = e.getX();
       this.y = e.getY();
       p3 = new DrawImage.Point(this.x, this.y, this.c, this.toolFlag, this.con);
       this.paintInfo.addElement(p3);
       this.paintInfo.addElement(this.cutflag);
       repaint();
       break;
     case 5:
       this.x = e.getX();
       this.y = e.getY();
       p3 = new DrawImage.Point(this.x, this.y, this.c, this.toolFlag, this.con);
       this.paintInfo.addElement(p3);
       this.paintInfo.addElement(this.cutflag);
       repaint();
     case 2:
     }
   }
 
   public void mouseDragged(MouseEvent e)
   {
     DrawImage.Point p1;
     switch (this.toolFlag)
     {
     case 0:
       this.x = e.getX();
       this.y = e.getY();
       p1 = new DrawImage.Point(this.x, this.y, this.c, this.toolFlag, this.con);
       this.paintInfo.addElement(p1);
       repaint();
       break;
     case 1:
       this.x = e.getX();
       this.y = e.getY();
       p1 = new DrawImage.Point(this.x, this.y, null, this.toolFlag, this.Econ);
       this.paintInfo.addElement(p1);
       repaint();
     }
   }
 
   public void mouseMoved(MouseEvent e)
   {
     if (!(this.toolPanel.isVisible()))
       return;
     setCursor(new Cursor(1));
   }
 
   public void clickhere(MouseEvent e)
   {
     if (this.curcan == null)
       return;
     if (e.getModifiers() != 4)
       return;
     Canvas ca = new DrawImage.canvas(this.curimg);
     add(ca);
     ca.setBounds(e.getX(), e.getY(), this.curimg.getWidth(this), 
       this.curimg.getHeight(this));
     validate();
     this.curcan = null;
   }
 
   public void paint(Graphics g)
   {
     this.g2d = ((Graphics2D)g);
     this.g2d = ((Graphics2D)this.bi.getGraphics());
 
    this.n = this.paintInfo.size();
 
     if (this.toolFlag == 2)
     {
       g.clearRect(0, 0, getSize().width, getSize().height);
       this.g2d.clearRect(0, 0, getSize().width, getSize().height);
     }
 
     for (int i = 0; i < this.n - 1; ++i)
     {
       DrawImage.Point p1 = (DrawImage.Point)this.paintInfo.elementAt(i);
       DrawImage.Point p2 = (DrawImage.Point)this.paintInfo.elementAt(i + 1);
       this.size = 
         new BasicStroke(p1.boarder, 0, 
         2);
 
       this.g2d.setColor(p1.col);
       this.g2d.setStroke(this.size);
 
       if (p1.tool != p2.tool)
         continue;
       switch (p1.tool)
       {
       case 0:
         Line2D line1 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
         this.g2d.draw(line1);
         break;
       case 1:
         g.clearRect(p1.x, p1.y, p1.boarder, p1.boarder);
         this.g2d.clearRect(p1.x, p1.y, p1.boarder, p1.boarder);
         break;
       case 3:
         Line2D line2 = null;
 
         int X = Math.abs(p2.x - p1.x);
         int Y = Math.abs(p2.y - p1.y);
 
         if (X >= Y)
         {
           line2 = new Line2D.Double(p1.x, p1.y, p2.x, p1.y);
         }
         else
         {
           line2 = new Line2D.Double(p1.x, p1.y, p1.x, p2.y);
         }
 
         this.g2d.draw(line2);
         break;
       case 4:
         Ellipse2D ellipse = new Ellipse2D.Double(p1.x, p1.y, 
           Math.abs(p2.x - p1.x), Math.abs(p2.y - p1.y));
         this.g2d.draw(ellipse);
         break;
       case 5:
         Rectangle2D rect = new Rectangle2D.Double(p1.x, p1.y, 
           Math.abs(p2.x - p1.x), Math.abs(p2.y - p1.y));
         this.g2d.draw(rect);
         break;
       case 6:
         ++i;
       case 2:
       }
 
     }
 
     g.drawImage(this.bi, 0, 0, this);
   }
 
   public double radians(int degrees)
   {
     return (degrees * 3.141592653589793D / 180.0D);
   }
 
   class MyWindowAdapter extends WindowAdapter
   {
     DrawImage.fileDialog sf;
 
     public MyWindowAdapter(DrawImage.fileDialog paramfileDialog)
     {
       this.sf = paramfileDialog;
     }
 
     public void windowClosing(WindowEvent we)
     {
       this.sf.setVisible(false);
     }
   }
 
   class Point
     implements Serializable
   {
     int x;
     int y;
     Color col;
     int tool;
     int boarder;
 
     Point(int paramInt1, int paramInt2, Color paramColor, int paramInt3, int paramInt4)
     {
       this.x = paramInt1;
       this.y = paramInt2;
       this.col = paramColor;
       this.tool = paramInt3;
       this.boarder = paramInt4;
     }
   }
 
   class canvas extends Canvas
   {
     private static final long serialVersionUID = 1L;
     Image im = null;
     int X;
     int Y;
     int bufferX;
     int bufferY;
     int bufferWidth;
     int bufferHeight;
 
     public canvas(Image paramImage)
     {
       setCursor(new Cursor(13));
       this.im = paramImage;
 
       addMouseListener(new MouseAdapter()
       {
         public void mousePressed(MouseEvent e)
         {
           DrawImage.canvas.this.clickcanvas(e);
         }
 
         public void mouseReleased(MouseEvent e)
         {
           Canvas mcan = (Canvas)e.getSource();
 
           int mtop = mcan.getY(); int mleft = mcan.getX(); int mwidth = 
             mcan.getWidth(); int mheight = mcan.getHeight();
 
           DrawImage.canvas.this.X = (mleft + e.getX() - DrawImage.this.clickX);
           DrawImage.canvas.this.Y = (mtop + e.getY() - DrawImage.this.clickY);
 
           DrawImage.canvas.this.bufferX = DrawImage.canvas.this.X;
           DrawImage.canvas.this.bufferY = DrawImage.canvas.this.Y;
           DrawImage.canvas.this.bufferWidth = mwidth;
           DrawImage.canvas.this.bufferHeight = mheight;
 
           DrawImage.canvas.this.drawBufferImage();
         }
       });
       addKeyListener(new KeyAdapter()
       {
         public void keyPressed(KeyEvent e)
         {
           if (e.getKeyCode() == 127)
             DrawImage.canvas.this.removecanvas(e);
         }
       });
       addMouseMotionListener(new MouseMotionAdapter()
       {
         public void mouseDragged(MouseEvent e)
         {
           DrawImage.canvas.this.movecanvas(e);
         }
       });
     }
 
     public void movecanvas(MouseEvent e)
     {
       Canvas mcan = (Canvas)e.getSource();
 
       int mtop = mcan.getY(); int mleft = mcan.getX(); int mwidth = 
         mcan.getWidth(); int mheight = mcan.getHeight();
 
       clearImage();
 
       mcan.setBounds(mleft + e.getX() - DrawImage.this.clickX, mtop + e.getY() - DrawImage.this.clickY, 
         mwidth, mheight);
 
       for (int i = 0; i < DrawImage.this.v.size(); ++i)
       {
         Vector v1 = (Vector)DrawImage.this.v.elementAt(i);
         if (((Canvas[])v1.elementAt(0))[0].equals(mcan))
         {
           ((int[])v1.elementAt(1))[0] = mcan.getX();
           ((int[])v1.elementAt(1))[1] = mcan.getY();
           ((int[])v1.elementAt(1))[2] = mcan.getWidth();
           ((int[])v1.elementAt(1))[3] = mcan.getHeight();
         } else {
           if (!(((Canvas[])v1.elementAt(0))[1].equals(mcan)))
             continue;
           ((int[])v1.elementAt(1))[4] = mcan.getX();
           ((int[])v1.elementAt(1))[5] = mcan.getY();
           ((int[])v1.elementAt(1))[6] = mcan.getWidth();
           ((int[])v1.elementAt(1))[7] = mcan.getHeight();
         }
       }
 
       ((DrawImage)getParent()).repaint();
     }
 
     public void removecanvas(KeyEvent e)
     {
       try
       {
         Canvas mcan = (Canvas)e.getSource();
         ((DrawImage)getParent()).remove(DrawImage.this.curdelcan);
         for (int i = DrawImage.this.v.size() - 1; i >= 0; --i)
         {
           Vector v1 = (Vector)DrawImage.this.v.elementAt(i);
           if ((!(((Canvas[])v1.elementAt(0))[0].equals(mcan))) && 
             (!(((Canvas[])v1.elementAt(0))[1].equals(mcan)))) continue;
           DrawImage.this.v.removeElementAt(i);
         }
         ((DrawImage)getParent()).validate();
         ((DrawImage)getParent()).repaint();
       }
       catch (NullPointerException ex)
       {
         return;
       }
     }
 
     public void clickcanvas(MouseEvent e)
     {
       DrawImage.this.curdelcan = ((Canvas)e.getSource());
       DrawImage.this.clickX = e.getX();
       DrawImage.this.clickY = e.getY();
       ((DrawImage)getParent()).repaint();
 
       if (((DrawImage)getParent()).drawclear.equals(""))
         return;
       if ((((DrawImage)getParent()).startObj == null) && 
         (((DrawImage)getParent()).endObj == null)) {
         ((DrawImage)getParent()).startObj = ((Canvas)e.getSource()); } else {
         if ((((DrawImage)getParent()).startObj == null) || 
           (((DrawImage)getParent()).endObj != null) || 
           (((DrawImage)getParent()).startObj == (Canvas)
           e.getSource()))
           return;
         ((DrawImage)getParent()).endObj = ((Canvas)e.getSource());
         Vector v1 = new Vector();
         v1.addElement(new Canvas[] { DrawImage.this.startObj, DrawImage.this.endObj });
         v1.addElement(new int[] { DrawImage.this.startObj.getX(), DrawImage.this.startObj.getY(), 
           DrawImage.this.startObj.getWidth(), DrawImage.this.startObj.getHeight(), DrawImage.this.endObj.getX(), 
           DrawImage.this.endObj.getY(), DrawImage.this.endObj.getWidth(), 
           DrawImage.this.endObj.getHeight() });
         DrawImage.this.v.addElement(v1);
         ((DrawImage)getParent()).drawclear = "";
 
         ((DrawImage)getParent()).startObj = null;
         ((DrawImage)getParent()).endObj = null;
       }
     }
 
     public void paint(Graphics g)
     {
       g.drawImage(this.im, 0, 0, this);
     }
 
     public void drawBufferImage()
     {
       Graphics2D g2 = (Graphics2D)DrawImage.this.bi.getGraphics();
 
       g2.drawImage(this.im, this.X, this.Y, this);
       g2.dispose();
     }
 
     public void clearImage()
     {
       Graphics2D g2 = (Graphics2D)DrawImage.this.bi.getGraphics();
 
       g2.clearRect(this.bufferX, this.bufferY, this.bufferWidth, this.bufferHeight);
     }
   }
 
   class fileDialog extends Frame
   {
     private static final long serialVersionUID = 1L;
 
     fileDialog(String title)
     {
       super(title);
       DrawImage.MyWindowAdapter adapter = new DrawImage.MyWindowAdapter(this);
       addWindowListener(adapter);
     }
   }
 
   class myCanvas extends Canvas
   {
     private static final long serialVersionUID = 1L;
     Image im = null;
     String str = "";
 
     public myCanvas(String paramString)
     {
       this.str = paramString;
     }
 
     public myCanvas(Image paramImage)
     {
       setCursor(new Cursor(12));
       this.im = paramImage;
       addMouseListener(new MouseAdapter()
       {
         public void mousePressed(MouseEvent e)
         {
           DrawImage.myCanvas.this.clickmyCanvas(e);
         }
       });
     }
 
     public void clickmyCanvas(MouseEvent e)
     {
       ((DrawImage)getParent().getParent()).drawclear = "";
       ((DrawImage)getParent().getParent()).startObj = null;
       ((DrawImage)getParent().getParent()).endObj = null;
 
       DrawImage.this.curcan = ((Canvas)e.getSource());
       DrawImage.this.curimg = this.im;
     }
 
     public void paint(Graphics g)
     {
       if (this.str.equals(""))
       {
         g.drawImage(this.im, 0, 0, this);
       }
       else
       {
         g.drawString(this.str, 20, getHeight() / 2);
       }
     }
   }
 }

 

分享到:
评论

相关推荐

    基于java applet的简易画图程序

    以上代码展示了如何创建一个简单的Java Applet画图程序,用户可以按住鼠标左键拖动来绘制线条。 6. 总结: 通过这个基于Java Applet的简易画图程序,我们可以学习到Java事件处理机制,理解如何响应用户的鼠标操作,...

    一个简单的Applet画图程序(鼠标画线)

    一个Applet程序,通过鼠标监听实现鼠标随笔画线 没多大技术含量,比较简单

    applet 画图程序

    这是我写的一个简陋的画图版.里面还有很多可以改进的地方,简化版方便大家参考.

    Java Applet 设计的画图程序

    在这个特定的项目中,"Java Applet 设计的画图程序" 是一个基于Java编程语言实现的简单画图应用,它允许用户在浏览器环境下进行图形绘制。这个程序非常适合初学者了解Applet的工作原理以及如何利用Java的图形库来...

    applet 小的画图程序

    这个小程序是我自己搞的,能参数化的绘制椭圆,长方形,以及鼠标绘制多边形,有兴趣的可以研究研究,如果有什么新的思路,留言给我,以后会改进的,

    Applet画图,上传

    在这个特定的场景下,"Applet画图,上传"涉及到的技术点主要包括Java Applet的基础使用、图形绘制以及文件上传。 首先,我们需要了解Java Applet。Applet是Java平台早期的一种特性,允许开发者创建可以在Web浏览器...

    有applet实现画图的操作界面

    本案例中,我们探讨的是一个利用Applet来实现画图操作界面的应用。这个界面允许用户绘制直线、椭圆、矩形以及曲线,并且可以自定义颜色和画笔的大小,提供了一种丰富的图形绘制功能。 首先,我们需要了解Applet的...

    java的applet小程序

    Java的Applet小程序是Java平台早期的一个特性,主要用于在Web浏览器中执行客户端的Java代码,为网页增添交互性。这个技术允许开发者创建嵌入到HTML页面中的小型应用程序,比如动态图像、游戏或者其他交互式功能。在...

    Java在Applet小程序中显示图片.rar

    Java Applet是一种小型的Java程序,它可以在支持Java的浏览器中运行,为用户提供交互式的功能。在Web页面中,我们通常使用HTML、CSS和JavaScript来展示内容,包括图像。然而,Java Applet允许我们利用Java的强大功能...

    java用applet画图用到的方法(涉及双缓冲)

    Java Applet 是一种在网页上运行的小型Java应用程序,它允许开发者创建动态、交互式的Web内容。在Java的早期版本中,Applet被广泛用于图形用户界面(GUI)的开发,尤其是在网页游戏和简单动画中。然而,随着技术的...

    Java中用Applet画sinx

    在Java编程语言中,Applet是一种特殊的Java程序,它可以在Web浏览器中运行,为用户提供交互式的体验。在本示例中,我们将探讨如何利用Applet来绘制正弦函数sin(x)的图形。这个过程涉及到Java图形处理、数学计算以及...

    java小游戏(鼠标画图)程序

    ### Java小游戏(鼠标画图)程序知识点解析 #### 一、程序概述 本Java小游戏主要实现了用户通过鼠标绘制图形的功能,并允许用户改变图形的颜色。此外,还提供了清除单个图形和清空整个画布的功能。 #### 二、关键...

    java编写的画图小程序

    &lt;APPLET CODE="Snowman.class"WIDTH=200 HEIGHT=100&gt; &lt;/APPLET&gt; import java.applet.*; import java.awt.*; public class Snowman extends Applet{ public void paint(Graphics g){ final int MID=150; final ...

    PaintChat日本Java画图聊天程序(另附详细使用说明)

    "PaintChat日本Java画图聊天程序"是一款基于Java Applet技术的在线互动工具,它集成了画图和聊天的功能,让用户能够通过网络实时共享创作过程和进行文字交流。这款软件源自日本,以其独特的创意和易用性吸引了众多...

    用java 编的小应用程序Applet 实现风扇的简单功能

    Java Applet 是一种小型的、可嵌入网页中的Java程序。它可以在支持Java的Web浏览器上运行,用于增强网页的功能性和交互性。在本案例中,我们通过Java Applet 实现了一个具有简单功能的风扇模拟器,该程序允许用户...

    WEB画图(java)

    然而,由于安全性和性能问题,以及现代浏览器对Applet支持的逐渐淘汰,这一技术已不再推荐使用。 2. **JavaFX**: JavaFX是一个用于创建桌面、Web和移动平台的富客户端应用程序的现代框架。它提供了丰富的图形库和...

    java_paiting.rar_drawlet applet

    Java_paiting.rar_drawlet_applet 是一个基于Java编写的画图应用程序,它利用了Java Applet技术,为用户提供了一个简单但功能丰富的矢量图形绘制环境。Drawlet Applet是Java编程领域中一种常见的示例,它展示了如何...

    Java简单画图实例_画一个诺基亚手机

    在Java的历史版本中,APPLET是一种可以嵌入HTML网页中的小程序,用于提供动态和交互式的网页内容。然而,随着Web技术的发展,APPLET已经过时,现在更推荐使用JApplet或者JavaFX。在本实例中,我们依然可以学习...

    软件学院精品课程《Java程序设计》.doc

    实验一的练习部分要求学生运行并分析给定的程序,了解其类型(Application或Applet)、运行步骤、方法参数的意义,并尝试将其转换为另一种类型的Java程序。 实验二进一步深化Java语言的基础,包括: 1. **图形绘制...

Global site tag (gtag.js) - Google Analytics