Paint 繪圖板 JAVA 程式設計 指導老師 : 鄞宗賢 組員 : 4A3G0901 劉彥佐 4A3G0907 韓偉志
畫面預覽
匯入參數 package paint; import java.awt.*; import java.awt.event.*; import javax.swing.*;
主程式 public class paint{ public static void main(string[] args){ Icon iconb = new ImageIcon("img/blue.gif"); // 讀取 img 裡面的圖片藍色 Icon iconm = new ImageIcon("img/magenta.gif"); // 讀取 img 裡面的圖片桃紅色 Icon iconr = new ImageIcon("img/red.gif"); // 讀取 img 裡面的圖片紅色 Icon iconbl = new ImageIcon("img/black.gif"); // 讀取 img 裡面的圖片黑色 Icon icong = new ImageIcon("img/green.gif"); // 讀取 img 裡面的圖片綠色
JFrame frame = new JFrame(" 大 yee 術家 "); // 創立一個繪圖的框架 Container content = frame.getcontentpane(); // 創建一個面板 content.setlayout(new BorderLayout()); // 設置佈局為 BorderLayout final PadDraw drawpad = new PadDraw(); // 創建一個新的 paddraw, 這是相當多的畫圖程序 content.add(drawpad, BorderLayout.CENTER); // 放置繪圖在中央 JPanel panel = new JPanel(); // 創建一個 JPanel
設定左方尺寸 panel.setpreferredsize(new Dimension(40, 70));//32 68 panel.setminimumsize(new Dimension(40, 70)); panel.setmaximumsize(new Dimension(45, 70)); // 設置面板的尺寸
JButton redbutton = new JButton(iconR); // 創建紅色按鈕, 並設置創建紅色的圖標 redbutton.addactionlistener(new ActionListener(){ public void actionperformed(actionevent e){ drawpad.red(); ); //ActionListener 介面進行按鈕事件的處理 ActionEvent class 主要處理按鈕被按下的事件
按鈕 JButton blackbutton = new JButton(iconBl); // 黑色按鈕 blackbutton.addactionlistener(new ActionListener(){ public void actionperformed(actionevent e){ drawpad.black(); ); JButton magentabutton = new JButton(iconM); // 桃紅按鈕 magentabutton.addactionlistener(new ActionListener(){ public void actionperformed(actionevent e){ drawpad.magenta(); );
按鈕 JButton bluebutton = new JButton(iconB); // 藍色按鈕 bluebutton.addactionlistener(new ActionListener(){ public void actionperformed(actionevent e){ drawpad.blue(); ); JButton greenbutton = new JButton(iconG); // 綠色按鈕 greenbutton.addactionlistener(new ActionListener(){ public void actionperformed(actionevent e){ drawpad.green(); );
blackbutton.setpreferredsize(new Dimension(16, 16)); magentabutton.setpreferredsize(new Dimension(16, 16)); redbutton.setpreferredsize(new Dimension(16, 16)); bluebutton.setpreferredsize(new Dimension(16, 16)); greenbutton.setpreferredsize(new Dimension(16,16)); // 按鈕的尺寸 panel.add(greenbutton); panel.add(bluebutton); panel.add(magentabutton); panel.add(blackbutton); panel.add(redbutton); // 把 buttons 加進去 panel
content.add(panel, BorderLayout.WEST); // 將 Panel 設置到左側 frame.setsize(500, 450); // 設置 Size 的大小 frame.setdefaultcloseoperation(jframe.exit_on_close); // 按叉叉可以直接關閉視窗 frame.setvisible(true); // 顯示視窗
繪圖 class PadDraw extends JComponent{ Image image; // 宣告要畫的地方 Graphics2D graphics2d; // 繪圖 Graphics2D int currentx, currenty, oldx, oldy; // 宣告座標
座標 public PadDraw(){ setdoublebuffered(false); addmouselistener(new MouseAdapter(){ public void mousepressed(mouseevent e){ oldx = e.getx(); oldy = e.gety(); ); // 按下滑鼠 oldx,oldy // 座標為滑鼠 X 和 Y
滑鼠移動 addmousemotionlistener(new MouseMotionAdapter(){ public void mousedragged(mouseevent e){ currentx = e.getx(); currenty = e.gety(); if(graphics2d!= null) graphics2d.drawline(oldx, oldy, currentx, currenty); repaint(); oldx = currentx; oldy = currenty; ); // 當滑鼠移動設置 currentx,currenty 作為滑鼠 x 和 y // 然後將其繪製在坐標線 // 重新繪製, 並設置 oldx 和 oldy 作為 currentx 和 CurrentY
public void paintcomponent(graphics g){ if(image == null){ image = createimage(getsize().width, getsize().height); graphics2d = (Graphics2D)image.getGraphics(); graphics2d.setrenderinghint(renderinghints.key_antialiasing, RenderingHints.VALUE_ANTIALIAS_ON); clear(); g.drawimage(image, 0, 0, null); // 畫布
public void clear(){ graphics2d.setpaint(color.white); graphics2d.fillrect(0, 0, getsize().width, getsize().height); graphics2d.setpaint(color.black); repaint(); // 空白
public void red(){ graphics2d.setpaint(color.red); repaint(); // 紅色畫筆 public void black(){ graphics2d.setpaint(color.black); repaint(); // 黑色畫筆 public void magenta(){ graphics2d.setpaint(color.magenta); repaint(); // 桃紅畫筆
public void blue(){ graphics2d.setpaint(color.blue); repaint(); // 藍色畫筆 public void green(){ graphics2d.setpaint(color.green); repaint(); // 綠色畫筆
報告結束