新写的代码,估计有些没有写完,比如菜单方面,但是界面绝对的仿xp画板,你可以看看我上传的画板界面图片
package drawpaintv1;
import java.awt.BasicStroke;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JRadioButton;
public class SelectLisenter implements ActionListener{
DrawingBorder DB;
String small[] = {"0","1","2","3","4"}; //画笔粗细的图片位置
String xiangpi[] = {"1","2","3","4"};//橡皮擦的图片位置
String juxing[] = {"00","01","02"};//矩形图片的位置
String ziti[] = {"1","2"};//字体的位置
public SelectLisenter(DrawingBorder DB) {
this.DB = DB;
}
//更新容器中的组件
public void UPdate() {
DB.J_select.removeAll();
javax.swing.SwingUtilities.updateComponentTreeUI(DB.J_select);
}
public void actionPerformed(ActionEvent e) {
String value = e.getActionCommand();
// g.setStroke(new BasicStroke(2));
//任意形状及选定以及字体
if(value.equals("0")||value.equals("1")||value.equals("9")){
UPdate();
DB.J_select.setLayout(new GridLayout(ziti.length,1,3,0));
ButtonGroup BG=new ButtonGroup();
for(int i=0;i<ziti.length;i++){
ImageIcon img=new ImageIcon("imges/矩形"+ziti[i]+".jpg");
JRadioButton bt= new JRadioButton(img);
BG.add(bt);
DB.J_select.add(bt);
bt.addActionListener(Ac);
javax.swing.SwingUtilities.updateComponentTreeUI(DB.J_select);
}
}
//橡皮及橡皮擦
if(DB.value.equals("2")){
UPdate();
DB.J_select.setLayout(new FlowLayout(FlowLayout.CENTER,6,0));
ButtonGroup BG=new ButtonGroup();
for(int i=0;i<xiangpi.length;i++){
ImageIcon img=new ImageIcon("imges/橡皮"+xiangpi[i]+".jpg");
JRadioButton bt= new JRadioButton(img);
bt.setBorder(null);
BG.add(bt);
DB.J_select.add(bt);
bt.addActionListener(Ac);
javax.swing.SwingUtilities.updateComponentTreeUI(DB.J_select);
}
}
//空白,铅笔,填充,取色
if(DB.value.equals("3")||value.equals("4")||value.equals("6")){
UPdate();
}
//直线及曲线
if(DB.value.equals("10")||DB.value.equals("11")){
UPdate();
DB.J_select.setLayout(new GridLayout(small.length,1,3,0));
ButtonGroup BG=new ButtonGroup();
for(int i=0;i<small.length;i++){
ImageIcon img=new ImageIcon("imges/"+small[i]+"号线.jpg");
JRadioButton bt= new JRadioButton(img);
BG.add(bt);
DB.J_select.add(bt);
bt.setActionCommand(""+i);
bt.addActionListener(Ac);
javax.swing.SwingUtilities.updateComponentTreeUI(DB.J_select);
}
}
//矩形及多变形,圆角矩形
if(DB.value.equals("12")||value.equals("13")||DB.value.equals("14")||DB.value.equals("15")){
UPdate();
DB.J_select.setLayout(new GridLayout(juxing.length,1,3,0));
ButtonGroup BG=new ButtonGroup();
for(int i=0;i<juxing.length;i++){
ImageIcon img=new ImageIcon("imges/矩形"+juxing[i]+".jpg");
JRadioButton bt= new JRadioButton(img);
BG.add(bt);
DB.J_select.add(bt);
bt.addActionListener(Ac);
javax.swing.SwingUtilities.updateComponentTreeUI(DB.J_select);
}
}
}
public ActionListener Ac = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String label = e.getActionCommand();
System.out.println("你好啊");
Graphics2D g = DB.g;
//笔粗细的设定
if(label.equals(small[0])) {
System.out.println("label="+label+"====="+small[0]);
g.setStroke(new BasicStroke(1));
}
if(label.equals(small[1])) {
System.out.println("label="+label+"====="+small[0]);
g.setStroke(new BasicStroke(2));
}
if(label.equals(small[2])) {
System.out.println("label="+label+"====="+small[0]);
g.setStroke(new BasicStroke(4));
}
if(label.equals(small[3])) {
System.out.println("label="+label+"====="+small[0]);
g.setStroke(new BasicStroke(6));
}
if(label.equals(small[4])) {
System.out.println("label="+label+"====="+small[0]);
g.setStroke(new BasicStroke(8));
}
}
};
}
package drawpaintv3;
import java.awt.BorderLayout;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
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.JToolBar;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
/**
* 定义一个画图的主界面类,该类继承自JFrame;那么DrawingBorder就是窗体了
*
* @author Administrator
*
*/
public class DrawingBorder extends JFrame {
private static final int VERTICAL = 0;
/**
* 主函数
*/
/**
* 初始化界面的方法
*/
public String str;
public JButton Bjtn ,Fjtn;//前景、背景按钮
public String value = "6",picture = "0";// 声明一个图形属性监听判断
public JPanel J_select; //选择粗细面板
public JPanel JC_Center;//设置一个共有的绘图板
public Graphics2D g; //画布
private Color bcolor = Color.BLACK;//按钮背景、前景颜色的初始化
private Color fcolor = Color.WHITE;
private int Xo = 600,Yo = 400;//绘图区的初始大小
public int dragx , dragy ;
String[] Menus = {"文件(F)","编辑(E)","查看(V)","图像(I)","颜色(C)","帮助(H)"}; //字符串,用于菜单的存放
//创建一个数组,用来存储图片的地址
String draw[][] = {
{"imges/draw0.jpg","imges/draw0-1.jpg","imges/draw0-2.jpg","imges/draw0-3.jpg"},
{"imges/draw1.jpg","imges/draw1-1.jpg","imges/draw1-2.jpg","imges/draw1-3.jpg"},
{"imges/draw2.jpg","imges/draw2-1.jpg","imges/draw2-2.jpg","imges/draw2-3.jpg"},
{"imges/draw3.jpg","imges/draw3-1.jpg","imges/draw3-2.jpg","imges/draw3-3.jpg"},
{"imges/draw4.jpg","imges/draw4-1.jpg","imges/draw4-2.jpg","imges/draw4-3.jpg"},
{"imges/draw5.jpg","imges/draw5-1.jpg","imges/draw5-2.jpg","imges/draw5-3.jpg"},
{"imges/draw6.jpg","imges/draw6-1.jpg","imges/draw6-2.jpg","imges/draw6-3.jpg"},
{"imges/draw7.jpg","imges/draw7-1.jpg","imges/draw7-2.jpg","imges/draw7-3.jpg"},
{"imges/draw8.jpg","imges/draw8-1.jpg","imges/draw8-2.jpg","imges/draw8-3.jpg"},
{"imges/draw9.jpg","imges/draw9-1.jpg","imges/draw9-2.jpg","imges/draw9-3.jpg"},
{"imges/draw10.jpg","imges/draw10-1.jpg","imges/draw10-2.jpg","imges/draw10-3.jpg"},
{"imges/draw11.jpg","imges/draw11-1.jpg","imges/draw11-2.jpg","imges/draw11-3.jpg"},
{"imges/draw12.jpg","imges/draw12-1.jpg","imges/draw12-2.jpg","imges/draw12-3.jpg"},
{"imges/draw13.jpg","imges/draw13-1.jpg","imges/draw13-2.jpg","imges/draw13-3.jpg"},
{"imges/draw14.jpg","imges/draw14-1.jpg","imges/draw14-2.jpg","imges/draw14-3.jpg"},
{"imges/draw15.jpg","imges/draw15-1.jpg","imges/draw15-2.jpg","imges/draw15-3.jpg"}};
//图片按钮的下标提示
String txt[] = {
"任意形状的裁剪","选定","橡皮/彩色橡皮擦","用颜色填充","取色","放大镜",
"铅笔","刷子","喷枪","文字","直线","曲线","矩形","多边形","椭圆","圆角矩形"};
//按钮颜色的初始化
Color col[] = {
Color.black,new Color(128,128,128),new Color(128,0,64),new Color(128,128,0),
new Color(0,128,0),new Color(64,128,128),new Color(0,0,128),new Color(128,0,128),
new Color(128,128,64),new Color(0,128,255),new Color(0,87,87),new Color(150,0,150),
new Color(128,64,0),new Color(255,255,255),new Color(192,192,192),new Color(128,128,128),
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,128),new Color(255,128,64),new Color(128,128,255),
new Color(128,255,255),new Color(255,255,128),new Color(128,255,128),new Color(200,240,240)};
//子菜单的字符串
String[][] MenuItems = {
{"新建(N) Ctrl+N","打开(O)... Ctrl+O","保存(S) Ctrl+S","另存为(A)...","从扫描仪或照相机(C)...","退出(X) Alt+F4"},
{"撤销(U) Ctrl+Z","剪切(T) Ctrl+X","复制(C) Ctrl+C","粘贴(P) Ctrl+V","全选(A) Ctrl+A","粘贴来源(F)..."},
{"工具箱(T) Ctrl+T","颜料盒(C) Ctrl+C","状态栏(S)","查看位图(V) Ctrl+F"},
{"翻转/旋转(F)...Ctrl+R","拉伸/扭曲(S)...Ctrl+W","反色(I) Ctrl+I","属性(A)...Ctrl","清除图像(C) Ctrl+Shift+N","不透明处理(D)",},
{"编辑颜色(E)..."},{"帮助主题(H)","关于画图(A)"}};
// Image img = JC_Center.createImage(JC_Center.getWidth(), JC_Center.getHeight());
public static void main(String[] args) {
//函数的入口
DrawingBorder DB = new DrawingBorder();
DB.initUI();
}
private void initUI() {
//初始化窗体
ImageIcon ion = new ImageIcon();
this.setTitle("简单画板");
this.setSize(800, 600);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setLayout(new BorderLayout());
/**
* 实例化一个放置在北边的面板对象,该面板对象上要放置图形和颜色按钮
*/
//菜单栏
JMenuBar J_menubar=new JMenuBar();
for(int i=0;i<Menus.length;i++) {
JMenu J_men=new JMenu(Menus[i]);
J_men.setFont(new Font("新宋体",5,12));
for(int j=0;j<MenuItems[i].length;j++){
JMenuItem JM_item=new JMenuItem(MenuItems[i][j]);
JM_item.setFont(new Font("新宋体",5,12));
JM_item.setBackground(Color.WHITE);
J_men.add(JM_item);
}
J_menubar.add(J_men);
}
this.add(J_menubar,BorderLayout.NORTH);
JToolBar J_bt = new JToolBar("工具",1);
J_bt.setOrientation(0);
J_bt.setPreferredSize(new Dimension(60,300));
J_bt.setBorder(null);
//设定一个画板放于西面,用于固定西面的宽度
JPanel Jpanel_West = new JPanel();
Jpanel_West.setPreferredSize( new Dimension(60,0) );
// Jpanel_West.setLayout(new BorderLayout());
J_bt.add(Jpanel_West);
//选择粗细面板
J_select=new JPanel();
J_select.setPreferredSize(new Dimension(45,72));
J_select.setBorder(new BevelBorder(0, Color.DARK_GRAY, Color.WHITE));
//图形选择面板,用于添加按钮
JPanel Jpanel_Bt=new JPanel();
// Jpanel_Bt.setPreferredSize(new Dimension(60,150));
Jpanel_West.setBorder(new LineBorder(new Color(180,180,180)));
Jpanel_Bt.setLayout(new GridLayout(8,2,0,0));
// 实例化按钮群对象
ButtonGroup jbp=new ButtonGroup();
for(int i=0;i<draw.length;i++) {
ImageIcon img0=new ImageIcon(draw[i][0]);
ImageIcon img1=new ImageIcon(draw[i][1]);
ImageIcon img2=new ImageIcon(draw[i][2]);
ImageIcon img3=new ImageIcon(draw[i][3]);
JRadioButton btn= new JRadioButton(img0);
btn.setBorder(null);
btn.setIcon(img0);
btn.setPressedIcon(img2);
btn.setRolloverIcon(img1);
btn.setSelectedIcon(img3);
btn.setToolTipText(txt[i]);
jbp.add(btn);
Jpanel_Bt.add(btn);// 将按钮添加到panelNorth
// 给按钮添加动作监听器方法,绑定事件处理类的对象al
btn.setActionCommand(draw[i][0].substring(draw[i][0].indexOf("w")+1,draw[i][0].lastIndexOf(".")));
btn.addActionListener(al);
// 将panelNorth添加到窗体的北边
}
Jpanel_West.add(Jpanel_Bt,BorderLayout.NORTH);
Jpanel_West.add(J_select,BorderLayout.CENTER);
this.add(J_bt, BorderLayout.WEST);
//南面画板,用于固定高度
JPanel Jpanel_south=new JPanel();
Jpanel_south.setLayout(new BorderLayout());
JToolBar Jt = new JToolBar("颜色",0);
Jt.setPreferredSize(new Dimension(262,48));
Jt.setBorder(null);
// Jt.setLayout(null);
//创建一个颜色选择板,添加到北面
JPanel JS_North=new JPanel();
JS_North.setLayout(new FlowLayout(FlowLayout.LEADING,0,8));//设置布局为流布局切靠左对齐
JS_North.setPreferredSize(new Dimension(262,48));
JS_North.setBorder(new LineBorder(new Color(180,180,180)));
//帮助信息栏,添加到西面
JPanel JS_West=new JPanel();
JS_West.setPreferredSize(new Dimension(0,22));
JS_West.setLayout(new FlowLayout(FlowLayout.LEFT));
JS_West.setBackground(Color.GRAY);
JS_West.setBorder(new LineBorder(new Color(180,180,180)));
//坐标显示,添加到中间
JPanel JS_Center=new JPanel(){
public void paint(Graphics g) {
super.paint(g);
g.drawString(""+dragx+","+dragy+"",50 , 11);
}
};
JS_Center.setPreferredSize(new Dimension(100,22));
JS_Center.setBorder(new LineBorder(new Color(180,180,180)));
JS_Center.setLayout(null);
// Graphics drag = JS_Center.getGraphics();
//以鼠标单击点为原点,显示坐标,添加到东面
JPanel JS_East=new JPanel();
JS_East.setPreferredSize(new Dimension(100,22));
JS_East.setBorder(new LineBorder(new Color(180,180,180)));
// Jt.setBackground(Color.BLUE);
Jt.add(JS_North);
Jpanel_south.add(Jt, BorderLayout.NORTH);
Jpanel_south.add(JS_West, BorderLayout.WEST);
Jpanel_south.add(JS_Center, BorderLayout.CENTER);
Jpanel_south.add(JS_East, BorderLayout.EAST);
//添加到南面
this.add(Jpanel_south, BorderLayout.SOUTH);
//背景按钮画板
JPanel BF_Jpanel=new JPanel();
BF_Jpanel.setLayout(null);
BF_Jpanel.setBorder(new BevelBorder(0, Color.DARK_GRAY, Color.WHITE));
BF_Jpanel.setBackground(new Color(245,245,245));
BF_Jpanel.setPreferredSize(new Dimension(30,30));
//背景按钮
Bjtn=new JButton();
Bjtn.setBackground(bcolor);
Bjtn.setBounds(5, 5, 13, 13);
Bjtn.setEnabled(false);
Bjtn.setBorder(new BevelBorder(0, new Color(120,120,120), Color.DARK_GRAY));
//前景按钮
Fjtn=new JButton();
Fjtn.setBackground( fcolor);
Fjtn.setBounds(11, 11, 14, 14);
Fjtn.setEnabled(false);
Fjtn.setBorder(new BevelBorder(0, new Color(120,120,120), Color.DARK_GRAY));
BF_Jpanel.add(Bjtn);
BF_Jpanel.add(Fjtn);
JS_North.add(BF_Jpanel);
Jbuttonlistener Jl=new Jbuttonlistener(this);//按钮鼠标监听
//创建一个颜色按钮板
JPanel Col_Jpanel=new JPanel();
Col_Jpanel.setPreferredSize(new Dimension(230,30));
Col_Jpanel.setLayout(new GridLayout(2,14,0,0));
for(int i=0;i<col.length;i++){
JButton btnColor = new JButton();
btnColor.setBackground(col[i]);
btnColor.setBorder(new BevelBorder(0, Color.DARK_GRAY,Color.WHITE));
Col_Jpanel.add(btnColor);
btnColor.addMouseListener(Jl);
}
JS_North.add(Col_Jpanel);
/**
* 实例化一个放置在中间的面板对象,该面板是用来绘图的
*/
//创建一个背景灰色的底板
JPanel Jpanel_Center = new JPanel();
Jpanel_Center.setBackground(Color.gray);//设置背景颜色为灰色
Jpanel_Center.setLayout(new FlowLayout(FlowLayout.LEFT));//设置为流布局,且靠左对齐
//中间绘图画板
JC_Center = new JPanel();
JC_Center.setLayout(null);
JC_Center.setBackground(Color.WHITE);// 设置按钮的背景色为白色
JC_Center.setPreferredSize(new Dimension(Xo,Yo));
Jpanel_Center.add(JC_Center);// 将panelCenter添加到窗体的中央
this.add(Jpanel_Center, BorderLayout.CENTER);
this.setVisible(true);
//要再窗体可见之后才能获取画布对象
Graphics g1 = JC_Center.getGraphics();
g=(Graphics2D)g1;
DrawingListener dl = new DrawingListener(this,g);
//实例化一个DrawingListener类的对象,并且传递参数
//给panelCenter添加鼠标监听器方法,绑定事件处理类对象dl
JC_Center.addMouseListener(dl);
JC_Center.addMouseMotionListener(dl);
Drawthread Td = new Drawthread(this);
Td.start();
}
/**
* 此处使用匿名内部类,用来对图形进行监听,同时对选着画板给出选择图形
*/
public ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
value = e.getActionCommand();
SelectLisenter SL = new SelectLisenter(DrawingBorder.this);
SL.actionPerformed(e);
}
};
}
package drawpaintv1;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JColorChooser;
public class Jbuttonlistener extends MouseAdapter {
DrawingBorder DB;
private Color bcolor;//按钮的背景颜色
public Jbuttonlistener(DrawingBorder DB) {
this.DB = DB;
}
public void mouseClicked(MouseEvent e) {
//按钮背景颜色的处理
JButton btn = (JButton) e.getSource();
if (e.getClickCount() == 2) {
bcolor = JColorChooser.showDialog(null, "颜色选择", Color.BLACK);
btn.setBackground(bcolor);
if (e.getButton() == 1) {
DB.Bjtn.setBackground(bcolor);
}
if(e.getButton()==3){
DB.Fjtn.setBackground(bcolor);
}
}
}
public void mousePressed(MouseEvent e) {
int a = e.getButton();
// 获取事件源对象
JButton btn = (JButton) e.getSource();
if (a == 1) {
DB.Bjtn.setBackground(btn.getBackground());
} else if (a == 3) {
DB.Fjtn.setBackground(btn.getBackground());
}
}
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
}
public void mouseMoved(MouseEvent e) {
super.mouseMoved(e);
}
}