importjava.awt.*;importjava.util.*;importjava.awt.geom.*;importjava.awt.image.*;importjava.awt.event.*;importjava.io.*;importjavax.swing.*;importcom.sun.image.codec.jpeg.*;classPoint{intx,y;Point(intx,inty){this.x=x;this.y=y;}}publicclassMakeJPEGextendsCanvasimplementsMouseMotionListener,MouseListener,ActionListener{intx=-1,y=-1,橡皮擦通知=0,清除通知=0;Vectorv=null;intn=1;Graphics2Dggg;BufferedImageimage;Framewindow;Button保存,调色板,橡皮,清除,画笔,获取屏幕,绘制图形;Color画笔颜色;PanelpCenter,pSouth,pNorth;publicMakeJPEG(){保存=newButton("将绘制的图形或屏幕保存为JPEG文件");获取屏幕=newButton("获取屏幕");绘制图形=newButton("绘制图形");调色板=newButton("打开调色板");画笔=newButton("画笔");橡皮=newButton("橡皮");清除=newButton("清除");调色板.addActionListener(this);绘制图形.addActionListener(this);保存.addActionListener(this);画笔.addActionListener(this);橡皮.addActionListener(this);清除.addActionListener(this);获取屏幕.addActionListener(this);画笔颜色=newColor(0,0,0);addMouseMotionListener(this);addMouseListener(this);v=newVector();setBackground(Color.white);image=newBufferedImage(200,200,BufferedImage.TYPE_INT_RGB);ggg=image.createGraphics();Rectangle2Drect=newRectangle2D.Double(0,0,200,200);ggg.setColor(getBackground());ggg.fill(rect);window=newFrame("JPEG图像生成器");pCenter=newPanel();pCenter.setLayout(null);pCenter.add(this);pCenter.setBackground(Color.gray);this.setBounds(80,30,210,210);window.add(pCenter,BorderLayout.CENTER);pNorth=newPanel();pNorth.add(保存);pNorth.add(绘制图形);pNorth.add(获取屏幕);window.add(pNorth,BorderLayout.NORTH);pSouth=newPanel();pSouth.add(调色板);pSouth.add(橡皮);pSouth.add(清除);pSouth.add(画笔);window.add(pSouth,BorderLayout.SOUTH);window.setVisible(true);window.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});window.setBounds(100,80,390,380);window.validate();}publicvoidpaint(Graphicsg){if(x!=-1&&y!=-1&&橡皮擦通知==0&&清除通知==0){g.setColor(画笔颜色);n=v.size();for(inti=0;i<n-1;i++){Pointp1=(Point)v.elementAt(i);Pointp2=(Point)v.elementAt(i+1);g.drawLine(p1.x,p1.y,p2.x,p2.y);ggg.setColor(g.getColor());ggg.drawLine(p1.x,p1.y,p2.x,p2.y);}}elseif(橡皮擦通知==1&&清除通知==0){g.setColor(getBackground());g.fillRect(x-2,y-2,4,4);ggg.setColor(getBackground());ggg.fillRect(x-2,y-2,4,4);}elseif(清除通知==1&&橡皮擦通知==0){g.setColor(getBackground());g.fillRect(0,0,200,200);ggg.setColor(getBackground());ggg.fillRect(0,0,200,200);}g.drawImage(image,0,0,200,200,this);}publicvoidmouseDragged(MouseEvente){x=(int)e.getX();y=(int)e.getY();Pointp=newPoint(x,y);v.addElement(p);repaint();}publicvoidmouseMoved(MouseEvente){}publicvoidmousePressed(MouseEvente){}publicvoidmouseReleased(MouseEvente){v.removeAllElements();}publicvoidmouseEntered(MouseEvente){}publicvoidmouseExited(MouseEvente){}publicvoidmouseClicked(MouseEvente){}publicvoidupdate(Graphicsg){{paint(g);}}publicvoidactionPerformed(ActionEvente){if(e.getSource()==橡皮){橡皮擦通知=1;清除通知=0;}elseif(e.getSource()==清除){清除通知=1;橡皮擦通知=0;repaint();}elseif(e.getSource()==画笔){橡皮擦通知=0;清除通知=0;}elseif(e.getSource()==保存){FileDialogsavedialog=newFileDialog(window,"保存图型到JPEG格式",FileDialog.SAVE);savedialog.setVisible(true);if(savedialog.getFile()!=null){try{StringfileName=savedialog.getFile();FileOutputStreamout=newFileOutputStream(fileName);JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(out);JPEGEncodeParamparam=encoder.getDefaultJPEGEncodeParam(image);param.setQuality(1.0f,false);encoder.setJPEGEncodeParam(param);encoder.encode(image);out.close();}catch(ExceptionEE){}}}elseif(e.getSource()==获取屏幕){Robotrobot=null;try{robot=newRobot();}catch(Exceptioner){}RectanglescreenRect=null;intwidth=getToolkit().getScreenSize().width;intheight=getToolkit().getScreenSize().height;screenRect=newRectangle(0,0,width,height);window.setVisible(false);this.window.setVisible(false);image=robot.createScreenCapture(screenRect);window.setVisible(true);repaint();}elseif(e.getSource()==调色板){ColortempColor=JColorChooser.showDialog(window,"调色板",画笔颜色);{if(tempColor!=null){画笔颜色=tempColor;画笔.setForeground(画笔颜色);}}}elseif(e.getSource()==绘制图形){window.dispose();this.window.dispose();MakeJPEGcanvas=newMakeJPEG();}}publicstaticvoidmain(Stringargs[]){newMakeJPEG();}}