package Draw8;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class draw8 {
//设置全局变量
Graphics g;
int x1,x2,y1,y2;
String command="7";
String command1="0";
Color[] colors={Color.red,Color.blue,Color.green,Color.pink,Color.yellow,Color.cyan,Color.black,Color.gray,Color.orange,Color.lightGray};
JPanel draw;
public void showUI(){
//创建窗体,并设置标题 大小 位置居中 关闭操作设置成退出程序
JFrame f=new JFrame("画图板");
f.setSize(400, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(3);
//创建三个面板 放图标按钮面板 颜色的按钮面板 作画的面板
JPanel shape=new JPanel();
JPanel yanse=new JPanel();
draw=new JPanel();
//添加三个面板在窗体上,并将三个面板分别放在西面 南面 中间
f.add(shape,BorderLayout.WEST);
f.add(yanse,BorderLayout.SOUTH);
f.add(draw,BorderLayout.CENTER);
//调用方法setshape来装饰shape面板 调用setyanse方法来修饰yanse面板
setshape(shape);
setyanse(yanse);
//设置作图面板的背景颜色为白色
draw.setBackground(Color.white);
//添加监听器到画板上
draw.addMouseListener(mouse);
draw.addMouseMotionListener(m);
//使窗体可见
f.setVisible(true);
g=draw.getGraphics();
}
//设置shape面板方法
public void setshape(JPanel panel){
//设置面板尺寸
panel.setPreferredSize(new Dimension(80,400));
//设置面板背景颜色
panel.setBackground(Color.pink);
//在面板上添加按钮
for(int i=1;i<17;i++){
//创建按钮
JButton buttons=new JButton();
//设置按钮尺寸
buttons.setPreferredSize(new Dimension(26,26));
//吧图标放在按钮上 先创建后安装
ImageIcon icon=new ImageIcon("image//"+i+".png");
buttons.setIcon(icon);
buttons.addActionListener(action);
buttons.setActionCommand(i+"");
//添加按钮到面板上
panel.add(buttons);
}
}
//设置yanse面板方法
public void setyanse(JPanel panel){
//设置面板尺寸
panel.setPreferredSize(new Dimension(400,50));
//设置面板背景颜色
panel.setBackground(Color.gray);
for(int i=0;i<10;i++){
//创建按钮
JButton color=new JButton();
//设置按钮背景图片
color.setBackground(colors[i]);
//设置按钮尺寸
color.setPreferredSize(new Dimension(30,20));
color.addActionListener(action1);
color.setActionCommand(i+"");
//添加按钮到面板上
panel.add(color);
}
}
//创建监听器 放在形状按钮上的监听器
ActionListener action=new ActionListener(){
public void actionPerformed(ActionEvent e){
//点击便获得标记 字符串command接收
command=e.getActionCommand();
}
};
//创建监听器 放在颜色按钮上的监听器
ActionListener action1=new ActionListener(){
public void actionPerformed(ActionEvent e){
command1=e.getActionCommand();
for(int i=0;i<10;i++){
if(command1.equals(i+"")){
g.setColor(colors[i]);
}
}
}
};
MouseListener mouse=new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
x1=e.getX();
y1=e.getY();
if(command1.equals("0")){
g.setColor(colors[0]);
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
x2=e.getX();
y2=e.getY();
if(command.equals("1")){}
if(command.equals("2")){
int xmin=Math.min(x1, x2),a=xmin,xmax=Math.max(x1,x2),b=xmax,ymin=Math.min(y1, y2),c=ymin,ymax=Math.max(y1, y2),d=ymax;
/*for(int i=a;i<b;i+=8){
g.drawLine(xmin,y1,xmin+4,y1);
g.drawLine(xmin,y2,xmin+4,y2);
xmin=xmin+8;
}
for(int j=c;j<=d;j+=8){
g.drawLine(x1,ymin,x1,ymin+4);
g.drawLine(x2,ymin,x2,ymin+4);
ymin=ymin+8;
}*/
for(int i=a;i<b;i+=3){
g.drawLine(xmin,y1,xmin,y1);
g.drawLine(xmin,y2,xmin,y2);
xmin=xmin+3;
}
for(int j=c;j<=d;j+=3){
g.drawLine(x1,ymin,x1,ymin);
g.drawLine(x2,ymin,x2,ymin);
ymin=ymin+3;
}
}
if(command.equals("3")){g.setColor(Color.white);
g.fillRect(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));
for(int i=0;i<10;i++){
if(command1.equals(i+"")){
g.setColor(colors[i]);
}
}
}
if(command.equals("4")){}
if(command.equals("5")){}
if(command.equals("6")){}
if(command.equals("10")){}
if(command.equals("11")){g.drawLine(x1,y1,x2,y2);}
if(command.equals("12")){}
if(command.equals("13")){g.drawRect(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));}
if(command.equals("14")){}
if(command.equals("15")){g.drawOval(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));}
if(command.equals("16")){g.drawRoundRect(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1),Math.abs(x2-x1)/4,Math.abs(y2-y1)/4);}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
};
MouseMotionListener m=new MouseMotionListener(){
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
x2=e.getX();
y2=e.getY();
if(command.equals("7")){
g.drawLine(x1,y1,x2,y2);
x1=x2;
y1=y2;
}
/*if(command.equals("8")){
g.drawLine(x1,y1,x2,y2);
for(int i=0;i<4;i++){
g.drawLine(x1,y1-i,x2,y2-i);
g.drawLine(x1-i,y1,x2-i,y2);
g.drawLine(x1,y1+i,x2,y2+i);
g.drawLine(x1+i,y1,x2+i,y2);
x1=x2;
y1=y2;
}
}*/
}
@Override
public void mouseMoved(MouseEvent e){
// TODO Auto-generated method stub
}
};
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class draw8 {
//设置全局变量
Graphics g;
int x1,x2,y1,y2;
String command="7";
String command1="0";
Color[] colors={Color.red,Color.blue,Color.green,Color.pink,Color.yellow,Color.cyan,Color.black,Color.gray,Color.orange,Color.lightGray};
JPanel draw;
public void showUI(){
//创建窗体,并设置标题 大小 位置居中 关闭操作设置成退出程序
JFrame f=new JFrame("画图板");
f.setSize(400, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(3);
//创建三个面板 放图标按钮面板 颜色的按钮面板 作画的面板
JPanel shape=new JPanel();
JPanel yanse=new JPanel();
draw=new JPanel();
//添加三个面板在窗体上,并将三个面板分别放在西面 南面 中间
f.add(shape,BorderLayout.WEST);
f.add(yanse,BorderLayout.SOUTH);
f.add(draw,BorderLayout.CENTER);
//调用方法setshape来装饰shape面板 调用setyanse方法来修饰yanse面板
setshape(shape);
setyanse(yanse);
//设置作图面板的背景颜色为白色
draw.setBackground(Color.white);
//添加监听器到画板上
draw.addMouseListener(mouse);
draw.addMouseMotionListener(m);
//使窗体可见
f.setVisible(true);
g=draw.getGraphics();
}
//设置shape面板方法
public void setshape(JPanel panel){
//设置面板尺寸
panel.setPreferredSize(new Dimension(80,400));
//设置面板背景颜色
panel.setBackground(Color.pink);
//在面板上添加按钮
for(int i=1;i<17;i++){
//创建按钮
JButton buttons=new JButton();
//设置按钮尺寸
buttons.setPreferredSize(new Dimension(26,26));
//吧图标放在按钮上 先创建后安装
ImageIcon icon=new ImageIcon("image//"+i+".png");
buttons.setIcon(icon);
buttons.addActionListener(action);
buttons.setActionCommand(i+"");
//添加按钮到面板上
panel.add(buttons);
}
}
//设置yanse面板方法
public void setyanse(JPanel panel){
//设置面板尺寸
panel.setPreferredSize(new Dimension(400,50));
//设置面板背景颜色
panel.setBackground(Color.gray);
for(int i=0;i<10;i++){
//创建按钮
JButton color=new JButton();
//设置按钮背景图片
color.setBackground(colors[i]);
//设置按钮尺寸
color.setPreferredSize(new Dimension(30,20));
color.addActionListener(action1);
color.setActionCommand(i+"");
//添加按钮到面板上
panel.add(color);
}
}
//创建监听器 放在形状按钮上的监听器
ActionListener action=new ActionListener(){
public void actionPerformed(ActionEvent e){
//点击便获得标记 字符串command接收
command=e.getActionCommand();
}
};
//创建监听器 放在颜色按钮上的监听器
ActionListener action1=new ActionListener(){
public void actionPerformed(ActionEvent e){
command1=e.getActionCommand();
for(int i=0;i<10;i++){
if(command1.equals(i+"")){
g.setColor(colors[i]);
}
}
}
};
MouseListener mouse=new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
x1=e.getX();
y1=e.getY();
if(command1.equals("0")){
g.setColor(colors[0]);
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
x2=e.getX();
y2=e.getY();
if(command.equals("1")){}
if(command.equals("2")){
int xmin=Math.min(x1, x2),a=xmin,xmax=Math.max(x1,x2),b=xmax,ymin=Math.min(y1, y2),c=ymin,ymax=Math.max(y1, y2),d=ymax;
/*for(int i=a;i<b;i+=8){
g.drawLine(xmin,y1,xmin+4,y1);
g.drawLine(xmin,y2,xmin+4,y2);
xmin=xmin+8;
}
for(int j=c;j<=d;j+=8){
g.drawLine(x1,ymin,x1,ymin+4);
g.drawLine(x2,ymin,x2,ymin+4);
ymin=ymin+8;
}*/
for(int i=a;i<b;i+=3){
g.drawLine(xmin,y1,xmin,y1);
g.drawLine(xmin,y2,xmin,y2);
xmin=xmin+3;
}
for(int j=c;j<=d;j+=3){
g.drawLine(x1,ymin,x1,ymin);
g.drawLine(x2,ymin,x2,ymin);
ymin=ymin+3;
}
}
if(command.equals("3")){g.setColor(Color.white);
g.fillRect(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));
for(int i=0;i<10;i++){
if(command1.equals(i+"")){
g.setColor(colors[i]);
}
}
}
if(command.equals("4")){}
if(command.equals("5")){}
if(command.equals("6")){}
if(command.equals("10")){}
if(command.equals("11")){g.drawLine(x1,y1,x2,y2);}
if(command.equals("12")){}
if(command.equals("13")){g.drawRect(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));}
if(command.equals("14")){}
if(command.equals("15")){g.drawOval(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));}
if(command.equals("16")){g.drawRoundRect(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1),Math.abs(x2-x1)/4,Math.abs(y2-y1)/4);}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
};
MouseMotionListener m=new MouseMotionListener(){
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
x2=e.getX();
y2=e.getY();
if(command.equals("7")){
g.drawLine(x1,y1,x2,y2);
x1=x2;
y1=y2;
}
/*if(command.equals("8")){
g.drawLine(x1,y1,x2,y2);
for(int i=0;i<4;i++){
g.drawLine(x1,y1-i,x2,y2-i);
g.drawLine(x1-i,y1,x2-i,y2);
g.drawLine(x1,y1+i,x2,y2+i);
g.drawLine(x1+i,y1,x2+i,y2);
x1=x2;
y1=y2;
}
}*/
}
@Override
public void mouseMoved(MouseEvent e){
// TODO Auto-generated method stub
}
};
}
下面是进入画板入口
public class text {
public static void main(String[] args) {
// TODO Auto-generated method stub
draw8 d=new draw8();
d.showUI();
}
}