写的第一个有实用性的程序——简易画图板,已经完成了部分按钮的功能(橡皮擦、铅笔、刷子、喷枪、直线、矩形、多边形、椭圆、圆角矩形),颜色的选取,鼠标左右键可以任意切换颜色,还有菜单栏。
下面是一些相关代码:
//******************左边面板添加形状选择工具**************//
//添加图片及按钮
ButtonGroup group =new ButtonGroup();
String[] strs ={ "0", "1", "easer", "3", "4", "5", "pencial", "brush", "spray", "9",
"line", "11", "rect", "polygon", "oval", "roundrect"};
for(int i=0;i<16;i++){
JRadioButton btn1 =new JRadioButton();
left.add(btn1);
group.add(btn1);
btn1.setActionCommand(strs[i]);
btn1.setSelected(true);
ImageIcon defaultIcon = new ImageIcon("img/draw"+i+".jpg");
btn1.setIcon(defaultIcon);
ImageIcon rolloverIcon =new ImageIcon("img/draw"+i+"-1.jpg");
btn1.setRolloverIcon(rolloverIcon);
ImageIcon pressedIcon = new ImageIcon("img/draw"+i+"-2.jpg");
btn1.setPressedIcon(pressedIcon);
ImageIcon selectedIcon =new ImageIcon("img/draw"+i+"-3.jpg");
btn1.setSelectedIcon(selectedIcon);
}
//******************中间面板添加****************//
JPanel drawpJPanel=new JPanel();
Dimension drawDim=new Dimension(400,300);
drawpJPanel.setPreferredSize(drawDim);
drawpJPanel.setBackground(Color.WHITE);
FlowLayout f1=new FlowLayout(FlowLayout.LEFT);
center.setLayout(f1);
center.add(drawpJPanel);
//*******************底部面板添加颜色选择工具******//
JLabel frontLabel =new JLabel();
JLabel backLabel=new JLabel();
foot.setLayout(null);
frontLabel.setBounds(50, 20, 40, 60);
backLabel.setBounds(110, 20, 40, 60);
frontLabel.setBackground(Color.BLACK);
backLabel.setBackground(Color.BLUE);
frontLabel.setOpaque(true);
backLabel.setOpaque(true);
foot.add(frontLabel);
foot.add(backLabel);
//颜色
Color[] cs={Color.BLACK, Color.GRAY, new Color(128, 0, 0),Color.RED,
new Color(255, 128, 0), Color.YELLOW, Color.GREEN,
new Color(0, 128, 255), Color.BLUE, Color.MAGENTA, new Color(255,128,128),
new Color(128, 0, 125), new Color(128, 255, 0), new Color(128, 0, 255),
new Color(0, 128, 120),new Color(128, 0, 20), new Color(128, 128, 0),
new Color(100, 128, 255), new Color(128, 30, 50), new Color(128, 110, 50)};
ColorListener mlis =new ColorListener(frontLabel,backLabel);
for(int i=0;i<20;i++){
JLabel colorLabel= new JLabel();
colorLabel.addMouseListener(mlis);
if(i<10){
colorLabel.setBounds(160+30*i, 20, 25, 25);
}else{
colorLabel.setBounds(160+30*(i-10), 50, 25, 25);
}
colorLabel.setBackground(cs[i]);
colorLabel.setOpaque(true);
foot.add(colorLabel);
}
//添加监听器
DrawListener dlis = new DrawListener(drawpJPanel,group,frontLabel,backLabel);
drawpJPanel.addMouseListener(dlis);
drawpJPanel.addMouseMotionListener(dlis);
//添加菜单栏
private static JMenuBar craeteMenu() {
JMenuBar jmb=new JMenuBar();
String[] array={"文件","颜色","编辑","图像","帮助"};
String[][] array2={{"新建", "保存", "打开", "退出"},{"编辑颜色"},{"字体"},{"修改"},{"关于画图"}};
for(int i=0;i<array.length;i++){
JMenu jmenu=new JMenu(array[i]);
jmb.add(jmenu);
for(int j=0;j<array2[i].length;j++){
JMenuItem jmt=new JMenuItem(array2[i][j]);
jmenu.add(jmt);
}
}
return jmb;
}
//直线、矩形、椭圆、圆角矩形、多边形的绘制
public void mouseReleased(MouseEvent e){
x2=e.getX();
y2=e.getY();
int x=x1<x2?x1:x2 ;
int y=y1<y2?y1:y2 ;
int width= x1<x2?x2-x1:x1-x2;
int height=y1<y2?y2-y1:y1-y2;
int arcWidth=10;
int arcHeight=10;
//直线,矩形,椭圆的绘制
if(str.equals("line")){//直线
g.drawLine(x1, y1, x2, y2);
}else if(str.equals("rect")){//矩形
g.drawRect(x, y, width, height);
}else if(str.equals("oval")){//椭圆
g.drawOval(x, y, width, height);
}else if(str.equals("roundrect")){//圆角矩形
g.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
}else if(str.equals("polygon")){//多边形
if(flag==false){
g.drawLine(x2, y2, x1, y1);
x3=x2;
y3=y2;
flag=true;
}else if(flag==true){
g.drawLine(x3,y3,x1,y1);
x3=x1;
y3=y1;
}
}
};
//刷子、橡皮擦、 铅笔、 喷枪的绘制
public void mouseDragged(MouseEvent e){
x2=e.getX();
y2=e.getY();
//刷子
if(str.equals("brush")){
Graphics2D g2d=(Graphics2D)g;
g2d.setStroke(new BasicStroke(8));
g2d.drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
g2d.setStroke(new BasicStroke(1));
}
//橡皮擦
else if(str.equals("easer")){
g.setColor(Color.WHITE);
Graphics2D g2d=(Graphics2D)g;
g2d.setStroke(new BasicStroke(8));
g2d.drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
g2d.setStroke(new BasicStroke(1));
}
//铅笔
else if(str.equals("pencial")){
g.drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
}
//喷枪
else if(str.equals("spray")){
Random random=new Random();
for(int i=0;i<30;i++){
int x=random.nextInt(10);
int y=random.nextInt(10);
g.drawLine(x1+x, y1+y, x2+x, y2+y);
x1=x2;
y1=y2;
} }
}
//鼠标左右键选取颜色
public void mouseReleased(MouseEvent e) {
JLabel label=(JLabel)e.getSource();
Color c=label.getBackground();
int num = e.getButton();
if(num==1){
frontLabel.setBackground(c);
}else if(num==3){
backLabel.setBackground(c);
}
}
但是还有一些按钮的功能没有实现:虚线五角星,虚线矩形,油漆,取色器,放大镜,写字,以及曲线;菜单栏按钮的功能没有实现;还有将画图板放大后,图像会消失,这个也没有解决。以后将会慢慢把它补充完整。
附上完整代码(求改进):
package com.newer.yt.draw.copy;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
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;
/**
* 主函数
* @author leaf-stop
*
*/
public class DrawUI{
public static void main(String[] args) {
DrawUI dui=new DrawUI();
dui.shoeUI();
}
/**
* 画图板主窗口
* 画图板布局及各个板块存放的东西
*/
public void shoeUI(){
// 创建窗体
JFrame jf = new JFrame();
jf.setTitle("画图板");
jf.setSize(600,500);
jf.setDefaultCloseOperation(3);
//指定布局为边框布局
BorderLayout layout = new BorderLayout();
jf.setLayout(layout);
//左边布局
JPanel left =new JPanel();
Dimension leftDim=new Dimension(100,1);
left.setPreferredSize(leftDim);
//中间布局
JPanel center =new JPanel();
center.setBackground(Color.GRAY);
//底部布局
JPanel foot =new JPanel();
Dimension footdim=new Dimension(1,100);//
foot.setPreferredSize(footdim);
//设置位置
jf.add(left,BorderLayout.WEST);
jf.add(center,BorderLayout.CENTER);
jf.add(foot,BorderLayout.SOUTH);
jf.setJMenuBar(craeteMenu());
//******************左边面板添加形状选择工具***************************************//
//添加图片及按钮
ButtonGroup group =new ButtonGroup();
//定义一个一维数组来存放工具栏图片的名字,数字表示还没有实现的按钮
String[] strs ={ "0", "1", "easer", "3", "4", "5", "pencial", "brush", "spray", "9",
"line", "11", "rect", "polygon", "oval", "roundrect"};
for(int i=0;i<16;i++){
JRadioButton btn1 =new JRadioButton();
left.add(btn1);
group.add(btn1);
btn1.setActionCommand(strs[i]);
btn1.setSelected(true);
// 设置按钮的图片
ImageIcon defaultIcon = new ImageIcon("img/draw"+i+".jpg");
btn1.setIcon(defaultIcon);
ImageIcon rolloverIcon =new ImageIcon("img/draw"+i+"-1.jpg");
btn1.setRolloverIcon(rolloverIcon);
ImageIcon pressedIcon = new ImageIcon("img/draw"+i+"-2.jpg");
btn1.setPressedIcon(pressedIcon);
ImageIcon selectedIcon =new ImageIcon("img/draw"+i+"-3.jpg");
btn1.setSelectedIcon(selectedIcon);
}
//******************中间面板添加***********************************************//
JPanel drawpJPanel=new JPanel();
Dimension drawDim=new Dimension(400,300);
drawpJPanel.setPreferredSize(drawDim);
drawpJPanel.setBackground(Color.WHITE);
// 面板默认的布局是流式布局
// 指定中间面板的布局为流式布局左对齐
FlowLayout f1=new FlowLayout(FlowLayout.LEFT);
center.setLayout(f1);
center.add(drawpJPanel);
//*******************底部面板添加颜色选择工具****************************************//
JLabel frontLabel =new JLabel();
JLabel backLabel=new JLabel();
// 使用绝对定位布局
foot.setLayout(null);
frontLabel.setBounds(50, 20, 40, 60);
backLabel.setBounds(110, 20, 40, 60);
frontLabel.setBackground(Color.BLACK);
backLabel.setBackground(Color.BLUE);
// 是否允许背景色显示出来
frontLabel.setOpaque(true);
backLabel.setOpaque(true);
foot.add(frontLabel);
foot.add(backLabel);
//颜色
Color[] cs={Color.BLACK, Color.GRAY, new Color(128, 0, 0),Color.RED,
new Color(255, 128, 0), Color.YELLOW, Color.GREEN,
new Color(0, 128, 255), Color.BLUE, Color.MAGENTA, new Color(255,128,128),
new Color(128, 0, 125), new Color(128, 255, 0), new Color(128, 0, 255),
new Color(0, 128, 120),new Color(128, 0, 20), new Color(128, 128, 0),
new Color(100, 128, 255), new Color(128, 30, 50), new Color(128, 110, 50)};
ColorListener mlis =new ColorListener(frontLabel,backLabel);
for(int i=0;i<20;i++){
JLabel colorLabel= new JLabel();
// 给颜色Label添加鼠标监听器
colorLabel.addMouseListener(mlis);
if(i<10){
colorLabel.setBounds(160+30*i, 20, 25, 25);
}else{
colorLabel.setBounds(160+30*(i-10), 50, 25, 25);
}
colorLabel.setBackground(cs[i]);
colorLabel.setOpaque(true);
foot.add(colorLabel);
}
jf.setVisible(true);
//调用创建监听器的方法,并传入参数
DrawListener dlis = new DrawListener(drawpJPanel,group,frontLabel,backLabel);
drawpJPanel.addMouseListener(dlis);
drawpJPanel.addMouseMotionListener(dlis);
}
//*******************菜单栏的实现****************************************//
/**
* 创建菜单栏
* @return 返回JMenuBar的对象
*/
private static JMenuBar craeteMenu() {
//实例化一个JMenuBar类的对象jmb
JMenuBar jmb=new JMenuBar();
String[] array={"文件","颜色","编辑","图像","帮助"};
String[][] array2={{"新建", "保存", "打开", "退出"},{"编辑颜色"},{"字体"},{"修改"},{"关于画图"}};
for(int i=0;i<array.length;i++){
JMenu jmenu=new JMenu(array[i]);
for(int j=0;j<array2[i].length;j++){
JMenuItem jmt=new JMenuItem(array2[i][j]);
jmenu.add(jmt);
}
jmb.add(jmenu);
}
return jmb;
}
}
package com.newer.yt.draw.copy;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* 画图的监听器
* 各种按钮功能的实现
* @author leaf-stop
*
*/
public class DrawListener extends MouseAdapter {
private int x1,y1,x2,y2,x3,y3;
private Graphics g;
private JPanel drawJPanel;
private ButtonGroup group;
private String str="line";
private Color frontColor=Color.RED;
private Color backColor=Color.BLUE;
private JLabel frontLabel;
private JLabel backLabel;
public static boolean flag;
//重载
public DrawListener(JPanel dp,ButtonGroup bg,JLabel fLabel,JLabel bLabel){
drawJPanel=dp;
group=bg;
frontLabel=fLabel;
backLabel=bLabel;
}
//鼠标按下
public void mousePressed(MouseEvent e){
// 鼠标按下准备绘制图形的时候先获取能绘制的区域[画布]
// 获取drawPanel在屏幕上占据的区域,作为可以改变颜色的区域
g=drawJPanel.getGraphics();
frontColor= frontLabel.getBackground();
backColor=backLabel.getBackground();
int num= e.getButton();
// 获得鼠标点击的是左键还是右键
if(num==1){
g.setColor(frontColor);
}else if(num==3)
g.setColor(backColor);
// 鼠标按下准备绘制的时候来确定要绘制的图形
// 获得被选中的按钮模型
ButtonModel model= group.getSelection();
// 获得动作命令[每一个按钮的唯一标识]
str=model.getActionCommand();
x1 = e.getX();
y1 = e.getY();
};
//释放
public void mouseReleased(MouseEvent e){
x2=e.getX();
y2=e.getY();
int x=x1<x2?x1:x2 ;
int y=y1<y2?y1:y2 ;
int width= x1<x2?x2-x1:x1-x2;
int height=y1<y2?y2-y1:y1-y2;
int arcWidth=10;
int arcHeight=10;
//直线,矩形,椭圆的绘制
if(str.equals("line")){//直线
g.drawLine(x1, y1, x2, y2);
}else if(str.equals("rect")){//矩形
g.drawRect(x, y, width, height);
}else if(str.equals("oval")){//椭圆
g.drawOval(x, y, width, height);
}else if(str.equals("roundrect")){//圆角矩形
g.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
}else if(str.equals("polygon")){//多边形
if(flag==false){
g.drawLine(x2, y2, x1, y1);
x3=x2;
y3=y2;
flag=true;
}else if(flag==true){
g.drawLine(x3,y3,x1,y1);
x3=x1;
y3=y1;
}
}
};
public void mouseDragged(MouseEvent e){
x2=e.getX();
y2=e.getY();
//刷子
if(str.equals("brush")){
Graphics2D g2d=(Graphics2D)g;
g2d.setStroke(new BasicStroke(8));
g2d.drawLine(x1, y1, x2, y2);
//将末位置变成下一次的初始位置
x1=x2;
y1=y2;
g2d.setStroke(new BasicStroke(1));
}
//橡皮擦
else if(str.equals("easer")){
g.setColor(Color.WHITE);
Graphics2D g2d=(Graphics2D)g;
g2d.setStroke(new BasicStroke(8));
g2d.drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
g2d.setStroke(new BasicStroke(1));
}
//铅笔
else if(str.equals("pencial")){
g.drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
}
//喷枪
else if(str.equals("spray")){
Random random=new Random();
for(int i=0;i<30;i++){
int x=random.nextInt(10);
int y=random.nextInt(10);
g.drawLine(x1+x, y1+y, x2+x, y2+y);
x1=x2;
y1=y2;
}
}
}
//进入
public void mouseEntered(MouseEvent e){
};
//离开
public void mouseExited(MouseEvent e){
};
//点击
public void mouseClicked(MouseEvent e){
}
}
package com.newer.yt.draw.copy;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
/**
* 颜色选择的监听器
* @author leaf-stop
*
*/
public class ColorListener implements MouseListener{
private JLabel frontLabel;
private JLabel backLabel;
public ColorListener(JLabel fLabel,JLabel bLabel){
frontLabel=fLabel;
backLabel=bLabel;
}
public void mouseReleased(MouseEvent e) {
// 获得事件源对象:发生事件的组件
// 由于监听器是加在Jlabel上的,所以事件源对象一定是JLabel
JLabel label=(JLabel)e.getSource();
Color c=label.getBackground();
// 获得鼠标是左键还是右键
int num = e.getButton();
if(num==1){
frontLabel.setBackground(c);
}else if(num==3){
backLabel.setBackground(c);
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
}