Java学习—画图程序项目(2)
添加水印,改变鼠标图标,简笔画功能
添加水印所用到方法
方法名 |
方法说明 |
参数 |
参数说明 |
rotate(double theta) |
设置绘图的旋转弧度,让图片旋转 |
double theta |
旋转弧度 |
setFont(Font font) |
设置字体 |
Font font |
字体对象 |
setComposite(Composite comp) |
设置图片绘制像素的方法,这次用来设置透明度 |
Composite comp |
绘图合成借口 |
drawString(String str,intx,inty) |
绘制文字 |
|
要绘制的文字 绘制的横坐标 绘制的纵坐标 |
改添加素材
如图,创建包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)相关推荐
- 简单明了实现Java地图小程序项目
简单明了实现Java地图小程序项目 ✨博主介绍 前言 地图概述 地图技术 地图应用场景 网约车服务 智能穿戴 智能物流 智能景区 车联网 国内常见地图 地图API与搜索 JS API GL(演示百度地 ...
- Java学习笔记_16 项目实战之天天酷跑(三):缓冲加载游戏界面
前文,我们完成了开始游戏界面的搭建.本文将实现缓冲加载界面的搭建.并搭建与前面俩界面间的桥梁. 实现输入正确用户名密码后,进入开始游戏界面,点击开始游戏按钮后,进入缓冲加载界面的功能. 界面示意图: ...
- JAVA简单画图程序
1.JFrame窗体界面(JButton.Graphics.Graphics2D.Color) 创建一个Draw类,在主函数中实例化Draw类,并调用Ondraw方法 public class Dra ...
- java学习-画图小程序
定义容器 java界面开发主要运用了swing和awt库.主要分为容器类和组件类,为了实现一个能与用户交互的界面,往往需要定义一个底层容器-窗体,窗体负责承载组件类.窗体类的常用方法如下. JFram ...
- java学习笔记——众筹项目练习——项目中的忘记密码(密码找回)与记住我(自动登录)功能 和 返回json数据低版本浏览器显示下载问题的解决、众筹系统架构重构
忘记密码(密码找回) 忘记密码(密码找回)这个功能可以说是目前所有为别人提供服务的软件系统都具备的基础功能啦!很普遍,因为总 ...
- Java学习笔记_17 项目实战之天天酷跑(四):游戏主界面
接上文,本文将实现游戏主界面,功能如下: 移动的背景图片.动态的玩家.玩家的移动功能. 五种障碍物持续出现.玩家和障碍物的碰撞. 暂停.继续功能. 首先,看一下整体效果: 动图实在太大,几秒钟的 Gi ...
- Java学习小程序(10)三个等级的才字母游戏
猜字母游戏----实现游戏等级,游戏等级设为三级:5,7,9,代表所需猜测的字母个数 //为猜字母游戏添加游戏等级,游戏等级为5,7,9 import java.util.Scanner; publi ...
- Java学习小程序(6)随机加法运算器
有加法运算器程序,其规则为:程序依次出10道加法题目,由用户输出题目的答案. //随机加法运算器 import java.util.Scanner; public class Addition {pu ...
- Java学习小程序(5)猜数字游戏
猜数字游戏,游戏规则:程序内置一个1到1000之间的数字作为猜测的结果,由用户猜测此数字.分别用 while 和 do-while 语句实现. import java.util.Scanner; pu ...
最新文章
- .net core 程序退出事件
- JavaSE(十九)——equals() 和 == 的区别
- SQL Server:查看数据库用户权限
- 牛X,试用了下GitHub上22万Star的第一抢票神器,3秒钟抢到!
- 数字三角形的显示 java
- 5 EDA技术实用教程【基本语句2】
- 高数笔记(二十一):幂级数,函数展开成幂级数,傅里叶级数
- xshell上传文件到linux很慢,XShell上传文件到Linux服务器上
- #pragma clang diagnostic ignored 忽略警告
- 以艺术教育培养大学生艺术素质
- geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
- USB 之枚举过程概述
- gis平移至所选要素_ArcGIS中如何实现矢量数据平移
- 归并排序(Python)
- 租服务器太贵?流程太麻烦?教你如何免费解决
- (二十一)sift 特征点检测
- 平面直角坐标系中的旋转公式_定比分点公式及定理
- Ubuntu18.04下安装Nvidia驱动和CUDA10.1+CUDNN7.6.5
- 六、RTMP协议 时间戳
- 惠普(HP)笔记本的型号解析