import java.awt.*;
import java.util.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;
class Point
{
int x,y;
Point(int x,int y)
{
this.x=x;
this.y=y;
}
}
public class MakeJPEG extends Canvas implements MouseMotionListener,MouseListener,ActionListener
{
int x=-1,y=-1,橡皮擦通知=0,清除通知=0;
Vector v=null;int n=1;
Graphics2D ggg ; //負責將圖形繪製到image的畫筆對象
BufferedImage image; //映像對象
Frame window;
Button 儲存,調色盤,橡皮,清除,畫筆,擷取螢幕,繪製圖形;
Color 畫筆顏色;
Panel pCenter,pSouth,pNorth;
public MakeJPEG()
{
儲存=new Button("將繪製的圖形或螢幕儲存為JPG檔案");
擷取螢幕=new Button("擷取螢幕");
繪製圖形=new Button("繪製圖形");
調色盤=new Button("開啟調色盤");
畫筆=new Button("畫筆");
橡皮=new Button("橡皮");
清除=new Button("清除");
調色盤.addActionListener(this);
繪製圖形.addActionListener(this);
儲存.addActionListener(this);
畫筆.addActionListener(this);
橡皮.addActionListener(this);
清除.addActionListener(this);
擷取螢幕.addActionListener(this);
畫筆顏色=new Color(0,0,0);
addMouseMotionListener(this);
addMouseListener(this);
v=new Vector();
setBackground(Color.white); //設定畫布顏色
//建立一個寬高各為210個像素的待繪製的image對象
image=new BufferedImage(210,210,BufferedImage.TYPE_INT_RGB);
ggg=image.createGraphics(); //建立在image對象中進行繪製的畫筆對象
Rectangle2D rect=new Rectangle2D.Double(0,0,210,210);
ggg.setColor(getBackground());
ggg.fill(rect); //將映像的底色設定為和畫布相同
window=new Frame("JPEG映像產生器");
pCenter=new Panel();
pCenter.setLayout(null);
pCenter.add(this);
pCenter.setBackground(Color.gray);
this.setBounds(80,30,210,210); //設定畫布的大小和映像的大小相同
window.add(pCenter,BorderLayout.CENTER);
pNorth=new Panel();
pNorth.add(儲存);
pNorth.add(繪製圖形);
pNorth.add(擷取螢幕);
window.add(pNorth,BorderLayout.NORTH);
pSouth=new Panel();
pSouth.add(調色盤);
pSouth.add(橡皮);
pSouth.add(清除);
pSouth.add(畫筆);
window.add(pSouth,BorderLayout.SOUTH);
window.setVisible(true);
window.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
window.setBounds(100,80,390,380);
window.validate(); //確保視窗中的組件能正確顯示出來
}
public void paint(Graphics g) //負責在畫布中繪製圖形的筆畫對象
{
if(x!=-1&&y!=-1&&橡皮擦通知==0&&清除通知==0)
{
g.setColor(畫筆顏色);
n=v.size();
for(int i=0;i<n-1;i++)
{
Point p1=(Point)v.elementAt(i);
Point p2=(Point)v.elementAt(i+1);
g.drawLine(p1.x,p1.y,p2.x,p2.y); //畫筆g在畫布中繪製圖形
ggg.setColor(g.getColor());
ggg.drawLine(p1.x,p1.y,p2.x,p2.y); //同時畫筆ggg也在image中繪製圖形
}
}
else if(橡皮擦通知==1&&清除通知==0) //擦去4X4大小地區的圖形
{
g.setColor(getBackground());
g.fillRect(x-2,y-2,4,4);
ggg.setColor(getBackground());
ggg.fillRect(x-2,y-2,4,4);
}
else if(清除通知==1&&橡皮擦通知==0) //清除畫布及image中的圖形
{
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); //將image對象繪製到畫布中
}
public void mouseDragged(MouseEvent e)
{
x=(int)e.getX();
y=(int)e.getY();
Point p=new Point(x,y);
v.addElement(p);
repaint();
}
public void mouseMoved(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{}
public void mouseReleased(MouseEvent e) //放開滑鼠,則清除圖形向量
{
v.removeAllElements();
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void update(Graphics g)
{
{
paint(g);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==橡皮)
{
橡皮擦通知=1;
清除通知=0 ;
}
else if(e.getSource()==清除)
{
清除通知=1;
橡皮擦通知=0;
repaint();
}
else if(e.getSource()==畫筆)
{
橡皮擦通知=0;
清除通知=0;
}
else if(e.getSource()==儲存)
{
//顯示“儲存檔案”對話方塊
FileDialog savedialog=new FileDialog(window,"儲存圖型到JPG格式",FileDialog.SAVE);
//該對話方塊是有強制回應對話方塊,只有收到響應,程式才能繼續執行
savedialog.setVisible(true);
//選擇對話方塊的yes或cancel按鈕後,對話方塊自動關閉,程式繼續執行
if(savedialog.getFile()!=null)
//如果選擇了yes,getFile()將返迴文件的名字
{
try{
//JPEG檔案的命名由對話方塊負責
String fileName=savedialog.getFile();
FileOutputStream out=new FileOutputStream(fileName);
//擷取一個JPEG編碼器
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
//擷取解碼器的編碼參數
JPEGEncodeParam param=encoder.getDefaultJPEGEncodeParam(image);
//將映像品質設定為100%
param.setQuality(1.0f,false);
encoder.setJPEGEncodeParam(param);
//編碼器負責將image寫入輸出資料流out
encoder.encode(image);
out.close();
}
catch(Exception EE)
{
}
}
}
else if(e.getSource()==擷取螢幕)
{
Robot robot=null; //用來擷取螢幕映像的Robot對象
try{
robot=new Robot();
}
catch(Exception er)
{
}
Rectangle screenRect=null; //用來確定image大小的矩形對象
int width=getToolkit().getScreenSize().width; //擷取螢幕的寬
int height=getToolkit().getScreenSize().height; //擷取螢幕的高
screenRect=new Rectangle(0,0,width,height);
window.setVisible(false); //隱去當前Java程式視窗,以使擷取螢幕映像
this.window.setVisible(false);
image=robot.createScreenCapture(screenRect); //將螢幕映像存入image
window.setVisible(true); //重新顯示當前程式視窗
repaint();
}
else if(e.getSource()==調色盤) //選擇畫顏色
{
Color tempColor=JColorChooser.showDialog(window,"調色盤",畫筆顏色);
{
if(tempColor!=null)
{
畫筆顏色=tempColor;
畫筆.setForeground(畫筆顏色);
}
}
}
else if(e.getSource()==繪製圖形)
{
window.dispose(); //釋放視窗
this.window.dispose();
MakeJPEG canvas=new MakeJPEG(); //建立畫布對象
}
}
public static void main(String args[])
{
new MakeJPEG();
}
}
然後解決Eclipse的設定問題
這個事eclipse的設定問題,它預設把這些受訪問限制的API設成了ERROR,你只要把
Windows-Preferences-Java-Complicer-Errors/Warnings
裡面的Deprecated and restricted API中的Forbidden references(access rules)選為Warning就可以編譯通過了。
成功通過,真是太有才了,哈哈