赞
踩
Win10,64位操作系统
如何下载和安装JDK,配置系统的环境变量。
登录www.oracle.com下载
甲骨文官网:https://www.oracle.com/cn/index.html
甲骨文官网→资源→软件下载→Java→面向开发人员的 Java (JDK)
→ JDK Download(这里是Java SE 15为例,可下最新版)
→下载Windows x64 Installer(找到自己对应操作系统的,本文win10,64位为例,点击jdk-15.0.1_windows-x64_bin.exe)
→下载好jdk-15.0.1_windows-x64_bin.exe后点击启动
→无脑安装,默认设置,(小白没必要可以不改默认安装路径)安装完毕关闭即可
1.甲骨文官网→资源
2.软件下载
3.Java
4.面向开发人员的 Java (JDK)
6.下载Windows x64 Installer(找到自己对应操作系统的,本文win10,64位为例,点击jdk-15.0.1_windows-x64_bin.exe),现在一般都是Window的64位操作系统,详细看自己电脑属性。
7.下载好jdk-15.0.1_windows-x64_bin.exe后点击启动,下载好后去自己浏览器的下载找到已经下载好的jdk,点击安装就行了,默认设置。
8.无脑安装,默认设置,(小白没必要的话建议不改默认安装路径)安装完毕关闭即可。最好使用默认路径,后边系统环境变量要用,不推荐改。安装完点关闭就可以了
1.找到环境变量:
文件→此电脑→右键打开属性→高级系统设置→环境变量
(实在找不到,直接在win10的搜索框搜索)
1.配置JAVA_HOME环境变量
点击下面系统变量的新建(不是用户变量!)
变量名填 JAVA_HOME
变量值填C:\ProgramFiles\Java\jdk-15.0.1
(注意:这是我的jdk版本,你要选着自己的jdk安装路径,如果上面是安装默认的话,去C盘,ProgramFiles,Java,里面的你自己的jdk安装路径,也可以点浏览目录,选择jdk文件夹确定就行了。)
点击确定。
2.配置Path环境变量
在系统变量找到Path,双击Path
3.进去后点击右上角的新建
4.在输入框中输入%JAVA_HOME%\bin
输入完记得点确定
5.再次点击新建(也可以重新双击Path),输入%JAVA_HOME%\jre\bin
点确定
3.配置CLASSPATH环境变量
4.注意.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar;这段变量值前面是有一个小点符号“.”和一个分号的“;” 不要漏掉。记得点确认,就配置好了。
配置好后出现变量和值
出现这些java使用说明,那么你的环境变量就配置成功了。
3.输入javac命令,按Enter执行。
环境变量配置成功。
下面展示一些 内联代码片
。
配置好环境变量后,直接下载Eclipse安装包,默认设置安装即可。
eclipse-java-2020-09-R-win32-x86_64
这里不详细介绍,可以自行百度一下。
一起画一画入门实战代码参考
javax.swing.JFrame类
package com.mr.draw; import javax.swing.JFrame;//引入窗口类,如果不含类,先把module-info.java删掉 import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage;//子类具有可访问图像数据缓冲区的image import java.awt.event.MouseAdapter;//鼠标监听事件 import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter;//空类 import java.awt.BorderLayout; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JToggleButton; import javax.swing.JToolBar; import java.awt.BasicStroke;//按钮 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JColorChooser;//颜色 import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; import com.mr.util.FrameGetShape; import com.mr.util.ShapeWindow; import com.mr.util.Shapes; import com.mr.util.DrawImageUtil; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; 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 { // 继承窗口类 // 创建一个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);// 设置窗口位置和宽高 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("展开简笔画");// 初始化按钮对象,并添加文本内容 toolBar.add(showPicButton);// 工具栏添加按钮 saveButton = new JButton("保存");// 初始化按钮对象,并添加文本内容 toolBar.add(saveButton);// 工具栏添加按钮 toolBar.addSeparator();// 添加分割条 // 初始化有选中状态的按钮对象,并添加文本内容 strokeButton1 = new JToggleButton("细线"); strokeButton1.setSelected(true);// 细线按钮处于被选中状态 toolBar.add(strokeButton1);// 工具栏添加按钮 // 初始化有选中状态的按钮对象,并添加文本内容 strokeButton2 = new JToggleButton("粗线"); toolBar.add(strokeButton2);// 工具栏添加按钮 // 初始化有选中状态的按钮对象,并添加文本内容 strokeButton3 = new JToggleButton("较粗"); // 画笔粗细按钮组,保证同时只有一个按钮被选中 ButtonGroup strokeGroup = new ButtonGroup(); strokeGroup.add(strokeButton1);// 按钮组添加按钮 strokeGroup.add(strokeButton2);// 按钮组添加按钮 strokeGroup.add(strokeButton3);// 按钮组添加按钮 toolBar.add(strokeButton3);// 工具栏添加按钮 toolBar.addSeparator();// 添加分割 backgroundButton = new JButton("背景颜色");// 初始化按钮对象,并添加文本内容 toolBar.add(backgroundButton);// 工具栏添加按钮 foregroundButton = new JButton("前景颜色");// 初始化按钮对象,并添加文本内容 toolBar.add(foregroundButton);// 工具栏添加按钮 toolBar.addSeparator();// 添加分割条 shapeButton = new JButton("图形"); toolBar.add(shapeButton); clearButton = new JButton("清 除");// 初始化按钮对象,并添加文本内容 toolBar.add(clearButton);// 工具栏添加按钮 eraserButton = new JButton("橡皮");// 初始化按钮对象,并添加文本内容 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);// 菜单添加菜单项 systemMenu.addSeparator();// 添加分割条 exitMenuItem = new JMenuItem("退出");// 初始化菜单项对象,并添加文本内容 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);// 菜单添加菜单项 // 创建简笔画展示面板,并将本类当作它的父窗体 picWindow = new PictureWindow(DrawPictureFrame.this); } 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 { // 如果橡皮标识为false,表示用画笔画图 g.drawLine(x, y, e.getX(), e.getY());// 在鼠滑过的位置画直线 } // else结束 } // if结束 x = e.getX(); // 上一次鼠标绘制点的横坐标 y = e.getY(); // 上一次鼠标绘制点的纵坐标 canvas.repaint(); // 更新画布 }// 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)); } // else结束 }// mouseMoved()结束 });// canvas.addMouseMotionListener()结束 canvas.addMouseListener(new MouseAdapter() {// 画板添加鼠标单击事件监听 public void mouseReleased(final MouseEvent arg0) {// 当按键抬起时 x = -1; // 将记录上一次鼠标绘制点的横坐标恢复成-1 y = -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;// 结束switch语句 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;// 结束switch语句 }// switch结束 canvas.repaint(); // 更新画布 drawShape = false; } // if结束 }// mousePressed()结束 });// canvas.addMouseListener()结束 toolBar.addMouseMotionListener(new MouseMotionAdapter() {// 工具栏添加鼠标移动监听 public void mouseMoved(final MouseEvent arg0) {// 当鼠标移动时 // 设置鼠标指针的形状为默认光标 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); }// mouseMoved() });// toolBar.addMouseMotionListener()结束 strokeButton1.addActionListener(new ActionListener() {// “细线”按钮添加动作监听 public void actionPerformed(final ActionEvent arg0) {// 单击时 // 声明画笔的属性,粗细为1像素,线条末端无修饰,折线处呈尖角 BasicStroke bs = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER); g.setStroke(bs); // 画图工具使用此画笔 }// actionPerformed()结束 });// strokeButton1.addActionListener()结束 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); // 画图工具使用此画笔 }// actionPerformed()结束 });// strokeButton2.addActionListener()结束 strokeButton3.addActionListener(new ActionListener() {// “较粗”按钮添加动作监听 public void actionPerformed(final ActionEvent arg0) { // 声明画笔的属性,粗细为4像素,线条末端无修饰,折线处呈尖角 BasicStroke bs = new BasicStroke(4, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER); g.setStroke(bs); // 画图工具使用此画笔 }// actionPerformed()结束 });// strokeButton3.addActionListener()结束 backgroundButton.addActionListener(new ActionListener() {// 背景颜色按钮添加动作监听 public 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(); // 更新画布 }// actionPerformed()结束 });// backgroundButton.addActionListener()结束 foregroundButton.addActionListener(new ActionListener() {// 前景色颜色按钮添加动作监听 public void actionPerformed(final ActionEvent arg0) {// 单击时 // 打开选择颜色对话框,参数依次为:父窗体、标题、默认选中的颜色(青色) Color fColor = JColorChooser.showDialog(DrawPictureFrame.this, "选择颜色对话框", Color.CYAN); if (fColor != null) {// 如果选中的颜色不是空的 foreColor = fColor;// 将选中的颜色赋给前景色变量 } // 前景色按钮的文字也更换为这种颜色 foregroundButton.setForeground(foreColor); g.setColor(foreColor); // 绘图工具使用前景色 }// actionPerformed()结束 });// foregroundButton.addActionListener()结束 clearButton.addActionListener(new ActionListener() {// 清除按钮添加动作监听 public void actionPerformed(final ActionEvent arg0) {// 单击时 g.setColor(backgroundColor); // 绘图工具使用背景色 g.fillRect(0, 0, 570, 390); // 画一个背景颜色的方形填满整个画布 g.setColor(foreColor); // 绘图工具使用前景色 canvas.repaint(); // 更新画布 }// actionPerformed()结束 });// eraserButton.addActionListener()结束 eraserButton.addActionListener(new ActionListener() {// 橡皮按钮添加动作监听 public void actionPerformed(final ActionEvent arg0) {// 单击时 // 单击工具栏上的橡皮按钮,使用橡皮 if (eraserButton.getText().equals("橡皮")) { rubber = true; // 设置橡皮标识为true eraserButton.setText("画图"); // 改变按钮上显示的文本为画图 } else { // 单击工具栏上的画图按钮,使用画笔 rubber = false; // 设置橡皮标识为false eraserButton.setText("橡皮"); // 改变按钮上显示的文本为橡皮 g.setColor(foreColor); // 设置绘图对象的前景色 } // else结束 }// actionPerformed()结束 });// eraserButton.addActionListener()结束 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);// 图形组件可见 }// actionPerformed()结束 });// shapeButton.addActionListener()结束 saveButton.addActionListener(new ActionListener() {// 保存按钮添加动作监听 public void actionPerformed(final ActionEvent arg0) {// 单击时 addWatermark();// 添加水印 DrawImageUtil.saveImage(DrawPictureFrame.this, image);// 打印图片 } }); exitMenuItem.addActionListener(new ActionListener() { // 退出菜单栏添加动作监听 public void actionPerformed(final ActionEvent e) { // 单击时 System.exit(0); // 程序关闭 }/// actionPerformed()结束 });// exitMenuItem.addActionListener()结束 eraserMenuItem.addActionListener(new ActionListener() { // 橡皮菜单栏添加动作监听 public void actionPerformed(final ActionEvent e) { // 单击时 if (eraserMenuItem.getText().equals("橡皮")) { // 如果菜单的文字内容为“橡皮” rubber = true; // 设置橡皮标识为true eraserMenuItem.setText("画图"); // 改变菜单上显示的文本为画图 eraserButton.setText("画图"); // 改变按钮上显示的文本为画图 } else { // 单击工具栏上的画图按钮,使用画笔 rubber = false; // 设置橡皮标识为false eraserMenuItem.setText("橡皮"); // 改变菜单上显示的文本为“橡皮” eraserButton.setText("橡皮"); // 改变按钮上显示的文本为“橡皮” g.setColor(foreColor); // 设置绘图对象的前景色 } // else结束 }// actionPerformed()结束 });// eraserMenuItem.addActionListener()结束 clearMenuItem.addActionListener(new ActionListener() { // 清除菜单添加动作监听 public void actionPerformed(final ActionEvent e) { // 单击时 g.setColor(backgroundColor); // 绘图工具使用背景色 g.fillRect(0, 0, 570, 390); // 画一个背景颜色的方形填满整个画布 g.setColor(foreColor); // 绘图工具使用前景色 canvas.repaint(); // 更新画布 }// actionPerformed()结束 });// clearMenuItem.addActionListener()结束 strokeMenuItem1.addActionListener(new ActionListener() {// "细线"菜单添加动作监听 public void actionPerformed(final ActionEvent e) { // 单击时 // 声明画笔的属性,粗细为1像素,线条末端无修饰,折线处呈尖角 BasicStroke bs = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER); g.setStroke(bs); // 画图工具使用此画笔 strokeButton1.setSelected(true); // "细线"按钮设为选中状态 }// actionPerformed()结束 });// strokeMenuItem1.addActionListener()结束 strokeMenuItem2.addActionListener(new ActionListener() {// "粗线"菜单添加动作监听 public void actionPerformed(final ActionEvent e) { // 单击时 // 声明画笔的属性,粗细为2像素,线条末端无修饰,折线处呈尖角 BasicStroke bs = new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER); g.setStroke(bs); // 画图工具使用此画笔 strokeButton2.setSelected(true); // "粗线"按钮设为选中状态 }// actionPerformed()结束 });// strokeMenuItem2.addActionListener()结束 strokeMenuItem3.addActionListener(new ActionListener() {// "较粗"菜单添加动作监听 public void actionPerformed(final ActionEvent e) { // 单击时 // 声明画笔的属性,粗细为4像素,线条末端无修饰,折线处呈尖角 BasicStroke bs = new BasicStroke(4, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER); g.setStroke(bs); // 画图工具使用此画笔 strokeButton3.setSelected(true); // "较粗"按钮设为选中状态 }// actionPerformed()结束 });// strokeMenuItem3.addActionListener()结束 foregroundMenuItem.addActionListener(new ActionListener() {// 前景色菜单添加动作监听 public void actionPerformed(final ActionEvent e) { // 单击时 // 打开选择颜色对话框,参数依次为:父窗体、标题、默认选中的颜色(青色) Color fColor = JColorChooser.showDialog(DrawPictureFrame.this, "选择颜色对话框", Color.CYAN); if (fColor != null) { // 如果选中的颜色不是空的 foreColor = fColor; // 将选中的颜色赋给前景色变量 } // if结束 foregroundButton.setForeground(foreColor);// 前景色按钮的文字也更换为这种颜色 g.setColor(foreColor); // 绘图工具使用前景色 }// actionPerformed()结束 });// foregroundMenuItem.addActionListener()结束 backgroundMenuItem.addActionListener(new ActionListener() {// 背景色菜单添加动作监听 public void actionPerformed(final ActionEvent e) { // 单击时 // 打开选择颜色对话框,参数依次为:父窗体、标题、默认选中的颜色(青色) Color bgColor = JColorChooser.showDialog(DrawPictureFrame.this, "选择颜色对话框", Color.CYAN); if (bgColor != null) { // 如果选中的颜色不是空的 backgroundColor = bgColor; // 将选中的颜色赋给背景色变量 } // if结束 // 背景色按钮更改按钮自身的背景颜色,其颜色与画板背景颜色相同 backgroundButton.setBackground(backgroundColor); g.setColor(backgroundColor); // 绘图工具使用背景色 g.fillRect(0, 0, 570, 390); // 画一个背景颜色的方形填满整个画布 g.setColor(foreColor); // 绘图工具使用前景色 canvas.repaint(); // 更新画布 }// actionPerformed()结束 });// backgroundMenuItem.addActionListener()结束 saveMenuItem.addActionListener(new ActionListener() { // 保存菜单添加动作监听 public void actionPerformed(ActionEvent e) { // 单击时 addWatermark();// 添加水印 DrawImageUtil.saveImage(DrawPictureFrame.this, image);// 打印图片 }// actionPerformed()结束 });// saveMenuItem.addActionListener()结束 shuiyinMenuItem.addActionListener(new ActionListener() {// 水印菜单项添加动作监听 public void actionPerformed(ActionEvent e) {// 单击时 // 弹出输入对话框 shuiyin = JOptionPane.showInputDialog(DrawPictureFrame.this, "你想添加什么水印?"); if (null == shuiyin) {// 如果输入对话框返回的是null shuiyin = "";// 字符串设为空内容 } else {// 如果不是null setTitle("画图程序(水印内容:[ " + shuiyin + " ] )");// 修改窗体标题 } // else结束 }// actionPerformed()结束 });// shuiyinMenuItem.addActionListener()结束 showPicButton.addActionListener(new ActionListener() {// 展示简笔画按钮添加动作监听 public void actionPerformed(ActionEvent e) {// 单击时 boolean isVisible = picWindow.isVisible();// 获取简笔画展示窗体可见状态 if (isVisible) {// 如果简笔画展示窗体是可见的 showPicButton.setText("展开简笔画");// 修改按钮的文本 picWindow.setVisible(false);// 隐藏简笔画展示窗体 } else {// 如果是隐藏的 showPicButton.setText("隐藏简笔画");// 修改按钮的文本 // 重新指定简笔画展示窗体的显示位置 // 横坐标 = 主窗体横坐标 - 简笔画窗体宽度 - 5 // 纵坐标 = 主窗体纵坐标 picWindow.setLocation(getX() - picWindow.getWidth() - 5, getY()); picWindow.setVisible(true);// 简笔画展示窗体可见 } // else结束 }// actionPerformed()结束 });// showPicButton.addActionListener()结束 }// addListener()结束 /** * 恢复展开简笔画按钮的文本内容,此方法供简笔画面板的“隐藏”按钮调用。 */ public void initShowPicButton() { showPicButton.setText("展开简笔画");// 修改按钮的文本 }// initShowPicButton()结束 private void addWatermark() { if (!"".equals(shuiyin.trim())) {// 如果水印字段不是空字符串 g.rotate(Math.toRadians(-30));// 将图片旋转-30弧度 Font font = new Font("楷体", Font.BOLD, 60);// 设置字体 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结束s }// addWatermark()结束 public void getShape(Shapes shape) { this.shape = shape;// 将返回的图形对象付给类的全局变量 drawShape = true;// 画图形标识变量为true,说明现在鼠标画的是图形,而不是线 }// getShape()结束 public static void main(String[] args) { DrawPictureFrame frame = new DrawPictureFrame();// 创建窗体对象 frame.setVisible(true);// 让窗体可见 }// main结束 }// DrawPictureFrame类 结束
DrawPictureCanvas类
package com.mr.draw; import java.awt.Canvas;//画板类 import java.awt.Graphics;//绘图类 import java.awt.Image;//图像类 public class DrawPictureCanvas extends Canvas { private Image image = null;// 创建画板中图片对象 public void setImage(Image image) { this.image = image;// Ctrl+Shift+S 快捷 Generate Get Set } public void paint(Graphics g) { // 重写 paint方法 g.drawImage(image, 0, 0, null);// 在画布绘图 } public void update(Graphics g) { // 重写 update方法 解决屏幕闪烁 paint(g); } }//DrawPictureCanvas类结束
PictureWindow类
package com.mr.draw; import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Image; 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();//父类窗体还原简笔画按钮文本 }// actionPerformed(); });// hiddenButton.addActionListener()结束 changeButton.addActionListener(new ActionListener() {// 更换图片按钮添加动作监听 public void actionPerformed(ActionEvent e) {// 单击时 centerPanel.setImage(getListImage());// 背景面板重新载入图片 }// actionPerformed(); });// changeButton.addActionListener()结束 }// addListener() /** * 获取图片文件夹下的图片,每次调用此方法,都会获得不同的文件对象 * * @return 返回图片对象 */ private Image getListImage() { String imgPath = list[index].getAbsolutePath();// 获取当前索引下的图片文件路径 ImageIcon image = new ImageIcon(imgPath);// 获取此图片文件的图标对象 index++;// 索引变量递增 if (index >= list.length) {// 如果索引变量超过图片数量了 index = 0;// 索引变量归零 } // if结束 return image.getImage();// 获得图标对象的图片对象 }// getListImage()结束 }// PictureWindow类结束
后续将更新,如何将在eclipse中写的java项目导出打包成。exe类文件,在没有非JVM的环境下执行,直接打包成安装包,在其它windows电脑安装运行。
第一次在CSDN写东西 ,这些也是我自己最近遇到的问题,和一些解决的方法经验,希望对你们有帮助。也可以记录自己的一些经验和成长。
有志者,事竟成,破釜沉舟,百二秦关终属楚;
有心人,天不负,卧薪尝胆,三千越甲可吞吴。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。