package com.yzd1129.DrawRecursion;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Board {
public void ShowDraw() {
JFrame jf = new JFrame("画板");
jf.setSize(800,600);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flayout = new FlowLayout();
jf.setLayout(flayout);
DrawListener dlistener = new DrawListener();
jf.addMouseListener(dlistener);
jf.addMouseMotionListener(dlistener);
String[] jname = {
"直线","矩形","曲线","分形图","立体图","三点分形","概率画图","递归直线"};
for(int i=0;i<jname.length;i++) {
JButton jline = new JButton(jname[i]);
jf.add(jline);
jline.addActionListener(dlistener);
}
Color[] color = {
Color.RED,Color.GREEN,Color.BLUE};
for(int i=0;i<color.length;i++) {
JButton jcolor = new JButton();
jcolor.setBackground(color[i]);
jcolor.setPreferredSize(new Dimension(30,30));
jf.add(jcolor);
jcolor.addActionListener(dlistener);
}
jf.setVisible(true);
Graphics g = jf.getGraphics();
dlistener.g = g;
}
public static void main(String[] args) {
Board dbroad = new Board();
dbroad.ShowDraw();
}
}
package com.yzd1129.DrawRecursion;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import javax.swing.JButton;
public class DrawListener implements MouseListener,ActionListener,MouseMotionListener{
Graphics g = null;
int xDown,yDown,xUp,yUp,x,y ;
String Bname;
Color red = new Color(255,0,0);
Color black = new Color(0,0,0);
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("鼠标点击");
}
public void mousePressed(MouseEvent e) {
xDown=e.getX();
yDown=e.getY();
System.out.println("鼠标按下");
}
public void mouseReleased(MouseEvent e) {
xUp=e.getX();
yUp=e.getY();
if(Bname.equals("直线")) {
System.out.println(Bname);
g.drawLine(xDown, yDown, xUp, yUp);
}
else if(Bname.equals("矩形")) {
System.out.println(Bname);
if(xDown<xUp) {
g.drawRect(xDown,yDown, xUp-xDown,yUp-yDown);
}else {
g.drawRect(xDown-(xDown-xUp),yDown-(yDown-yUp), Math.abs(xUp-xDown), Math.abs(yUp-yDown));
}
}else if(Bname.equals("立体图")) {
for(int i=0;i<100;i++){
g.setColor(new Color(i*2, 100+i, i*2+50));
g.fillOval(xUp+i/3, yUp+i/3, 100-i, 100-i);
}
}else if(Bname.equals("分形图")) {
double a=-1.4,b=1.6,c=1.0,d=0.7;
double x=0.0,y=0.0;
for(int i=0;i<25500;i++) {
double temx = Math.sin(a*y)+c*Math.cos(a*x);
double temy = Math.sin(b*x)+d*Math.cos(b*y);
x=temx;
y=temy;
int drawx = (int)(temx*50+xDown);
int drawy = (int)(temy*50+yDown);
g.drawLine(drawx, drawy, drawx, drawy);
System.out.println(drawx+" "+drawy);
}
}else if(Bname.equals("三点分形")) {
Random random = new Random();
int x1 = (int)(Math.random()*500);
int y1 = (int)(Math.random()*500);
int x2 = (int)(Math.random()*500);
int y2 = (int)(Math.random()*500);
int x3 = (int)(Math.random()*500);
int y3 = (int)(Math.random()*500);
int xp = (int)(Math.random()*500);
int yp = (int)(Math.random()*500);
for(int i=0;i<10000;i++) {
int k=random.nextInt(3);
if(k==0) {
g.drawLine((x1+xp)/2,(y1+yp)/2,(x1+xp)/2,(y1+yp)/2);
System.out.println((x1+xp)/2+" "+(y1+yp)/2);
xp=(x1+xp)/2;
yp=(y1+yp)/2;
}else if(k==1) {
g.drawLine((x2+xp)/2,(y2+yp)/2,(x2+xp)/2,(y2+yp)/2);
System.out.println((x2+xp)/2+" "+(y2+yp)/2);
xp=(x2+xp)/2;
yp=(y2+yp)/2;
}else if(k==2) {
g.drawLine((x3+xp)/2,(y3+yp)/2,(x3+xp)/2,(y3+yp)/2);
System.out.println((x3+xp)/2+" "+(y3+yp)/2);
xp=(x3+xp)/2;
yp=(y3+yp)/2;
}
}
}
else if (Bname.equals("概率画图")) {
double a,b,c,d,e1,f;
double x=0.0,y=0.0;
Random random = new Random();
for(int i=0;i<10000;i++) {
int k=random.nextInt(5);
if(k==0) {
a=0.1950;b=-0.4880;c=0.3440;d=0.4430;e1=0.4431;f=0.2452;
double temx=a*x+b*y+e1;
double temy=c*x+d*y+f;
int drawx = (int)(temx*500+xDown);
int drawy = (int)(temy*500+yDown);
g.drawLine(drawx, -drawy+800, drawx, -drawy+800);
x=temx;
y=temy;
System.out.println(drawx+" "+drawy);
}else if(k==1) {
a=0.4620;b=0.4140;c=-0.2520;d=0.3610;e1=0.2511;f=0.5692;
double temx=a*x+b*y+e1;
double temy=c*x+d*y+f;
int drawx = (int)(temx*500+xDown);
int drawy = (int)(temy*500+yDown);
g.drawLine(drawx, -drawy+800, drawx, -drawy+800);
System.out.println(drawx+" "+drawy);
x=temx;
y=temy;
}else if(k==2) {
a=-0.6370;b=0.0000;c=0.0000;d=0.5010;e1=0.8562;f=0.2512;
double temx=a*x+b*y+e1;
double temy=c*x+d*y+f;
int drawx = (int)(temx*500+xDown);
int drawy = (int)(temy*500+yDown);
g.drawLine(drawx, -drawy+800, drawx, -drawy+800);
System.out.println(drawx+" "+drawy);
x=temx;
y=temy;
}else if(k==3) {
a=-0.0350;b=0.0700;c=-0.4690;d=0.0220;e1=0.4884;f=0.5069;
double temx=a*x+b*y+e1;
double temy=c*x+d*y+f;
int drawx = (int)(temx*500+xDown);
int drawy = (int)(temy*500+yDown);
g.drawLine(drawx, -drawy+800, drawx, -drawy+800);
System.out.println(drawx+" "+drawy);
x=temx;
y=temy;
}else if(k==4) {
a=-0.0580;b=-0.0700;c=0.4530;d=-0.1110;e1=0.5976;f=0.0969;
double temx=a*x+b*y+e1;
double temy=c*x+d*y+f;
int drawx = (int)(temx*500+xDown);
int drawy = (int)(temy*500+yDown);
g.drawLine(drawx, -drawy+800, drawx, -drawy+800);
System.out.println(drawx+" "+drawy);
x=temx;
y=temy;
}
}
}
else if(Bname.equals("递归直线")) {
MyDrawLine(6,xDown,yDown,xDown+600,yDown);
}
System.out.println("鼠标释放");
}
public void mouseEntered(MouseEvent e) {
System.out.println("鼠标进入");
}
public void mouseExited(MouseEvent e) {
System.out.println("鼠标退出");
}
@Override
public void mouseDragged(MouseEvent e) {
if(Bname.equals("曲线")) {
x=e.getX();
y=e.getY();
g.drawLine(xDown,yDown,x,y);
xDown=x;
yDown=y;
}
}
public void mouseMoved(MouseEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
JButton jbu = (JButton)e.getSource();
Color color = jbu.getBackground();
if(e.getActionCommand().equals("")) {
g.setColor(color);
}else {
Bname = e.getActionCommand();
}
}
public void MyDrawLine(int n,int x1,int y1,int x2,int y2) {
if(n==0) {
return;
}
g.drawLine(x1, y1, x2, y2);
int xL1=x1;int yL1=y1+40;int xL2=x1+(x2-x1)/3;int yL2=y1+40;
MyDrawLine(n-1,xL1,yL1,xL2,yL2);
int xR1=x2-(x2-x1)/3;int yR1=y2+40;int xR2=x2;int yR2=y2+40;
MyDrawLine(n-1,xR1,yR1,xR2,yR2);
}
}