主函数类
package com.huaxin.thirdXPfour;
/**
* 主函数,启动程序
* @author Queen_Queen
*
*/
public class Test_main {
public static void main(String[] args) {
XPDraw xp=new XPDraw();
xp.initJFrame();
}
}
面板界面XPDraw类
package com.huaxin.thirdXPfour;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.awt.Color;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
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;
/**
* 仿XP画板的界面
* @author Queen_Queen
*
*/
@SuppressWarnings("serial")
public class XPDraw extends JFrame{
//创建全局数组队列
ArrayList<MyShape> arrayList=new ArrayList<MyShape>();
public void initJFrame() {
/**************************窗体属性的设置*************************************/
setSize(1025,526);
//设置窗体大小不可调
//setResizable(false);
setLocationRelativeTo(null);
setTitle("XP画板");
setDefaultCloseOperation(3);
/**************************菜单栏块的设置*************************************/
JMenuBar jMenuBar=new JMenuBar();
setJMenuBar(jMenuBar);
//菜单条
JMenu jMenuSum[]= {
new JMenu("文件(F)"),new JMenu("编辑(E)"),new JMenu("查看(V)"),
new JMenu("图像(I)"),new JMenu("颜色(C)"),new JMenu("查看(H)")
};
for(int i=0;i<6;i++)
jMenuBar.add(jMenuSum[i]);
//文件菜单项
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++)
jMenuSum[0].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++)
jMenuSum[1].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++)
jMenuSum[2].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++)
jMenuSum[3].add(jMenuItemImage[i]);
//颜色菜单项
jMenuSum[4].add(new JMenuItem("编辑颜色(E)"));
//帮助菜单项
jMenuSum[5].add(new JMenuItem("帮助主题(H)"));
jMenuSum[5].add(new JMenuItem("帮助主题(H)"));
/**************************西边面板的设置*************************************/
JPanel jPanelWest=new JPanel();
jPanelWest.setBackground(new Color(240, 240, 240));
jPanelWest.setPreferredSize(new Dimension(56, 0));
jPanelWest.setLayout(new FlowLayout(1,0,0));
jPanelWest.setBorder(new BevelBorder(0,Color.WHITE,Color.DARK_GRAY));
//添加面板
add(jPanelWest,BorderLayout.WEST);
ButtonGroup buttonGroupPen=new ButtonGroup();
for(int i=0;i<16;i++) {
JRadioButton jRadioButtonPen=new JRadioButton();
//设定指定按钮被选中
if (i==10) {
jRadioButtonPen.setSelected(true);
}
jRadioButtonPen.setActionCommand("jpg-"+i);
jRadioButtonPen.setIcon(new ImageIcon("images/draw"+i+".jpg"));
jRadioButtonPen.setRolloverIcon(new ImageIcon("images/draw"+i+"-1.jpg"));
jRadioButtonPen.setPressedIcon(new ImageIcon("images/draw"+i+"-2.jpg"));
jRadioButtonPen.setSelectedIcon(new ImageIcon("images/draw"+i+"-3.jpg"));
jRadioButtonPen.setBorder(null);
buttonGroupPen.add(jRadioButtonPen);
jPanelWest.add(jRadioButtonPen);
}
//子面板的设置
JPanel jPanelCloned=new JPanel();
jPanelCloned.setPreferredSize(new Dimension(43,55));
jPanelCloned.setBorder(new BevelBorder(1, Color.WHITE, Color.gray));
jPanelCloned.setLayout(new FlowLayout(1,2,5));
ButtonGroup buttonGroupLineSize=new ButtonGroup();
for(int i=1;i<6;i++) {
JRadioButton jRadioButtonLineSize=new JRadioButton();
jRadioButtonLineSize.setIcon(new ImageIcon("images/lineSize_"+i+".gif"));
jRadioButtonLineSize.setBorder(null);
buttonGroupLineSize.add(jRadioButtonLineSize);
jPanelCloned.add(jRadioButtonLineSize);
}
jPanelWest.add(jPanelCloned);
/**************************南边面板的设置*************************************/
JPanel jPanelSouth=new JPanel();
jPanelSouth.setBackground(new Color(240, 240, 240));
jPanelSouth.setPreferredSize(new Dimension(0, 79));
jPanelSouth.setLayout(null);
//添加面板
add(jPanelSouth,BorderLayout.SOUTH);
//添加左下角调色盘
JPanel jPanelPalette=new JPanel();
jPanelPalette.setBounds(0, 9, 240, 30);
jPanelPalette.setLayout(new BorderLayout());
jPanelSouth.add(jPanelPalette);
/**
* 笔刷显色区代码块
*/
//面板
JPanel jPanelChange=new JPanel();
jPanelChange.setBorder(new BevelBorder(1,Color.white,Color.darkGray));
jPanelChange.setLayout(null);
jPanelChange.setPreferredSize(new Dimension(30, 30));
//按钮属性
JButton jButtonPen=new JButton();
jButtonPen.setBackground(Color.black);
JButton jButtonBack=new JButton();
jButtonBack.setBackground(Color.white);
jButtonPen.setBorder(new BevelBorder(0, Color.white, Color.GRAY));
jButtonBack.setBorder(new BevelBorder(0, Color.white, Color.GRAY));
jButtonPen.setBounds(5, 5, 15, 15);
jButtonBack.setBounds(10, 10, 15, 15);
//设置按钮不可使用
jButtonPen.setEnabled(false);
jButtonBack.setEnabled(false);
jPanelChange.add(jButtonPen);
jPanelChange.add(jButtonBack);
jPanelPalette.add(jPanelChange,BorderLayout.WEST);
/**
* 颜料盒代码块
*/
JPanel jPanelPigment=new JPanel();
jPanelPigment.setPreferredSize(new Dimension(210, 30));
jPanelPigment.setLayout(new GridLayout(2, 14));
Color color[]= {
new Color(0, 0, 0),new Color(128, 128, 128),new Color(128, 0, 0),new Color(128, 128, 0),
new Color(0, 128, 0),new Color(0, 128, 128),new Color(0, 0, 128),new Color(128, 0, 128),
new Color(128, 128, 64),new Color(0, 64, 64),new Color(0, 128, 255),new Color(0, 64, 128),
new Color(128, 0, 255),new Color(128, 64, 0),new Color(255, 255, 255),new Color(192, 192, 192),
new Color(255, 0, 0),new Color(255, 255, 0),new Color(0, 255, 0),new Color(0, 255, 255),
new Color(0, 0, 255),new Color(255, 0, 255),new Color(255, 255, 128),new Color(0, 255, 128),
new Color(128, 255, 255),new Color(128, 128, 255),new Color(255, 0, 128),new Color(255, 128, 64)
};
SouthColorListener southColorListener=new SouthColorListener(jButtonPen);
for(int i=0;i<28;i++) {
JButton jButtonPigment=new JButton();
jButtonPigment.setBackground(color[i]);
jButtonPigment.setPreferredSize(new Dimension(15, 15));
jButtonPigment.setBorder(new BevelBorder(1,Color.WHITE,Color.DARK_GRAY));
jPanelPigment.add(jButtonPigment);
jButtonPigment.addActionListener(southColorListener);
}
jPanelPalette.add(jPanelPigment);
//帮助标签条
JLabel jLabelHelp=new JLabel("要获得帮助,请在帮助“菜单”中,单击“帮助主题”。");
jLabelHelp.setBounds(0, 49, 1025, 30);
jLabelHelp.setBorder(new BevelBorder(0,Color.WHITE,Color.GRAY));
jPanelSouth.add(jLabelHelp);
/**************************中间面板的设置*************************************/
JPanel jPanelCenter=new JPanel();
jPanelCenter.setBackground(new Color(170,170,170));
jPanelCenter.setLayout(new FlowLayout(0,5,5));
//添加面板
add(jPanelCenter);
//画布设置
JPanel jPanelDraw=new JPanel() {
//重绘功能
public void paint(Graphics graphicsBrush) {
super.paint(graphicsBrush);
for (int i = 0; i < arrayList.size(); i++) {
//获取对象
MyShape myShape =arrayList.get(i);
myShape.draw(graphicsBrush);
}
}
};
jPanelDraw.setPreferredSize(new Dimension(900, 350));
jPanelDraw.setBackground(Color.WHITE);
jPanelCenter.add(jPanelDraw);
setVisible(true);
//从画布获取画笔
Graphics graphicsBrush=jPanelDraw.getGraphics();
//添加画布的鼠标监听器
WestGraphListener westGraphListener=new WestGraphListener(graphicsBrush,buttonGroupPen,jButtonPen,arrayList);
jPanelDraw.addMouseListener(westGraphListener);
jPanelDraw.addMouseMotionListener(westGraphListener);
}
}
西边区域监听器WestGraphListener类
package com.huaxin.thirdXPfour;
import java.awt.AWTException;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Robot;
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;
public class WestGraphListener implements MouseListener,MouseMotionListener{
//创建随机对象
Random random=new Random();
public Graphics2D graphicsBrush;
public ButtonGroup buttonGroupPen;
public JButton jButtonPen;
public String command;
public Color color;
public int size;
public ArrayList<MyShape> arrayList;
public int x1,x2,y1,y2;
public WestGraphListener(Graphics graphicsBrush, ButtonGroup buttonGroupPen, JButton jButtonPen,
ArrayList<MyShape> arrayList) {
this.graphicsBrush=(Graphics2D) graphicsBrush;
this.buttonGroupPen=buttonGroupPen;
this.jButtonPen=jButtonPen;
this.arrayList=arrayList;
}
public void mousePressed(MouseEvent e) {
x1=e.getX();
y1=e.getY();
ButtonModel buttonModel=buttonGroupPen.getSelection();
command=buttonModel.getActionCommand();
//画笔设置颜色
color=jButtonPen.getBackground();
//画笔的默认大小
graphicsBrush.setStroke(new BasicStroke(1));
}
public void mouseReleased(MouseEvent e) {
x2=e.getX();
y2=e.getY();
MyShape myShape=null;
if (command.equals("jpg-4")) {
//取色器的实现
try {
Robot robot=new Robot();
//截屏
//创建一个矩形区域对象
Rectangle rectangle=new Rectangle(e.getXOnScreen(), e.getYOnScreen(), 1, 1);
BufferedImage bufferedImage=robot.createScreenCapture(rectangle);
//获取图片像素点的颜色
int c=bufferedImage.getRGB(0, 0);
Color color=new Color(c);
//把颜色设置到颜料盘的JButtonPen上
jButtonPen.setBackground(color);
} catch (AWTException e1) {
e1.printStackTrace();
}
}
//绘画直线,矩形,椭圆,原矩形
else if(command.equals("jpg-10")) {
myShape=new MyLine(x1, x2, y1, y2,color);
}else if(command.equals("jpg-12")){
myShape =new MyRectangle(x1, x2, y1, y2,color);
}else if(command.equals("jpg-14")) {
myShape=new MyOval(x1, x2, y1, y2,color);
}else if(command.equals("jpg-15")){
myShape=new MyRoundRect(x1, x2, y1, y2,color);
}
if (myShape!=null) {
myShape.draw(graphicsBrush);
arrayList.add(myShape);
}
}
public void mouseDragged(MouseEvent e) {
x2=e.getX();
y2=e.getY();
MyShape myShape=null;
if (command.equals("jpg-2")) {
//绘制橡皮擦
size=10;
color=Color.WHITE;
myShape=new MyLine(x1, x2, y1, y2, color, size);
}else if (command.equals("jpg-6")) {
//绘制铅笔
myShape=new MyLine(x1, x2, y1, y2,color);
}else if (command.equals("jpg-7")) {
//绘制刷子
size=10;
myShape=new MyLine(x1, x2, y1, y2, color, size);
}
if (myShape!=null) {
x1=x2;
y1=y2;
myShape.draw(graphicsBrush);
arrayList.add(myShape);
}
if (command.equals("jpg-8")) {
//绘制喷枪
graphicsBrush.drawLine(x2, y2, x2, y2);
//在当前点周围绘制更多的随机点
for (int i = 0; i < 30; i++) {
int x_p=random.nextInt(21)-10;
int y_p=random.nextInt(21)-10;
graphicsBrush.drawLine(x2+x_p, y2+y_p, x2+x_p, y2+y_p);
}
}
}
//用不到的类方法
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
}
南边区域监听器SouthColorListener类
package com.huaxin.thirdXPfour;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class SouthColorListener implements ActionListener{
public JButton jButtonPen;
public SouthColorListener(JButton jButtonPen) {
this.jButtonPen=jButtonPen;
}
public void actionPerformed(ActionEvent e) {
//获取事件源
JButton jButtonPigment=(JButton)e.getSource();
Color color=jButtonPigment.getBackground();
jButtonPen.setBackground(color);
}
}
封装对象MyShape类
package com.huaxin.thirdXPfour;
import java.awt.Color;
import java.awt.Graphics;
public abstract class MyShape {
public int x1,x2,y1,y2,size;
public Color color;
public MyShape() {}
public MyShape(int x1, int x2, int y1, int y2,Color color) {
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
this.color=color;
}
public MyShape(int x1, int x2, int y1, int y2, Color color, int size) {
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
this.color=color;
this.size=size;
}
public abstract void draw(Graphics graphicsBrush);
}
MyShape子类
package com.huaxin.thirdXPfour;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class MyLine extends MyShape{
public MyLine(int x1, int x2, int y1, int y2, Color color) {
super(x1, x2, y1, y2, color);
}
public MyLine(int x1, int x2, int y1, int y2, Color color, int size) {
super(x1, x2, y1, y2, color, size);
}
public void draw(Graphics graphicsBrush) {
graphicsBrush.setColor(color);
((Graphics2D) graphicsBrush).setStroke(new BasicStroke(size));
graphicsBrush.drawLine(x1, y1, x2, y2);
}
}
package com.huaxin.thirdXPfour;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class MyRectangle extends MyShape{
public MyRectangle(int x1, int x2, int y1, int y2,Color color) {
super(x1, x2, y1, y2,color);
}
public void draw(Graphics graphicsBrush) {
graphicsBrush.setColor(color);
((Graphics2D) graphicsBrush).setStroke(new BasicStroke(size));
graphicsBrush.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
}
}
package com.huaxin.thirdXPfour;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class MyOval extends MyShape{
public MyOval(int x1, int x2, int y1, int y2,Color color) {
super(x1, x2, y1, y2,color);
}
public void draw(Graphics graphics) {
graphics.setColor(color);
((Graphics2D) graphics).setStroke(new BasicStroke(size));
graphics.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
}
}
package com.huaxin.thirdXPfour;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class MyRoundRect extends MyShape{
public MyRoundRect(int x1, int x2, int y1, int y2,Color color) {
super(x1, x2, y1, y2,color);
}
public void draw(Graphics graphics) {
graphics.setColor(color);
((Graphics2D) graphics).setStroke(new BasicStroke(size));
graphics.drawRoundRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2), 20,20);
}
}