import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;//這個類實現圖像的打開和保存
public class Huatu extends JPanel
implements MouseListener, MouseMotionListener, ActionListener
{
private JFrame f,f1;
private int xBegin=0,yBegin=0,xEnd=0,yEnd=0;//開始和結束點座標
private JButton butLine,butRect,butOval,butPen,butEraser;//線,方,圓
private JToolBar tb;
private int i = 0;//0,線;1,方;2,圓;3,筆;
//最後的圖形要保存下來,使用緩衝圖像
private BufferedImage bi;
private Graphics gg;//圖像專用畫筆
private JFileChooser jfc;
private JButton butSave,butOpen,butAbout;
private JButton butColor;//選擇顏色
private Color myColor;
private Label label1;
public Huatu()
{
//BufferedImage.TYPE_INT_RGB 圖像類型 標準的三基色
bi = new BufferedImage
(800,600,BufferedImage.TYPE_INT_RGB);
f = new JFrame("Yizero畫圖板");
butLine = new JButton("\");
butRect = new JButton("□");
butOval = new JButton("○");
butAbout = new JButton("關於");
butSave = new JButton("Save");
butOpen = new JButton("Open");
tb = new JToolBar();
jfc= new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG", "jpg");
jfc.setFileFilter(filter);
butColor = new JButton(" ");
myColor = Color.BLACK;//默認顏色黑色
butColor.setBackground(Color.RED);
//因爲緩衝圖像是剛創建的,所以默認顏色是黑色
//得到圖像專用畫筆
gg = bi.getGraphics();
gg.setColor(Color.white);//將畫筆調整爲白色
gg.fillRect(0,0,800,600);//將圖像塗白
gg.setColor(Color.BLACK);//將畫筆從新調成黑色
tb.add(butLine);
tb.add(butRect);
tb.add(butOval);
tb.add(butOpen);
tb.add(butSave);
tb.add(butColor);
tb.add(butAbout);
butLine.addActionListener(this);
butRect.addActionListener(this);
butOval.addActionListener(this);
butAbout.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) { About_me(); }
});
butOpen.addActionListener(this);
butSave.addActionListener(this);
butColor.addActionListener(this);
this.setLayout(new BorderLayout());
this.add(tb,BorderLayout.NORTH);
this.addMouseListener(this);
this.addMouseMotionListener(this);
f.add(this);
f.setBounds(100,100,800,600);
f.setVisible(true);
f1=new JFrame("關於作者");
setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
f1.add(new Label(" 此程序由Yizero編寫, 如有任何問題歡迎與Yizero聯繫 郵箱:[email protected]"));
f1.setLayout(new GridLayout(1,1));
f1.setFont(new Font("宋體",15,15));
f1.pack();
f1.setBounds(170,250,670,100);
f1.setResizable(false);
f1.show();
f1.setVisible(false);
f1.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e)
{
f1.setVisible(false);
}
});
}
public void About_me()
{
f1.setVisible(true);
}
public void paintComponent(Graphics g)
{
//設置畫筆顏色
g.setColor(myColor);
//手工強制清空面板
super.paintComponent(g);
//爲了能夠看到最終效果,所以將緩衝圖像也畫到面板上
g.drawImage(bi,0,0,null);
if(i==0)//line
g.drawLine(xBegin,yBegin,xEnd,yEnd);
else //其他圖形需要判斷起始點和寬高
{
int x,y,width,height;
if(xBegin>xEnd) x = xEnd;
else x = xBegin;
if(yBegin>yEnd) y = yEnd;
else y = yBegin;
width = Math.abs(xBegin-xEnd);//abs取絕對值
height= Math.abs(yBegin-yEnd);
if(i==1)//rect
g.drawRect(x,y,width,height);
else if(i==2)//oval
g.drawOval(x,y,width,height);
}
}
public static void main(String args[])
{
new Huatu();
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e)
{//按下鼠標的時候,記錄開始點的座標
this.xBegin = e.getX();
this.yBegin = e.getY();
}
public void mouseReleased(MouseEvent e)
{//鬆開鼠標,確認最終的圖形需要保存在緩衝圖像上
if(i==0)
gg.drawLine(xBegin,yBegin,xEnd,yEnd);
else //其他圖形需要判斷起始點和寬高
{
int x,y,width,height;
if(xBegin>xEnd) x = xEnd;
else x = xBegin;
if(yBegin>yEnd) y = yEnd;
else y = yBegin;
width = Math.abs(xBegin-xEnd);//abs取絕對值
height= Math.abs(yBegin-yEnd);
if(i==1)//rect
gg.drawRect(x,y,width,height);
else if(i==2)//oval
gg.drawOval(x,y,width,height);
}
}
public void mouseDragged(MouseEvent e)
{//記錄拖動的時候的結束點
this.xEnd = e.getX();
this.yEnd = e.getY();
//每次拖動,都需要重繪面板,顯示最的圖形
this.repaint();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==this.butLine)//線
{
i = 0;
}
else if(e.getSource()==this.butRect)//方
{
i = 1;
}
else if(e.getSource()==this.butOval)//圓
{
i = 2;
}
else if(e.getSource()==this.butOpen)//打開
{
int i = jfc.showOpenDialog(this.f);
if(i==JFileChooser.APPROVE_OPTION)
{
try
{//將文件圖像加載到內存中
bi = ImageIO.read(jfc.getSelectedFile());
//更換圖像以後,畫筆也必須更換爲新的畫筆
gg = bi.getGraphics();
//畫筆默認白色
gg.setColor(myColor);
//爲了防止打開圖片以後,出現一個亂圖,處理一下
this.xBegin = 100;
this.yBegin = 0;
this.xEnd = 0;
this.yEnd = 0;
this.repaint();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
else if(e.getSource()==this.butSave)//保存
{
int i = jfc.showSaveDialog(this.f);
if(i==JFileChooser.APPROVE_OPTION)
{
try
{//保存圖像
//參數 緩衝圖像 類型 文件所在位置
ImageIO.write(bi,"jpg",jfc.getSelectedFile());
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
else if(e.getSource()==butColor)//選擇顏色
{
myColor = JColorChooser.showDialog(f,"選擇你喜歡的顏色",myColor);
gg.setColor(myColor);
}
}
}
注:本Java小程序由"Music-戰車"提供源碼,經過我的修改,如有問題歡迎和我聯繫。郵箱:[email protected]
see Yizero by yizero.com