您的位置:首页 > 编程语言 > Java开发

画图板的简单实现

2016-06-29 13:11 579 查看
新手入门,画图板感觉不失为一个好的小项目,值得做做。

 简单粗暴,先看一下实现效果



很简单的布局

直接给出代码吧

DrowBoard.javapackage cn.sg.DrawBoard;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;

import javax.swing.*;

public class DrawBoard extends JFrame{
public Graphics g;
public static void main(String[]args){
DrawBoard bd=new DrawBoard();
bd.draw();
}
//绘制画板
public void draw(){

this.setTitle("画板");
this.setSize(800,650);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
//设置布局
BorderLayout b1=new BorderLayout();
this.setLayout(b1);
//添加面板
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
JPanel jp3=new JPanel();

FlowLayout fl = new FlowLayout(FlowLayout.LEFT,0,0);
jp1.setLayout(fl);

this.add(jp1,BorderLayout.WEST);
this.add(jp2,BorderLayout.SOUTH);
this.add(jp3,BorderLayout.CENTER);
//设置面板的颜色
jp1.setBackground(new Color(240,240,240));
jp2.setBackground(Color.LIGHT_GRAY);
jp3.setBackground(Color.white);
//设置面板的大小
Dimension dimen1=new Dimension(70,0);
jp1.setPreferredSize(dimen1);
Dimension dimen2=new Dimension(0,70);
jp2.setPreferredSize(dimen2);

//创建图形按钮
ButtonGroup bg=new ButtonGroup();

String []common={"五角星","矩形","eraser","pengtong","getcolor","五角星","pencil","brush","五角星","直线","直线","五角星","矩形","manyOfBian","五角星","直线"};
for(int i=0;i<common.length;i++){
JRadioButton jr=new JRadioButton();
if(i==10){
jr.setSelected(true);
}
ImageIcon image1=new ImageIcon("images/draw"+i+".jpg");
ImageIcon image2=new ImageIcon("images/draw"+i+"-1.jpg");
ImageIcon image3=new ImageIcon("images/draw"+i+"-2.jpg");
ImageIcon image4=new ImageIcon("images/draw"+i+"-3.jpg");
jr.setIcon(image1);
jr.setRolloverIcon(image2);
jr.setPressedIcon(image3);
jr.setSelectedIcon(image4);
jr.setActionCommand(common[i]);
jp1.add(jr);
bg.add(jr);
}

myActionListener action1=new myActionListener(this);

FlowLayout f2 = new FlowLayout(FlowLayout.LEFT,0,0);
jp2.setLayout(f2);
JPanel jp4=new JPanel();
jp4.setBackground(Color.LIGHT_GRAY);
Dimension dimen4=new Dimension(300,50);
jp4.setPreferredSize(dimen4);
jp2.add(jp4);
Color[] colors = {Color.BLACK,Color.BLUE,Color.cyan,Color.DARK_GRAY,Color.GRAY,
Color.GREEN,Color.LIGHT_GRAY,Color.lightGray,Color.magenta,
Color.orange,Color.PINK,Color.RED,Color.GREEN,Color.YELLOW,
Color.blue,Color.black,Color.red,Color.green,Color.orange,
Color.black,Color.GREEN,Color.LIGHT_GRAY,Color.lightGray,Color.magenta};
for(int i=0;i<colors.length;i++){
JButton jb = new JButton();
Dimension dimen = new Dimension(20,20);
jb.setPreferredSize(dimen);
jb.setBackground(colors[i]);
jp4.add(jb);
jb.addActionListener(action1);
}

this.setVisible(true);
g=jp3.getGraphics();
//添加鼠标监听
myMouselistener m=new myMouselistener(g,bg);
jp3.addMouseListener(m);
jp3.addMouseMotionListener(m);

}

}


 myMouselistener.java
package cn.sg.DrawBoard;
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.Random;

import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;

public class myMouselistener implements MouseListener,MouseMotionListener {
public  Graphics2D g1;
public  int  x1,x2,y1,y2,x3,y3,x4,y4;
public  ButtonGroup bg1;
public  String command;
public  boolean bool=true;
public  myMouselistener(Graphics g,ButtonGroup bg){
g1=(Graphics2D)g;
bg1=bg;
}

public void mousePressed(MouseEvent e){
x1=e.getX();
y1=e.getY();
//获取被选中的按钮,并得到命令
ButtonModel bm = bg1.getSelection();
command = bm.getActionCommand();
};
public void mouseReleased(MouseEvent e){
x2=e.getX();
y2=e.getY();
if("直线".equals(command))
//画直线
g1.drawLine(x1, y1, x2, y2);
else if("矩形".equals(command))
//画矩形
g1.drawRect(Math.min(x1,x2),Math.min(y1,y2), Math.abs(x2-x1), Math.abs(y2-y1));
//画五角星
else if("五角星".equals(command))
{g1.drawLine(x1,(3*y1+y2)/4,(2*x1+x2)/3,(3*y1+y2)/4);
g1.drawLine((2*x1+x2)/3,(3*y1+y2)/4,(x1+x2)/2,y1);
g1.drawLine((x1+x2)/2,y1,(x1+2*x2)/3,(3*y1+y2)/4);
g1.drawLine((x1+2*x2)/3,(3*y1+y2)/4,x2,(3*y1+y2)/4);
g1.drawLine(x2,(3*y1+y2)/4,(x1+2*x2)/3,(y1+y2)/2);
g1.drawLine((x1+2*x2)/3,(y1+y2)/2,x2,(y1+3*y2)/4);
g1.drawLine(x2,(y1+3*y2)/4,(x1+2*x2)/3,(y1+3*y2)/4);
g1.drawLine((x1+2*x2)/3,(y1+3*y2)/4,(x1+x2)/2,y2);
g1.drawLine((x1+x2)/2,y2,(2*x1+x2)/3,(y1+3*y2)/4);
g1.drawLine((2*x1+x2)/3,(y1+3*y2)/4,x1,(y1+3*y2)/4);
g1.drawLine(x1,(y1+3*y2)/4,(2*x1+x2)/3,(y1+y2)/2);
g1.drawLine((2*x1+x2)/3,(y1+y2)/2,x1,(3*y1+y2)/4);}
else if("manyOfBian".equals(command)){

if(bool){
g1.drawLine(x1, y1, x2, y2);
x4=x1;
y4=y1;
bool=false;
}
else {
g1.drawLine(x3, y3, x2, y2);
}
x3=x2;
y3=y2;
}

};

public void mouseClicked(MouseEvent e){

if("manyOfBian".equals(command))
{    x2=e.getX();
y2=e.getY();
int count=e.getClickCount();
if(count==2){
g1.drawLine(x4,y4,x2,y2);
bool=true;
}
else if("getcolor".equals(command)){

try {
Robot robot=new Robot();
Rectangle rect=new Rectangle(e.getXOnScreen(),e.getYOnScreen(),1,1);
BufferedImage bi=robot.createScreenCapture(rect);
int value=bi.getRGB(0, 0);
Color color=new Color(value);
g1.setColor(color);
} catch (AWTException e1) {

e1.printStackTrace();
}

}

}}
public void mouseEntered(MouseEvent e){};
public void mouseExited(MouseEvent e){}

public void mouseDragged(MouseEvent e) {

if("pencil".equals(command))
{    x2=e.getX();
y2=e.getY();
g1.drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;}

else if("eraser".equals(command))
{    x2=e.getX();
y2=e.getY();
BasicStroke s=new BasicStroke (10);
g1.setStroke(s);
Color color1=g1.getColor();
g1.setColor(Color.white);
g1.drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
s=new BasicStroke (1);
g1.setStroke(s);
g1.setColor(color1);

}
else if("brush".equals(command))
{    x2=e.getX();
y2=e.getY();
BasicStroke s=new BasicStroke (10);
g1.setStroke(s);
g1.drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
s=new BasicStroke (1);
g1.setStroke(s);
}
else if("pengtong".equals(command))
{    Random ran=new Random();
for(int i=1;i<=50;i++){
x2=e.getX();
y2=e.getY();
int x=ran.nextInt(20)-10;
int y=ran.nextInt(20)-10;
g1.drawLine(x2+x, y2+y, x2+x, y2+y);

}

}

}

public void mouseMoved(MouseEvent e) {

};
}


myActionListener.java

package cn.sg.DrawBoard;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

public class myActionListener implements ActionListener{
public DrawBoard g2;
public myActionListener(DrawBoard g){
g2=g;
}

public void actionPerformed(ActionEvent e) {

Object obj=e.getSource();
JButton jb=(JButton) obj;
Color c=jb.getBackground();
g2.g.setColor(c);

}

}


好像没有加注释!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java java 窗体 画板