引子:
感谢阅读
希望作为读者的你们可以多多关注我的作品,并留言或者加我qq一起学习
因为之前自己在学习的时候总是觉得有些博主们讲的有些快,所以我会讲的比较详细,能力好的可以快速掠过~~
若需转载请注明来处:http://blog.csdn.net/SX_csu2016sw
希望你们多多留言,你们的鼓励与指出的不足都是我前进的动力
这个画图板是我好久之前做的,之后浙大的同学需要做课设然后就花了一点时间将它改了一下,变得简单些能够方便扩充功能,同时学习java基础
先截图一下吧,就可以知道有哪些功能了~
三个分区,上面选择图形,下面选择颜色,立体圆就是一个分形,也先放着不需要的同学可以注释了它
代码很简单,就是JPanel进行分区,得到画笔,同时使用画图的函数就可以做到了
贴代码应该很快就会了~
主类
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawMain extends JPanel{
public static void main(String[] args) {
// TODO Auto-generated method stub
DrawMain Draw=new DrawMain();
Draw.InitUI();
}
public void InitUI(){
JFrame jf=new JFrame();
jf.setSize(1000,780);
jf.setTitle("简单画板");
jf.setDefaultCloseOperation(3);
jf.setLocationRelativeTo(null);
jf.setLayout(new BorderLayout());
//实例化事件监听类
DrawListener dl=new DrawListener(this);
//实现中间面板
this.setBackground(Color.WHITE);
jf.add(this,BorderLayout.CENTER);
//实现性状面板
JPanel ShapePanel=new JPanel();
ShapePanel.setBackground(Color.black);
ShapePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
ShapePanel.setBackground(Color.gray);;
String [] Shape={"直线","曲线","圆","喷枪","橡皮擦","矩形","椭圆","圆角矩形","弧线","多边形","图形","三角形","立体圆",};
for(int i=0;i
监听辅助类
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JButton;
public class DrawListener extends MouseAdapter implements ActionListener{
private int x1, y1, x2, y2;
private int newx1,newy1,newx2,newy2;
private Graphics2D g;
private DrawMain df;
private boolean flag=false;
String shape="直线";
Color color;
private int [] arrx=new int[4];
private int [] arry=new int[4];
private int temp=0;
DrawListener(DrawMain d){
df=d;
}
//获取形状和颜色
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("")){
JButton button = (JButton) e.getSource();
color = button.getBackground();
System.out.println("color = " + color);
}else{
JButton button = (JButton) e.getSource();
shape = button.getActionCommand();
System.out.println("String = " + shape);
}
}
//实现画笔
public void mousePressed(MouseEvent e) {
g=(Graphics2D) df.getGraphics();
g.setColor(color);
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
if (shape.equals("直线")) {
g.drawLine(x1, y1, x2, y2);
}else if(shape.equals("弧线")){
g.drawArc(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1), 0, 180);
}else if(shape.equals("多边形")&&!flag){
g.drawLine(x1, y1, x2, y2);
newx1=x1;
newy1=y1;
newx2=x2;
newy2=y2;
flag=true;
}else if(shape.equals("圆")){
g.drawOval(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
}else if(shape.equals("矩形")){
g.drawRect(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
}else if(shape.equals("圆角矩形")){
g.drawRoundRect(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1),2,10);
}else if(shape.equals("椭圆")){
g.drawOval(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
}
}
public void mouseClicked(MouseEvent e) {
if(shape.equals("多边形")&&flag){
x2=e.getX();
y2=e.getY();
if(e.getClickCount()==2){
g.drawLine(newx1, newy1, newx2, newy2);
flag=false;
}
g.drawLine(newx2, newy2, x2, y2);
newx2=x2;
newy2=y2;
}else if(shape.equals("图形")){
arrx[temp]=e.getX();
arry[temp]=e.getY();
temp++;
if(temp==4){
int x=arrx[3];
int y=arry[3];
for(int i=0;i<=10000;i++){
Random ran=new Random();
int k=ran.nextInt(3);
x=(x+arrx[k])/2;
y=(y+arry[k])/2;
g.drawLine(x, y, x, y);
}
temp=0;
}
}else if(shape.equals("立体圆")){
// double a=-2,b=-2,c=-1.2,d=2;
double a=1.40,b=1.56,c=1.40,d=-6.56;
double x = 0,xo=0;
double y = 0,yo=0;
Color [] Col={Color.BLUE,Color.cyan,Color.green,Color.magenta,Color.red,Color.yellow};
for(int i=0;i<=90000;i++){
Random r=new Random(); //增加颜色
int R=r.nextInt(Col.length);
g.setColor(Col[R]);
// x=Math.sin(a*yo)-Math.cos(b*xo);
// y=Math.sin(c*xo)-Math.cos(d*yo);
x=d*Math.sin(a*xo)-Math.sin(b*yo);
y=c*Math.cos(a*xo)+Math.cos(b*yo);
int temp_x=(int)(x*50);
int temp_y=(int)(y*50);
g.drawLine(temp_x+500, temp_y+300, temp_x+500, temp_y+300);
xo=x;
yo=y;
}
}else if(shape.equals("三角形")){
double a=-2,b=-2,c=-1.2,d=2;
double x = 0,xo=0;
double y = 0,yo=0;
Color [] Col={Color.BLUE,Color.cyan,Color.green,Color.magenta,Color.red,Color.yellow};
for(int i=0;i<=90000;i++){
Random r=new Random(); //增加颜色
int R=r.nextInt(Col.length);
g.setColor(Col[R]);
x=Math.sin(a*yo)-Math.cos(b*xo);
y=Math.sin(c*xo)-Math.cos(d*yo);
int temp_x=(int)(x*50);
int temp_y=(int)(y*50);
g.drawLine(temp_x+500, temp_y+300, temp_x+500, temp_y+300);
xo=x;
yo=y;
}
}
}
public void mouseDragged(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
if (shape.equals("曲线")) {
// g.setStroke(new BasicStroke(10));
// g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}else if(shape.equals("橡皮擦")){
g.setStroke(new BasicStroke(80));
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}else if(shape.equals("喷枪")){
// g.setStroke(new BasicStroke(2)); //不用加粗
// g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for(int k=0;k<20;k++){
Random i=new Random();
int a=i.nextInt(8);
int b=i.nextInt(10);
g.drawLine(x2+a, y2+b, x2+a, y2+b);
}
}
}
}
遇到问题,或者觉得好欢迎留言~