日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java学习—画图程序项目(2)

發布時間:2024/1/8 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java学习—画图程序项目(2) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

添加水印,改變鼠標圖標,簡筆畫功能

添加水印所用到方法

方法名

方法說明

參數

參數說明

rotate(double theta)

設置繪圖的旋轉弧度,讓圖片旋轉

double theta

旋轉弧度

setFont(Font font)

設置字體

Font font

字體對象

setComposite(Composite comp)

設置圖片繪制像素的方法,這次用來設置透明度

Composite comp

繪圖合成借口

drawString(String str,intx,inty)

繪制文字

  • String str

  • int x

  • int y

  • 要繪制的文字

    繪制的橫坐標

    繪制的縱坐標

    改添加素材

    如圖,創建包img.icon和img.picture,并且從資源包里復制圖片素材到該包

    DrawPictureFrame.java
    package com.mr.draw;/*** 略略略略略略略*///添加水印 import java.awt.AlphaComposite; import java.awt.Font;//設置字體 import javax.swing.JOptionPane;// //改變圖標功能 import java.awt.Image; import java.awt.Point; import java.awt.Toolkit; import java.awt.Cursor;public class DrawPictureFrame extends JFrame implements FrameGetShape{//繼承窗體類//水印private JMenuItem shuiyinMenuItem;//水印菜單private String shuiyin ="";//水印字符內容//簡筆畫private PictureWindow picWindow;//簡筆畫展示窗體 private JButton showPicButton;//展開簡筆畫/*** 構造方法,添加組件的初始化方法*/public DrawPictureFrame() {setResizable(false); //窗體不能改變大小setTitle("畫圖程序(水印內容:["+ shuiyin+ "])");//設置標題,添加水印內容setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口關閉則停止程序setBounds(500,100,574,460); //設置窗口位置 Bounds:邊界init(); //組件初始化 init();addListener(); //添加組件監聽}//DrawPictureFrame()結束/*** 組件初始化*/private void init() {/*** 略略略略略略略*/toolBar =new JToolBar();//初始化工具欄getContentPane().add(toolBar,BorderLayout.NORTH);//工具欄放至船體北方//簡筆畫放到保存前面showPicButton =new JButton("展開簡筆畫");//初始化按鈕對象,并添加文本內容toolBar.add(showPicButton);工具欄添加按鈕/*** 略略略略略略略*/JMenu systemMenu =new JMenu("系統");//初始化菜單對象,并添加文本內容menuBar.add(systemMenu);//菜單欄添加菜單對象shuiyinMenuItem =new JMenuItem("設置水印");systemMenu.add(shuiyinMenuItem);} }private void addListener() {/*** 略略略略略略略*///mouseDragged結束public void mouseMoved(final MouseEvent arg0) {if (rubber) {//如果使用橡皮//設置鼠標指針的圖形為圖片Toolkit kit = Toolkit.getDefaultToolkit();//獲得系統默認的組件工具包//使用工具包獲得圖片Image img=kit.createImage("src/img/icon/鼠標橡皮.png");//使用工具包創建一個自定義的光標對象//參數為圖片,光標熱點寫成0.0,和鼠標描述字符串Cursor c =kit.createCustomCursor(img,new Point(0,0),"clear");setCursor(c);//使用自定義的光標}else {//設置鼠標指針的形狀為十字光體setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));}}//mouseMoved結束});//addMouseMotion結束toolBar.addMouseMotionListener(new MouseMotionAdapter() {//工具欄添加鼠標移動監聽public void mouseMoved(final MouseEvent arg0) {//設置鼠標指針為默認光標setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); }});//toolBar.addMouseMotionListener結束canvas.addMouseListener(new MouseAdapter() {//畫板添加鼠標單擊事件監聽public void mouseReleased(final MouseEvent arg0) {//當按鍵抬起時x=-1; //將記錄上一次鼠標繪制點的橫坐標恢復成-1y=-1; //將記錄上一次鼠標繪制點的縱坐標恢復成-1}//mouseReleased結束//addMouseListener結束//保存圖片saveButton.addActionListener(new ActionListener() {public void actionPerformed(final ActionEvent argo) {//單擊時addWatermark();//添加水印DrawImageUtil.saveImage(DrawPictureFrame.this,image);//打印圖片:打印當前窗體(DrawPictureFrame.this)為圖片}});saveMenuItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {//單擊時addWatermark();//添加水印DrawImageUtil.saveImage(DrawPictureFrame.this,image);//打印圖片:打印當前窗體(DrawPictureFrame.this)為圖片}});//設置水印shuiyinMenuItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {//單擊時//彈出對話框shuiyin=JOptionPane.showInputDialog(DrawPictureFrame.this,"你想添加的水印:");if (null == shuiyin) {shuiyin ="";//字符串為空內容}else {//如果不是nullsetTitle("畫圖程序(水印內容:[" +shuiyin+"])");//修改窗體標題}}}); }//addlistener()結束/** 添加水印*/private void addWatermark() {if(!"".equals(shuiyin.trim())) {//如果水印字段不是空字符串g.rotate(Math.toRadians(-30));//將照片旋轉-30Font font =new Font("楷體",Font.BOLD,72);//設置字體g.setFont(font);//載入字體g.setColor(Color.GRAY);//使用灰色AlphaComposite alpha=AlphaComposite.SrcOver.derive(0.4f);//設置透明效果g.setComposite(alpha);//使用透明效果g.drawString(shuiyin,150,500);//繪制文字canvas.repaint();//畫板重繪g.rotate(Math.toRadians(30));//將旋轉的圖片轉回來alpha =AlphaComposite.SrcOver.derive(1f);//不透明g.setComposite(alpha);//使用不透明效果g.setColor(foreColor);//畫筆恢復之前顏色}//if結束}
    添加簡筆畫

    創建PictureWindow.java

    package com.mr.draw; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Image; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JWindow; import com.mr.util.BackgroundPanel;/** 簡筆畫展示窗體*/ public class PictureWindow extends JWindow { private JButton changeButton; 更換圖片按鈕private JButton hiddenButton; //隱藏按鈕private BackgroundPanel centerPanel;//展示圖片的帶背景圖畫板File list[];//當前選中的圖片索引int index; //父窗體DrawPictureFrame frame;/** 構造方法* @param frame父窗體*/public PictureWindow(DrawPictureFrame frame) {this.frame =frame;//構造參數的值賦值給父窗口setSize(400,460); //設置窗體寬高init(); //初始化窗體組件addListener(); //給組件添加監聽}//PictureWindow/** 組件初始化方法*/private void init() {Container c=getContentPane(); //獲取窗體主容器File dir =new File("src/img/picture"); //創建簡筆畫素材文件夾對象list =dir.listFiles();//獲取文件夾里的所有文件//初始化背景面板,使用圖片文件夾里的第一張簡筆畫centerPanel =new BackgroundPanel(getListImage());c.add(centerPanel,BorderLayout.CENTER);FlowLayout flow =new FlowLayout(FlowLayout.RIGHT);//創建右對齊的流布局flow.setHgap(20);//水平間隔20像素JPanel southPanel =new JPanel();//創建南部面板southPanel.setLayout(flow);//南部面板使用剛才創建好的流布局changeButton =new JButton("更換圖片");//實例化"更換圖片"按鈕southPanel.add(changeButton);//南部面板添加按鈕hiddenButton =new JButton("隱藏");//實例化"隱藏"按鈕southPanel.add(hiddenButton);//南部面板添加按鈕c.add(southPanel,BorderLayout.SOUTH);//南部面板放到主容器的南部位置位置}//init結束/** 添加監聽*/private void addListener() {hiddenButton.addActionListener(new ActionListener() {//隱藏按鈕添加動作監聽public void actionPerformed(ActionEvent e) {//單擊時setVisible(false);//本窗體不可見frame.initShowPicButton();//父窗體還原簡筆畫按鈕文本的內容}});//hiddenButton結束changeButton.addActionListener(new ActionListener() {//隱藏按鈕添加動作監聽public void actionPerformed(ActionEvent e) {//單擊時centerPanel.setImage(getListImage());//背景面板重新載入圖片}});//changeButton.addActionListener結束}//addListeners結束/** 獲取圖片文件夾下的圖片,每次調用此方法,都會獲得不同的文件對象*/private Image getListImage() {String imgPath =list[index].getAbsolutePath();//獲取當前索引下的圖片文件路徑ImageIcon image =new ImageIcon(imgPath);//獲取此圖片文件的圖標對象index++;//索引變量遞增if(index >=list.length) {//如果索引變量超過圖片數量index =0;//索引歸零}return image.getImage();//獲得圖標對象的圖片對象}//getListImage結束}//PictureWindow 結束
    功能實現
    按鈕美化

    DrawPictureFrame.java

    package com.mr.draw;/*** 略略略略略略略*/ import javax.swing.ImageIcon;//后面的直接復制粘貼了//引入窗體類//alt+/ 快速導入public class DrawPictureFrame extends JFrame implements FrameGetShape{//繼承窗體類//創建一個8位BGR顏色分量的圖像BufferedImage image =new BufferedImage(570,390,BufferedImage.TYPE_INT_BGR);Graphics gs=image.getGraphics();//獲得圖像的繪畫對象Graphics2D g=(Graphics2D) gs; //獲得繪畫對象轉換為Graphics2D類型DrawPictureCanvas canvas =new DrawPictureCanvas();//創建畫布對象Color foreColor=Color.BLACK;//定義前景色 棕色Color backgroundColor =Color.WHITE; //定義背景色 白色//鼠標事件int x=-1; //上次鼠標繪制點的橫坐標int y=-1; //上次鼠標繪制點的縱坐標boolean rubber=false; //橡皮標識變量private JToolBar toolBar;//工具欄private JButton eraserButton;//橡皮private JToggleButton strokeButton1;//細線按鈕private JToggleButton strokeButton2;//粗線按鈕private JToggleButton strokeButton3;//較粗按鈕private JButton backgroundButton;//背景色按鈕private JButton foregroundButton;//前背景色按鈕private JButton clearButton;//清除按鈕private JButton saveButton;//保存按鈕private JButton shapeButton;//圖像按鈕boolean drawShape =false;//畫圖形標志變量Shapes shape;//繪制圖形//菜單private JMenuItem strokeMenuItem1;//細線菜單private JMenuItem strokeMenuItem2;//粗線菜單private JMenuItem strokeMenuItem3;//較粗private JMenuItem clearMenuItem;//清除菜單private JMenuItem foregroundMenuItem;//前景菜單private JMenuItem backgroundMenuItem; private JMenuItem eraserMenuItem;//橡皮菜單private JMenuItem exitMenuItem;//跳出菜單private JMenuItem saveMenuItem;//保存菜單//水印private JMenuItem shuiyinMenuItem;//水印菜單private String shuiyin ="";//水印字符內容//簡筆畫private PictureWindow picWindow;//簡筆畫展示窗體 private JButton showPicButton;//展開簡筆畫/*** 構造方法,添加組件的初始化方法*/public DrawPictureFrame() {setResizable(false); //窗體不能改變大小setTitle("畫圖程序(水印內容:["+ shuiyin+ "])");//設置標題,添加水印內容setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口關閉則停止程序setBounds(500,100,574,460); //設置窗口位置 Bounds:邊界init(); //組件初始化 init();addListener(); //添加組件監聽}//DrawPictureFrame()結束/*** 組件初始化*/private void init() {g.setColor(backgroundColor);//用背景色設置繪圖對象的顏色g.fillRect(0, 0, 570, 390);//使用背景色填充整個畫布g.setColor(foreColor); //設置畫布的圖像canvas.setImage(image);//設置畫布的圖像getContentPane().add(canvas);//將畫布添加到窗體容器默認布局的中部位置toolBar =new JToolBar();//初始化工具欄getContentPane().add(toolBar,BorderLayout.NORTH);//工具欄放至船體北方//簡筆畫//showPicButton =new JButton("展開簡筆畫");//初始化按鈕對象,并添加文本內容showPicButton =new JButton();showPicButton.setToolTipText("展開簡筆畫");//設置鼠標懸停提示showPicButton.setIcon(new ImageIcon("src/img/icon/展開.png"));//設置鼠標圖案toolBar.add(showPicButton);工具欄添加按鈕//saveButton=new JButton("保存");//初始化按鈕對象,并添加文本內容saveButton=new JButton();saveButton.setToolTipText("保存");saveButton.setIcon(new ImageIcon("src/img/icon/保存.png"));toolBar.add(saveButton);//工具欄添加按鈕toolBar.addSeparator();//添加分割線//初始化有選中狀態的按鈕對象,并添加文本strokeButton1 =new JToggleButton();strokeButton1.setToolTipText("細線");strokeButton1.setIcon(new ImageIcon("src/img/icon/1像素線條.png"));strokeButton1.setSelected(true);//細線按鈕處于選中狀態toolBar.add(strokeButton1);//工具欄添加按鈕strokeButton2 =new JToggleButton();strokeButton2.setToolTipText("粗線");strokeButton2.setIcon(new ImageIcon("src/img/icon/2像素線條.png"));toolBar.add(strokeButton2);//工具欄添加按鈕strokeButton3 =new JToggleButton();strokeButton3.setToolTipText("較粗");strokeButton3.setIcon(new ImageIcon("src/img/icon/4像素線條.png"));//把上面三個按順序,畫筆粗細按鈕組,保證同時只有一個按鈕被選中ButtonGroup strokeGroup =new ButtonGroup();strokeGroup.add(strokeButton1);//按鈕組添加按鈕strokeGroup.add(strokeButton2);strokeGroup.add(strokeButton3);toolBar.add(strokeButton3);//工具欄添加按鈕toolBar.addSeparator();//添加分割線backgroundButton=new JButton("");//初始化按鈕對象,并添加文本內容backgroundButton.setToolTipText("背景顏色");backgroundButton.setIcon(new ImageIcon("src/img/icon/背景色.png"));toolBar.add(backgroundButton);//工具欄添加按鈕foregroundButton =new JButton();foregroundButton.setToolTipText("畫筆顏色");foregroundButton.setIcon(new ImageIcon("src/img/icon/前景色.png"));toolBar.add(foregroundButton);toolBar.addSeparator();//添加分割線shapeButton =new JButton();shapeButton.setToolTipText("圖形");shapeButton.setIcon(new ImageIcon("src/img/icon/形狀.png"));toolBar.add(shapeButton);clearButton =new JButton();clearButton.setToolTipText("清除");clearButton.setIcon(new ImageIcon("src/img/icon/清除.png"));toolBar.add(clearButton);eraserButton =new JButton();eraserButton.setToolTipText("橡皮");eraserButton.setIcon(new ImageIcon("src/img/icon/橡皮.png"));toolBar.add(eraserButton);//工具欄添加按鈕//添加菜單欄JMenuBar menuBar=new JMenuBar();//創建菜單欄setJMenuBar(menuBar);//窗體載入菜單欄//系統內容JMenu systemMenu =new JMenu("系統");//初始化菜單對象,并添加文本內容menuBar.add(systemMenu);//菜單欄添加菜單對象shuiyinMenuItem =new JMenuItem("設置水印");systemMenu.add(shuiyinMenuItem);saveMenuItem=new JMenuItem("保存");systemMenu.add(saveMenuItem);//菜單添加菜單項//創建簡筆畫展示畫筆,并將本類當作它的父窗體picWindow =new PictureWindow(DrawPictureFrame.this);systemMenu.addSeparator();//添加分割線exitMenuItem =new JMenu("退出");//systemMenu.add(exitMenuItem);JMenu strokeMenu=new JMenu("線型");//初始菜單對象,并添加文本內容menuBar.add(strokeMenu);//菜單添加菜單對象strokeMenuItem1 =new JMenuItem("細線");strokeMenu.add(strokeMenuItem1);strokeMenuItem2 =new JMenuItem("粗線");strokeMenu.add(strokeMenuItem2);strokeMenuItem3 =new JMenuItem("較粗");strokeMenu.add(strokeMenuItem3);JMenu colorMenu =new JMenu("顏色");menuBar.add(colorMenu);foregroundMenuItem=new JMenuItem("畫筆顏色");colorMenu.add(foregroundMenuItem);backgroundMenuItem=new JMenuItem("背景顏色");colorMenu.add(backgroundMenuItem);JMenu editMenu =new JMenu("編輯");menuBar.add(editMenu);clearMenuItem =new JMenuItem("清除");editMenu.add(clearMenuItem);eraserMenuItem =new JMenuItem("橡皮");editMenu.add(eraserMenuItem);}//init()結束/*** 為組件添加鼠標移動事件監聽*/private void addListener() {//畫板添加鼠標移動事件監聽canvas.addMouseMotionListener(new MouseMotionAdapter() {public void mouseDragged(final MouseEvent e) { //當鼠標拖拽時if(x>0 && y>0) { //如果x和y存在鼠標記錄if(rubber) { //橡皮標識為true,表示使用橡皮g.setColor(backgroundColor); //繪圖工具使用背景色g.fillRect(x, y,10,10);//在鼠標劃過的位置畫填充的正方形}else {g.drawLine(x, y,e.getX(),e.getY());//在鼠標劃過的位置畫直線}}//if結束x=e.getX();y=e.getY();canvas.repaint(); //更新畫布}public void mouseMoved(final MouseEvent arg0) {if (rubber) {//如果使用橡皮//設置鼠標指針的圖形為圖片Toolkit kit = Toolkit.getDefaultToolkit();//獲得系統默認的組件工具包//使用工具包獲得圖片Image img=kit.createImage("src/img/icon/鼠標橡皮.png");//使用工具包創建一個自定義的光標對象//參數為圖片,光標熱點寫成0.0,和鼠標描述字符串Cursor c =kit.createCustomCursor(img,new Point(0,0),"clear");setCursor(c);//使用自定義的光標}else {//設置鼠標指針的形狀為十字光體setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));}}//mouseMoved結束});//addMouseMotion結束toolBar.addMouseMotionListener(new MouseMotionAdapter() {//工具欄添加鼠標移動監聽public void mouseMoved(final MouseEvent arg0) {//設置鼠標指針為默認光標setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); }});//toolBar.addMouseMotionListener結束canvas.addMouseListener(new MouseAdapter() {//畫板添加鼠標單擊事件監聽public void mouseReleased(final MouseEvent arg0) {//當按鍵抬起時x=-1; //將記錄上一次鼠標繪制點的橫坐標恢復成-1y=-1; //將記錄上一次鼠標繪制點的縱坐標恢復成-1}//mouseReleased結束//添加鼠標按下時觸發的方法,圖形public void mousePressed(MouseEvent e) {if (drawShape) {//如果此時我鼠標畫的是圖形switch(shape.getType()) {//判斷圖形的種類case Shapes.YUAN://如果是圓形//計算坐標,讓鼠標處于圖形的中心位置int yuanX =e.getX() -shape.getWidth()/2;int yuanY =e.getY() -shape.getHeigth()/2;//創建圓形,并指定坐標和寬高Ellipse2D yuan=new Ellipse2D.Double(yuanX,yuanY,shape.getWidth(),shape.getHeigth());g.draw(yuan);//畫圖工具畫此圓形break;case Shapes.FANG://計算坐標,讓鼠標處于圖形的中心位置int fangX =e.getX() -shape.getWidth()/2;int fangY =e.getY() -shape.getHeigth()/2;//創建方形,并指定坐標和寬高Rectangle2D fang =new Rectangle2D.Double(fangX,fangY,shape.getWidth(),shape.getHeigth());g.draw(fang);break;}canvas.repaint();//更新畫筆//畫圖形標識變量為false ,說明選擇鼠標畫的是圖形drawShape=false;//畫完圖形之后,回到畫筆狀態}}//圖形結束});//結束canvas.addMouseListenerstrokeButton1.addActionListener(new ActionListener() {//"細線“按鈕添加動作監聽public void actionPerformed(final ActionEvent arg0) {//單擊時//聲明畫筆屬性,粗細為1像素,線條末端無修飾,折線處呈尖角BasicStroke bs =new BasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);g.setStroke(bs);//畫圖工具使用此畫筆}});strokeButton2.addActionListener(new ActionListener() {//"細線“按鈕添加動作監聽public void actionPerformed(final ActionEvent arg0) {//單擊時//聲明畫筆屬性,粗細為2像素,線條末端無修飾,折線處呈尖角BasicStroke bs =new BasicStroke(2,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);g.setStroke(bs);//畫圖工具使用此畫筆}});strokeButton3.addActionListener(new ActionListener() {//"細線“按鈕添加動作監聽public void actionPerformed(final ActionEvent arg0) {//單擊時//聲明畫筆屬性,粗細為3像素,線條末端無修飾,折線處呈尖角BasicStroke bs =new BasicStroke(3,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);g.setStroke(bs);//畫圖工具使用此畫筆}});//背景顏色backgroundButton.addActionListener(new ActionListener() {//背景色按鈕添加動作監聽:ActionListenerpublic void actionPerformed(final ActionEvent arg0) {//單擊時打開顏色對話框,參數依次為:父窗體,標題,默認選中的顏色(青色)Color bgColor =JColorChooser.showDialog(DrawPictureFrame.this,"顏色對話框",Color.CYAN);if (bgColor !=null) {//如果選中的顏色不是空的backgroundColor=bgColor;//將選中的顏色賦給背景色變量}//背景色按鈕也更換為這種顏色backgroundButton.setBackground(backgroundColor);g.setColor(backgroundColor);//繪圖工具使用顏色g.fillRect(0,0,570,390);//畫一個背景顏色的方形填滿整個畫布g.setColor(foreColor);//畫圖工具使用前景色canvas.repaint();//更新畫布}});//backgroundButton.addActionListener結束//畫筆顏色--前景色foregroundButton.addActionListener(new ActionListener() {//背景色按鈕添加動作監聽:ActionListenerpublic void actionPerformed(final ActionEvent arg0) {//單擊時打開顏色對話框,參數依次為:父窗體,標題,默認選中的顏色(青色)Color fColor =JColorChooser.showDialog(DrawPictureFrame.this,"顏色對話框",Color.CYAN);if (fColor !=null) {//如果選中的顏色不是空的backgroundColor=fColor;//將選中的顏色賦給背景色變量}//背景色按鈕也更換為這種顏色foregroundButton.setBackground(foreColor);g.setColor(foreColor);//繪圖工具使用顏色}});//清除與橡皮按鈕事件實現clearButton.addActionListener(new ActionListener() {public void actionPerformed(final ActionEvent arg0) {g.setColor(backgroundColor);g.fillRect(0,0,570,390);//畫一個背景顏色的方形填滿整個畫布,實現清除功能g.setColor(foreColor);//畫圖工具使用畫筆顏色canvas.repaint();//更新畫布}});eraserButton.addActionListener(new ActionListener() {public void actionPerformed(final ActionEvent arg0) {if(rubber){//單擊按鈕,使用橡皮eraserButton.setToolTipText("橡皮");//鼠標懸停顯示//設置鼠標圖案eraserButton.setIcon(new ImageIcon("src/img/icon/橡皮.png"));eraserMenuItem.setText("橡皮");g.setColor(foreColor);rubber = false;}else {eraserMenuItem.setText("畫圖");eraserButton.setToolTipText("畫圖");//設置鼠標按鈕eraserButton.setIcon(new ImageIcon("src/img/icon/畫筆.png"));g.setColor(backgroundColor);rubber =true;}}});//橡皮結束//圖形shapeButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {//單擊時ShapeWindow shapeWindow=new ShapeWindow(DrawPictureFrame.this);//創建圖形選擇組件int shapeButtonWidth = shapeButton.getWidth();//獲取圖形按鈕寬度int shapeWindowWidth = shapeWindow.getWidth();//獲取圖形按鈕高度int shapeButtonX =shapeButton.getX();//獲取圖形按鈕橫坐標int shapeButtonY =shapeButton.getY();//獲取圖形按鈕縱坐標//計算圖形組件橫坐標,讓組件與“圖形”按鈕居中對齊int shapeWindowX =getX() +shapeButtonX-(shapeWindowWidth -shapeButtonWidth)/2;//計算圖形組件縱坐標,讓組件與“圖形”按鈕下方int shapeWindowY =getY()+shapeButtonY+80;//設置圖形組件坐標位置shapeWindow.setLocation(shapeWindowX,shapeWindowY);shapeWindow.setVisible(true);//圖形可見}});//圖形結束//保存圖片saveButton.addActionListener(new ActionListener() {public void actionPerformed(final ActionEvent argo) {//單擊時addWatermark();//添加水印DrawImageUtil.saveImage(DrawPictureFrame.this,image);//打印圖片:打印當前窗體(DrawPictureFrame.this)為圖片}});/*** 菜單監聽,基本和前面的一樣,修改為saveMenuItem.*/saveMenuItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {//單擊時addWatermark();//添加水印DrawImageUtil.saveImage(DrawPictureFrame.this,image);//打印圖片:打印當前窗體(DrawPictureFrame.this)為圖片}});//設置水印shuiyinMenuItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {//單擊時//彈出對話框shuiyin=JOptionPane.showInputDialog(DrawPictureFrame.this,"你想添加的水印:");if (null == shuiyin) {shuiyin ="";//字符串為空內容}else {//如果不是nullsetTitle("畫圖程序(水印內容:[" +shuiyin+"])");//修改窗體標題}}});//清除與橡皮按鈕事件實現clearMenuItem.addActionListener(new ActionListener() {public void actionPerformed(final ActionEvent arg0) {g.setColor(backgroundColor);g.fillRect(0,0,570,390);//畫一個背景顏色的方形填滿整個畫布,實現清除功能g.setColor(foreColor);//畫圖工具使用畫筆顏色canvas.repaint();//更新畫布}});eraserMenuItem.addActionListener(new ActionListener() {public void actionPerformed(final ActionEvent arg0) {if(rubber) {eraserButton.setToolTipText("橡皮");//鼠標懸停顯示eraserButton.setIcon(new ImageIcon("src/img/icon/橡皮.png"));eraserMenuItem.setText("橡皮");g.setColor(foreColor);rubber =false;}else {eraserButton.setToolTipText("畫圖");//鼠標懸停顯示eraserButton.setIcon(new ImageIcon("src/img/icon/畫筆.png"));eraserMenuItem.setText("畫圖");g.setColor(backgroundColor);rubber =true;}}});//橡皮結束//背景顏色backgroundMenuItem.addActionListener(new ActionListener() {//背景色按鈕添加動作監聽:ActionListenerpublic void actionPerformed(final ActionEvent arg0) {//單擊時打開顏色對話框,參數依次為:父窗體,標題,默認選中的顏色(青色)Color bgColor =JColorChooser.showDialog(DrawPictureFrame.this,"顏色對話框",Color.CYAN);if (bgColor !=null) {//如果選中的顏色不是空的backgroundColor=bgColor;//將選中的顏色賦給背景色變量}//背景色按鈕也更換為這種顏色backgroundButton.setBackground(backgroundColor);g.setColor(backgroundColor);//繪圖工具使用顏色g.fillRect(0,0,570,390);//畫一個背景顏色的方形填滿整個畫布g.setColor(foreColor);//畫圖工具使用前景色canvas.repaint();//更新畫布}});//backgroundButton.addActionListener結束//畫筆顏色--前景色foregroundMenuItem.addActionListener(new ActionListener() {//背景色按鈕添加動作監聽:ActionListenerpublic void actionPerformed(final ActionEvent arg0) {//單擊時打開顏色對話框,參數依次為:父窗體,標題,默認選中的顏色(青色)Color bgColor =JColorChooser.showDialog(DrawPictureFrame.this,"顏色對話框",Color.CYAN);if (bgColor !=null) {//如果選中的顏色不是空的backgroundColor=bgColor;//將選中的顏色賦給背景色變量}//背景色按鈕也更換為這種顏色foregroundButton.setBackground(foreColor);//畫筆顏色也更換為這種顏色g.setColor(backgroundColor);//繪圖工具使用顏色}}); strokeMenuItem1.addActionListener(new ActionListener() {//"細線“按鈕添加動作監聽public void actionPerformed(final ActionEvent arg0) {//單擊時//聲明畫筆屬性,粗細為1像素,線條末端無修飾,折線處呈尖角BasicStroke bs =new BasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);g.setStroke(bs);//畫圖工具使用此畫筆strokeButton1.setSelected(true);//細線按鈕為選中狀態}});strokeMenuItem2.addActionListener(new ActionListener() {//"細線“按鈕添加動作監聽public void actionPerformed(final ActionEvent arg0) {//單擊時//聲明畫筆屬性,粗細為2像素,線條末端無修飾,折線處呈尖角BasicStroke bs =new BasicStroke(2,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);g.setStroke(bs);//畫圖工具使用此畫筆strokeButton2.setSelected(true);}});strokeMenuItem3.addActionListener(new ActionListener() {//"細線“按鈕添加動作監聽public void actionPerformed(final ActionEvent arg0) {//單擊時//聲明畫筆屬性,粗細為3像素,線條末端無修飾,折線處呈尖角BasicStroke bs =new BasicStroke(3,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);g.setStroke(bs);//畫圖工具使用此畫筆strokeButton3.setSelected(true);}});//畫筆結束//簡筆畫showPicButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {boolean isVisible =picWindow.isVisible();//獲取簡筆畫展示窗體可見狀態if(isVisible){//如果簡筆畫展示窗體是可見的showPicButton.setToolTipText("展開簡筆畫");showPicButton.setIcon(new ImageIcon("src/img/icon/展開.png"));picWindow.setVisible(false);//隱藏簡筆畫展示窗體}else {showPicButton.setToolTipText("隱藏簡筆畫");showPicButton.setIcon(new ImageIcon("src/img/icon/隱藏.png"));//重新指定簡筆畫展示窗體的顯示位置//橫坐標 =主窗體橫坐標 -簡筆畫窗體寬度-5//縱坐標 =主窗體縱坐標picWindow.setLocation(getX()-picWindow.getWidth()-5,getY());picWindow.setVisible(true);//簡筆畫展示窗體可見}}});//showPicButton.addActionListener結束}//組件監聽結束/** 恢復展開簡筆畫按鈕的文本內容,此方法共簡筆畫畫筆的”隱藏“按鈕可調用*/public void initShowPicButton() {showPicButton.setToolTipText("展開簡筆畫");showPicButton.setIcon(new ImageIcon("src/img/icon/展開.png"));}//initShowPicButton結束/** 添加水印*/private void addWatermark() {if(!"".equals(shuiyin.trim())) {//如果水印字段不是空字符串g.rotate(Math.toRadians(-30));//將照片旋轉-30Font font =new Font("楷體",Font.BOLD,72);//設置字體g.setFont(font);//載入字體g.setColor(Color.GRAY);//使用灰色AlphaComposite alpha=AlphaComposite.SrcOver.derive(0.4f);//設置透明效果g.setComposite(alpha);//使用透明效果g.drawString(shuiyin,150,500);//繪制文字canvas.repaint();//畫板重繪g.rotate(Math.toRadians(30));//將旋轉的圖片轉回來alpha =AlphaComposite.SrcOver.derive(1f);//不透明g.setComposite(alpha);//使用不透明效果g.setColor(foreColor);//畫筆恢復之前顏色}//if結束}//FrameGetShape接口實現類,用于獲得圖形空間返回的被選中的圖形@Overridepublic void getShape(Shapes shape) {this.shape =shape;//將反回的圖形對象賦給類的全局變量drawShape =true; //畫圖形標識變量為true,聲明現在鼠標畫的是圖形,而不是線條 }//結束public static void main(String[] args) {DrawPictureFrame frame=new DrawPictureFrame();//創建窗體對象frame.setVisible(true);//讓窗口可見}//main()結束//主類結束}

    好啦,本次項目就做到這里啦,期待下一項吧~

    總結

    以上是生活随笔為你收集整理的Java学习—画图程序项目(2)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。