步骤:
ColorListener类:
1.先建立一个窗口,设置窗体的大小,位置,关闭方式等等
2.往窗体上添加菜单栏
3.添加面板,同时设置各面板的相关属性(为了区别各面板,每个面板的颜色应该不同用Color.+颜色),再添加到窗体上
4.先设置左边面板的内容
5.再设置下面面板内容
6.最后设置中间
7.实现基本图形绘制:直线,椭圆,矩形
8.实现颜色的选择
9.实现复杂图形的绘制
10.实现画板的重绘
测试类:
package com.huaxin.drawboard;
import java.util.Random;
/**
* 启动程序类
* @author 16974
*
*/
public class Test {
public static void main(String[] args) {
DrawBoardFrame dbf = new DrawBoardFrame();
dbf.initFrame();
}
}
DrawBoardFrame:
package com.huaxin.drawboard;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.BevelBorder;
/**
* 绘图工具窗体类
* 主要用于实现当前窗体的效果
* @author 16974
*
*/
public class DrawBoardFrame extends JFrame{
public ArrayList<MyShape> list = new ArrayList<MyShape>();
public Random ran = new Random();
/**
* 初始化窗体属性
*/
public void initFrame(){
this.setSize(700,500);
this.setDefaultCloseOperation(3);
//设置窗体是否能改变大小
this.setResizable(false);
this.setLocationRelativeTo(null);
//1.添加菜单
JMenuBar jMenuBar=new JMenuBar();
setJMenuBar(jMenuBar);
//菜单条
JMenu jMenuFile=new JMenu("文件(F)");
JMenu jMenuEditor=new JMenu("编辑(E)");
JMenu jMenuView=new JMenu("查看(V)");
JMenu jMenuImage=new JMenu("图像(I)");
JMenu jMenuColor=new JMenu("颜色(C)");
JMenu jMenuHelp=new JMenu("查看(H)");
jMenuBar.add(jMenuFile);
jMenuBar.add(jMenuEditor);
jMenuBar.add(jMenuView);
jMenuBar.add(jMenuImage);
jMenuBar.add(jMenuColor);
jMenuBar.add(jMenuHelp);
//文件菜单项
JMenuItem jMenuItemFile[]= {
new JMenuItem("新建(N) Ctrl+N"),new JMenuItem("打开(O) Ctrl+O"),new JMenuItem("保存(S) Ctrl+S"),
new JMenuItem("另存为(A)"),new JMenuItem("从扫描仪或照相机(C)"),new JMenuItem("打印预览(V)"),
new JMenuItem("页面设置(U)"),new JMenuItem("打印(P) Ctrl+P"),new JMenuItem("发送(E)"),
new JMenuItem("设置为墙纸(平铺)(B)"),new JMenuItem("设置为墙纸(居中)(K)"),new JMenuItem("最近使用过的文件"),
new JMenuItem("退出(X)")
};
for(int i=0;i<13;i++)
jMenuFile.add(jMenuItemFile[i]);
//编辑菜单项
JMenuItem jMenuItemEditor[]= {
new JMenuItem("撤销(U) Ctrl+Z"),new JMenuItem("重复(R) Ctrl+Y"),new JMenuItem("剪切(T) Ctrl+X"),
new JMenuItem("复制(C) Ctrl+C"),new JMenuItem("粘贴(P) Ctrl+V"),new JMenuItem("清除选定内容(L) Del"),
new JMenuItem("全选(A) Ctrl+A"),new JMenuItem("复制到(O)…"),new JMenuItem("粘贴来源(F)…")
};
for(int i=0;i<9;i++)
jMenuEditor.add(jMenuItemEditor[i]);
//查看菜单项
JMenuItem jMenuItemView[]= {
new JMenuItem("工具箱(T) Ctrl+T"),new JMenuItem("颜料盒(C) Ctrl+L"),new JMenuItem("状态栏(S)"),
new JMenuItem("文字工具栏(E)"),new JMenu("缩放(Z)"),new JMenuItem("查看位图(V) Ctrl+F")
};
for(int i=0;i<6;i++)
jMenuView.add(jMenuItemView[i]);
JMenuItem jMenuItemZoom[]= {
new JMenuItem("常规尺寸(N) Ctrl+PgUp"),new JMenuItem("大尺寸(L) Ctrl+PgDn"),new JMenuItem("自定义(U)"),
new JMenuItem("显示网格(G) Ctrl+G"),new JMenuItem("显示缩略图(H)")
};
for(int i=0;i<5;i++)
jMenuItemView[4].add(jMenuItemZoom[i]);
//图像菜单项
JMenuItem jMenuItemImage[]= {
new JMenuItem("翻转/旋转(F) Ctrl+R"),new JMenuItem("拉伸/扭曲(S) Ctrl+W"),new JMenuItem("反色(I) Ctrl+I"),
new JMenuItem("属性(A) Ctrl+E"),new JMenuItem("清除图像(C) Ctrl+Shift+N"),new JMenuItem("不透明处理(D)")
};
for(int i=0;i<6;i++)
jMenuImage.add(jMenuItemImage[i]);
//颜色菜单项
jMenuColor.add(new JMenuItem("编辑颜色(E)"));
//帮助菜单项
jMenuHelp.add(new JMenuItem("帮助主题(H)"));
jMenuHelp.add(new JMenuItem("帮助主题(H)"));
//2.添加面板
JPanel panelLeft = new JPanel();
JPanel panelDown = new JPanel();
JPanel panelCenter = new JPanel();
//设置面板的相关属性
panelLeft.setPreferredSize(new Dimension(50,0));
panelDown.setPreferredSize(new Dimension(0,50));
panelLeft.setBackground(new Color(235,233,236));
panelDown.setBackground(new Color(235,233,236));
panelCenter.setBackground(new Color(171,171,171));
//把三个面板添加到窗体上
this.add(panelLeft,BorderLayout.WEST);
this.add(panelDown,BorderLayout.SOUTH);
this.add(panelCenter);
/********************************左边面板内容设置**********************************/
//修改左边面板布局,设置为流式,横向纵线间隙为0,左对齐
panelLeft.setLayout(new FlowLayout(1,0,0));
//给左边面板添加图形选择功能内容
//创建按钮组对象,用于封装所有的单选按钮,并实现单选功能
ButtonGroup bg = new ButtonGroup();
for(int i=0;i<16;i++){
JRadioButton jrb = new JRadioButton();
//设置指定按钮被选中
if(i==10){
jrb.setSelected(true);
}
//给单选按钮设置命令
jrb.setActionCommand("pic"+i);
//设置按钮图片
jrb.setIcon(new ImageIcon("images/draw"+i+".jpg"));
jrb.setRolloverIcon(new ImageIcon("images/draw"+i+"-1.jpg"));
jrb.setPressedIcon(new ImageIcon("images/draw"+i+"-2.jpg"));
jrb.setSelectedIcon(new ImageIcon("images/draw"+i+"-3.jpg"));
panelLeft.add(jrb);
//去除单选按钮边框
jrb.setBorder(null);
//添加到组中
bg.add(jrb);
}
//子面板设置
JPanel panelC = new JPanel();
panelC.setPreferredSize(new Dimension(35,70));
//设置边框
panelC.setBorder(new BevelBorder(1,Color.white,Color.gray));
panelLeft.add(panelC);
/********************************下边面板内容设置**********************************/
panelDown.setLayout(new FlowLayout(0,10,10));
JPanel colorPanel = new JPanel();
colorPanel.setPreferredSize(new Dimension(250,30));
panelDown.add(colorPanel);
//颜色面板左边部分
colorPanel.setLayout(new BorderLayout());
JPanel colorLeft = new JPanel();
JPanel colorRight = new JPanel();
colorLeft.setPreferredSize(new Dimension(30,30));
colorPanel.add(colorLeft,BorderLayout.WEST);
colorPanel.add(colorRight);
//颜色左边面板特效
colorLeft.setBorder(new BevelBorder(1,Color.white,Color.gray));
colorLeft.setLayout(null);
JButton bt1 = new JButton();
JButton bt2 = new JButton();
bt1.setBackground(Color.black);
bt1.setBorder(new BevelBorder(0,Color.white,Color.gray));
bt2.setBackground(Color.white);
bt2.setBorder(new BevelBorder(0,Color.white,Color.gray));
bt1.setBounds(3,3,15,15);
bt2.setBounds(10,10,15,15);
//设置按钮不能使用
bt1.setEnabled(false);
bt2.setEnabled(false);
colorLeft.add(bt1);
colorLeft.add(bt2);
//右边部分的多个颜色按钮
// colorRight.setLayout(new FlowLayout(0,0,0));
colorRight.setLayout(new GridLayout(2, 14));
//把颜色监听器创建好
ColorListener cl = new ColorListener(bt1);
for(int i=0;i<28;i++){
JButton button = new JButton();
button.setPreferredSize(new Dimension(15,15));
button.setBorder(new BevelBorder(1,Color.white,Color.gray));
button.setBackground(new Color(ran.nextInt(256),ran.nextInt(256),ran.nextInt(256)));
colorRight.add(button);
//把监听器绑定到按钮上
button.addActionListener(cl);
}
/********************************中间面板内容设置**********************************/
panelCenter.setLayout(new FlowLayout(0,5,5));
JPanel panelDraw = new JPanel(){
public void paint(Graphics g){
super.paint(g);
//把消失的图形全部重新绘制上来
// g.drawLine(10, 10, 100, 100)
for(int i=0;i<list.size();i++){
MyShape shape = list.get(i);
shape.draw((Graphics2D) g);
}
}
};
panelDraw.setPreferredSize(new Dimension(400,300));
panelDraw.setBackground(Color.white);
panelCenter.add(panelDraw);
this.setVisible(true);
//获取画笔
Graphics g = panelDraw.getGraphics();
//创建监听器对象
DrawListener dl = new DrawListener(g,bg,bt1,list);
panelDraw.addMouseListener(dl);
panelDraw.addMouseMotionListener(dl);
}
}
DrawListener:
package com.huaxin.drawboard;
import java.awt.AWTException;
import java.awt.BasicStroke;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import org.omg.CORBA.PUBLIC_MEMBER;
import com.sun.javafx.geom.Shape;
/**
* 绘图功能类的实现
* @author 16974
*
*/
class DrawListener implements MouseListener,MouseMotionListener{
public Random ran = new Random();
//定义4个值,用于存储按下和释放的坐标
public int x1,y1,x2,y2;
public Graphics2D g;
public ButtonGroup bg;
public JButton jb;
public String command;
public String type="10";
public ArrayList<MyShape>list;
public Color color;
public Stroke s;
public DrawListener(Graphics g,ButtonGroup bg,JButton jb,ArrayList<MyShape>list){
this.g = (Graphics2D)g;
this.bg = bg;
this.jb = jb;
this.list = list;
}
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
//获取选中按钮的图形命令
ButtonModel bm = bg.getSelection();
String command = bm.getActionCommand();
type = command;
//设置画笔的颜色
g.setColor(jb.getBackground());
//把画笔粗细归为默认值1
g.setStroke(new BasicStroke (1));
}
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
MyShape shape = null;
//画图形
if(type.equals("pic10")){
shape = new MyLine(x1, y1, x2, y2,g.getColor(),g.getStroke());
}else if(type.equals("pic12")){
shape = new MyRect(x1, y1, x2, y2,g.getColor(),g.getStroke());
}else if(type.equals("pic14")){
shape = new MyOval(x1, y1, x2, y2,g.getColor(),g.getStroke());
}else if (type.equals("pic4")) {
try {
Robot robot = new Robot();
//截屏
//创建一个矩形区域对象
Rectangle rect = new Rectangle(e.getXOnScreen(),e.getYOnScreen(),1,1);
BufferedImage bi = robot.createScreenCapture(rect);
//获取图片的像素点颜色
int c = bi.getRGB(0, 0);
Color color = new Color(c);
//把颜色设置到颜色小面板上
jb.setBackground(color);
} catch (AWTException e1) {
e1.printStackTrace();
}
}else if (type.equals("pic15")){
shape = new MyRonundRect(x1, y1, x2, y2, g.getColor(),g.getStroke());
}
if(shape!=null){
shape.draw(g);
list.add(shape);
}
}
/*
* 拖动鼠标监听
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
*/
public void mouseDragged(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
MyShape shape = null;
if(type.equals("pic6")){
shape = new MyLine(x1, y1, x2, y2,g.getColor(),g.getStroke());
x1 = x2;
y1 = y2;
}else if (type.equals("pic7")) {
Stroke s = new BasicStroke(10);
g.setStroke(s);//改变笔的粗细
shape = new MyLine(x1, y1, x2, y2,g.getColor(),g.getStroke());
x1 = x2;
y1 = y2;
}else if (type.equals("pic2")) {
Stroke s = new BasicStroke(10);
g.setStroke(s);//改变笔的粗细
Color color = new Color(255, 255, 255);
g.setColor(color);
shape = new MyLine(x1, y1, x2, y2,g.getColor(),g.getStroke());
x1 = x2;
y1 = y2;
}else if (type.equals("pic8")) {
shape = new MyLine(x1, y1, x2, y2,g.getColor(),g.getStroke());
for(int i=0;i<30;i++){
int x_p = ran.nextInt(21)-10;
int y_p = ran.nextInt(21)-10;
shape = new MyLine(x2+x_p, y2+y_p, x2+x_p, y2+y_p,g.getColor(),g.getStroke());
}
}
if(shape!=null){
shape.draw(g);
list.add(shape);
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
}
package com.huaxin.drawboard;
import java.awt.AWTException;
import java.awt.BasicStroke;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import org.omg.CORBA.PUBLIC_MEMBER;
import com.sun.javafx.geom.Shape;
/**
* 绘图功能类的实现
* @author 16974
*
*/
class DrawListener implements MouseListener,MouseMotionListener{
public Random ran = new Random();
//定义4个值,用于存储按下和释放的坐标
public int x1,y1,x2,y2;
public Graphics2D g;
public ButtonGroup bg;
public JButton jb;
public String command;
public String type="10";
public ArrayList<MyShape>list;
public Color color;
public Stroke s;
public DrawListener(Graphics g,ButtonGroup bg,JButton jb,ArrayList<MyShape>list){
this.g = (Graphics2D)g;
this.bg = bg;
this.jb = jb;
this.list = list;
}
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
//获取选中按钮的图形命令
ButtonModel bm = bg.getSelection();
String command = bm.getActionCommand();
type = command;
//设置画笔的颜色
g.setColor(jb.getBackground());
//把画笔粗细归为默认值1
g.setStroke(new BasicStroke (1));
}
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
MyShape shape = null;
//画图形
if(type.equals("pic10")){
shape = new MyLine(x1, y1, x2, y2,g.getColor(),g.getStroke());
}else if(type.equals("pic12")){
shape = new MyRect(x1, y1, x2, y2,g.getColor(),g.getStroke());
}else if(type.equals("pic14")){
shape = new MyOval(x1, y1, x2, y2,g.getColor(),g.getStroke());
}else if (type.equals("pic4")) {
try {
Robot robot = new Robot();
//截屏
//创建一个矩形区域对象
Rectangle rect = new Rectangle(e.getXOnScreen(),e.getYOnScreen(),1,1);
BufferedImage bi = robot.createScreenCapture(rect);
//获取图片的像素点颜色
int c = bi.getRGB(0, 0);
Color color = new Color(c);
//把颜色设置到颜色小面板上
jb.setBackground(color);
} catch (AWTException e1) {
e1.printStackTrace();
}
}else if (type.equals("pic15")){
shape = new MyRonundRect(x1, y1, x2, y2, g.getColor(),g.getStroke());
}
if(shape!=null){
shape.draw(g);
list.add(shape);
}
}
/*
* 拖动鼠标监听
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
*/
public void mouseDragged(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
MyShape shape = null;
if(type.equals("pic6")){
shape = new MyLine(x1, y1, x2, y2,g.getColor(),g.getStroke());
x1 = x2;
y1 = y2;
}else if (type.equals("pic7")) {
Stroke s = new BasicStroke(10);
g.setStroke(s);//改变笔的粗细
shape = new MyLine(x1, y1, x2, y2,g.getColor(),g.getStroke());
x1 = x2;
y1 = y2;
}else if (type.equals("pic2")) {
Stroke s = new BasicStroke(10);
g.setStroke(s);//改变笔的粗细
Color color = new Color(255, 255, 255);
g.setColor(color);
shape = new MyLine(x1, y1, x2, y2,g.getColor(),g.getStroke());
x1 = x2;
y1 = y2;
}else if (type.equals("pic8")) {
shape = new MyLine(x1, y1, x2, y2,g.getColor(),g.getStroke());
for(int i=0;i<30;i++){
int x_p = ran.nextInt(21)-10;
int y_p = ran.nextInt(21)-10;
shape = new MyLine(x2+x_p, y2+y_p, x2+x_p, y2+y_p,g.getColor(),g.getStroke());
}
}
if(shape!=null){
shape.draw(g);
list.add(shape);
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
}
ColorListener类:
package com.huaxin.drawboard;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class ColorListener implements ActionListener{
public JButton jb;
public ColorListener(JButton jb){
this.jb = jb;
}
public void actionPerformed(ActionEvent e) {
//获取触发这个事件的按钮的背景色:获取事件源
JButton button = (JButton)e.getSource();
//获取按钮的背景色
Color color = button.getBackground();
//把颜色设置到前面小按钮上,作为背景色
jb.setBackground(color);
}
}
MyShape类:
package com.huaxin.drawboard;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
public abstract class MyShape {
public int x1,y1,x2,y2;
public Color color;
public Stroke s;
public MyShape(int x1,int y1,int x2,int y2,Color color,Stroke s){
super();
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
this.s = s;
}
public abstract void draw(Graphics2D g);
}
MyLine:类:
package com.huaxin.drawboard;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
public class MyLine extends MyShape {
public MyLine(int x1, int y1, int x2, int y2,Color color,Stroke s) {
super(x1, y1, x2, y2,color,s);
}
public void draw(Graphics2D g){
//设置画笔颜色
g.setColor(color);
//设置画笔粗细
g.setStroke(s);
g.drawLine(x1, y1, x2, y2);
}
}
MyRect类:
package com.huaxin.drawboard;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
public class MyRect extends MyShape{
public MyRect(int x1, int y1, int x2, int y2,Color color,Stroke s) {
super(x1, y1, x2, y2,color,s);
}
public void draw(Graphics2D g) {
//设置画笔颜色
g.setColor(color);
//设置画笔粗细
g.setStroke(s);
g.drawRect(Math.min(x1,x2),Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));
}
}
MyOval类:
package com.huaxin.drawboard;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
public class MyOval extends MyShape{
public MyOval(int x1, int y1, int x2, int y2,Color color,Stroke s) {
super(x1, y1, x2, y2,color,s);
}
public void draw(Graphics2D g) {
g.setColor(color);
g.setStroke(s);
g.drawOval(Math.min(x1,x2),Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));
}
}
MyRonundRect类:
package com.huaxin.drawboard;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Stroke;
public class MyRonundRect extends MyShape {
public MyRonundRect(int x1, int y1, int x2, int y2, Color color, Stroke s) {
super(x1, y1, x2, y2, color, s);
}
public void draw(Graphics2D g){
//设置画笔颜色
g.setColor(color);
//设置画笔粗细
g.setStroke(s);
g.drawRoundRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1),10, 10);
}
}
完成后测试: