package com.yzd1209.DrawRepaint;
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() {
DrawFrame jf = new DrawFrame();
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.dlistener=dlistener;
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.yzd1209.DrawRepaint;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
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;
int count=0;
Color color;;
Shape[] data=new Shape[100000];
@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);
NewShape(xDown,yDown,xUp,yUp,"直线",color);
}
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));
}
NewShape(xDown,yDown,xUp,yUp,"矩形",color);
}else if(Bname.equals("立体图")) {
int i;
for(i=0;i<100;i++){
g.setColor(new Color(i*2, 100+i, i*2+50));
g.fillOval(xDown+i/3, yDown+i/3, 100-i, 100-i);
}
NewShape(xDown, yDown, xDown,yDown,"立体图",color);
}else if(Bname.equals("分形图")) {
double a=-1.4,b=1.6,c=1.0,d=0.7;
double x=0.0,y=0.0;
double temx=0.0,temy=0.0;
int drawx=0,drawy=0;
for(int i=0;i<1000;i++) {
temx = Math.sin(a*y)+c*Math.cos(a*x);
temy = Math.sin(b*x)+d*Math.cos(b*y);
x=temx;
y=temy;
drawx = (int)(temx*50+xDown);
drawy = (int)(temy*50+yDown);
g.drawLine(drawx, drawy, drawx, drawy);
}
NewShape(xDown, yDown, xUp, yUp,"分形图",color);
}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<1000;i++) {
int k=random.nextInt(3);
if(k==0) {
g.drawLine((x1+xp)/2,(y1+yp)/2,(x1+xp)/2,(y1+yp)/2);
NewShape((x1+xp)/2,(y1+yp)/2,(x1+xp)/2,(y1+yp)/2,"三点分形",color);
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);
NewShape((x2+xp)/2,(y2+yp)/2,(x2+xp)/2,(y2+yp)/2,"三点分形",color);
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);
NewShape((x3+xp)/2,(y3+yp)/2,(x3+xp)/2,(y3+yp)/2,"三点分形",color);
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;
int drawx=0,drawy=0;
Random random = new Random();
for(int i=0;i<1000;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;
drawx = (int)(temx*500+xDown);
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;
drawx = (int)(temx*500+xDown);
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;
drawx = (int)(temx*500+xDown);
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;
drawx = (int)(temx*500+xDown);
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;
drawx = (int)(temx*500+xDown);
drawy = (int)(temy*500+yDown);
g.drawLine(drawx, -drawy+800, drawx, -drawy+800);
System.out.println(drawx+" "+drawy);
x=temx;
y=temy;
}
}
NewShape(xDown,yDown,xUp,yUp,"概率画图",color);
}
else if(Bname.equals("递归直线")) {
RecurShape shapeRecur = new RecurShape(xDown,yDown,xUp+600,yUp,Bname,color,6);
shapeRecur.show(g);
data[count]=shapeRecur;
count++;
}
else if(Bname.equals("九宫格")) {
System.out.println(color);
System.out.println();
SudokuShape shapeSudoku = new SudokuShape(xDown,yDown,300,300,Bname,color);
shapeSudoku.show(g);
data[count]=shapeSudoku;
count++;
}
else if(Bname.equals("立体九宫格")) {
DimenSudokuShape shapeSudokuDimen = new DimenSudokuShape(xDown,yDown,300,300,100,100,Bname,color);
shapeSudokuDimen.show(g);
data[count]=shapeSudokuDimen;
count++;
}
System.out.println("鼠标释放");
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
if(Bname.equals("曲线")) {
x=e.getX();
y=e.getY();
g.drawLine(xDown,yDown,x,y);
NewShape(xDown, yDown, x, y,"曲线",color);
xDown=x;
yDown=y;
}
}
public void mouseMoved(MouseEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("")) {
JButton jbu = (JButton)e.getSource();
color = jbu.getBackground();
g.setColor(color);
}else {
Bname = e.getActionCommand();
}
}
public void NewShape(int x1,int y1,int x2,int y2,String type,Color color) {
Shape s = new Shape();
s.x1=x1;
s.y1=y1;
s.x2=x2;
s.y2=y2;
s.type=Bname;
s.color=g.getColor();
data[count]=s;
count++;
}
}
package com.yzd1209.DrawRepaint;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.Random;
public class Shape{
int x1,y1,x2,y2;
String type;
Color color;
Graphics g;
public Shape() {
}
public Shape(int x1,int y1,int x2,int y2,String type,Color color) {
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.type=type;
this.color=color;
}
public void show(Graphics g) {
g.setColor(color);
if("直线".equals(type)) {
g.drawLine(x1,y1,x2,y2);
}else if("矩形".equals(type)) {
if(x1<x2) {
g.drawRect(x1,y1, x2-x1,y2-y1);
}else {
g.drawRect(x1-(x1-x2),y1-(y1-y2), Math.abs(x2-x1), Math.abs(y2-y1));
}
}else if("立体图".equals(type)) {
for(int i=0;i<100;i++) {
g.setColor(new Color(i*2, 100+i, i*2+50));
g.fillOval(x1+i/3, y1+i/3, 100-i, 100-i);
}
}else if("分形图".equals(type)) {
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<1000;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+x1);
int drawy = (int)(temy*50+y1);
g.drawLine(drawx, drawy, drawx, drawy);
}
}else if("概率画图".equals(type)) {
double a,b,c,d,e1,f;
double x=0.0,y=0.0;
Random random = new Random();
for(int i=0;i<1000;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+x1);
int drawy = (int)(temy*500+y1);
g.drawLine(drawx, -drawy+800, drawx, -drawy+800);
x=temx;
y=temy;
}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+x1);
int drawy = (int)(temy*500+y1);
g.drawLine(drawx, -drawy+800, drawx, -drawy+800);
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+x1);
int drawy = (int)(temy*500+y1);
g.drawLine(drawx, -drawy+800, drawx, -drawy+800);
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+x1);
int drawy = (int)(temy*500+y1);
g.drawLine(drawx, -drawy+800, drawx, -drawy+800);
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+x1);
int drawy = (int)(temy*500+y1);
g.drawLine(drawx, -drawy+800, drawx, -drawy+800);
x=temx;
y=temy;
}
}
}else if("曲线".equals(type)) {
g.drawLine(x1, y1, x2, y2);
}else if("三点分形".equals(type)) {
g.drawLine(x1, y1, x2, y2);
}
}
}
package com.yzd1209.DrawRepaint;
import java.awt.Graphics;
import javax.swing.JFrame;
public class DrawFrame extends JFrame{
Shape [] data;
DrawListener dlistener=null;
public void paint(Graphics g) {
super.paint(g);
data=dlistener.data;
for(int i=0;i<data.length;i++) {
if(data[i]!=null) {
data[i].show(g);
}
else {
break;
}
}
System.out.println("paint");
}
}
package com.yzd1209.DrawRepaint;
import java.awt.Color;
import java.awt.Graphics;
public class RecurShape extends Shape{
int n;
public RecurShape(int x1,int y1,int x2,int y2,String type,Color color,int n) {
super(x1,y1,x2,y2,type,color);
this.n=n;
}
public void show(Graphics g) {
if (type.equals("递归直线")) {
g.setColor(color);
MyDrawLine(x1,y1,x2,y2,g,6);
}
}
public void MyDrawLine(int x1,int y1,int x2,int y2,Graphics g,int n) {
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(xL1,yL1,xL2,yL2,g,n-1);
int xR1=x2-(x2-x1)/3;int yR1=y2+40;int xR2=x2;int yR2=y2+40;
MyDrawLine(xR1,yR1,xR2,yR2,g,n-1);
}
}
package com.yzd1209.DrawRepaint;
import java.awt.Color;
import java.awt.Graphics;
public class SudokuShape extends Shape{
int x,y,w,h,dx,dy;
String type;
Color color;
public SudokuShape (int x,int y,int w,int h,String type,Color color) {
this.x=x;
this.y=y;
this.w=w;
this.h=h;
this.type=type;
this.color=color;
}
public void show(Graphics g) {
if(type.equals("九宫格")) {
g.setColor(color);
MyDrawRec(x,y,300,300,g);
}
}
public void MyDrawRec(int x,int y,int w,int h,Graphics g) {
if(w<1) {
return;
}
g.fillRect(x+w/3, y+h/3, w/3, h/3);
MyDrawRec(x,y,w/3,h/3,g);
MyDrawRec(x+w/3,y,w/3,h/3,g);
MyDrawRec(x+2*w/3,y,w/3,h/3,g);
MyDrawRec(x,y+h/3,w/3,h/3,g);
MyDrawRec(x+2*w/3,y+h/3,w/3,h/3,g);
MyDrawRec(x,y+2*h/3,w/3,h/3,g);
MyDrawRec(x+w/3,y+2*h/3,w/3,h/3,g);
MyDrawRec(x+2*w/3,y+2*h/3,w/3,h/3,g);
}
}
package com.yzd1209.DrawRepaint;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
public class DimenSudokuShape extends Shape{
int x,y,w,h,dx,dy;
String type;
Color color;
public DimenSudokuShape(int x,int y,int w,int h,int dx,int dy,String type,Color color) {
this.x=x;
this.y=y;
this.w=w;
this.h=h;
this.dx=dx;
this.dy=dy;
this.type=type;
this.color=color;
}
public void show(Graphics g) {
if(type.equals("立体九宫格")) {
MyDrawDimenRec(x,y,300,300,100,100,g);
}
}
public void MyDrawDimenRec(int x,int y,int w,int h,int dx,int dy,Graphics g) {
if(w<10) {
return;
}
g.drawLine(x, y, x+w, y);
g.drawLine(x, y, x, y+h);
g.drawLine(x, y+h, x+w, y+h);
g.drawLine(x+w, y, x+w, y+h);
g.drawLine(x+w, y+h, x+dx+w, y+h-dy);
g.drawLine(x+dx+w, y-dy+h, x+dx+w, y-dy);
g.drawLine(x+w, y, x+dx+w, y-dy);
g.drawLine(x+dx+w, y-dy, x+dx, y-dy);
g.drawLine(x, y, x+dx, y-dy);
Polygon pon1=new Polygon();
pon1.addPoint(x,y);
pon1.addPoint(x+w,y);
pon1.addPoint(x+w,y+h);
pon1.addPoint(x,y+h);
g.setColor(new Color(0,150,0));
g.fillPolygon(pon1);
Polygon pon2=new Polygon();
pon2.addPoint(x,y);
pon2.addPoint(x+w,y);
pon2.addPoint(x+dx+w,y-dy);
pon2.addPoint(x+dx,y-dy);
g.setColor(new Color(150,0,0));
g.fillPolygon(pon2);
Polygon pon3=new Polygon();
pon3.addPoint(x+w,y);
pon3.addPoint(x+w,y+h);
pon3.addPoint(x+dx+w,y-dy+h);
pon3.addPoint(x+dx+w,y-dy);
g.setColor(new Color(0,0,150));
g.fillPolygon(pon3);
MyDrawDimenRec(x+2*dx/3,y+2*h/3-2*dy/3,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x+w/3+2*dx/3,y+2*h/3-2*dy/3,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x+2*w/3+2*dx/3,y+2*h/3-2*dy/3,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x+dx/3,y+2*h/3-dy/3,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x+2*w/3+dx/3,y+2*h/3-dy/3,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x,y+2*h/3,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x+w/3,y+2*h/3,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x+2*w/3,y+2*h/3,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x+2*dx/3,y+h/3-2*dy/3,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x+2*w/3+2*dx/3,y+h/3-2*dy/3,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x,y+h/3,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x+2*w/3,y+h/3,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x+2*dx/3,y-2*dy/3,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x+w/3+2*dx/3,y-2*dy/3,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x+2*w/3+2*dx/3,y-2*dy/3,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x+dx/3,y-dy/3,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x+2*w/3+dx/3,y-dy/3,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x,y,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x+w/3,y,w/3,h/3,dx/3,dy/3,g);
MyDrawDimenRec(x+2*w/3,y,w/3,h/3,dx/3,dy/3,g);
}
}