双击右键才显示功能菜单。。。
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); } } } }