两个图形
代码:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class DrawListener implements MouseListener{
private Graphics g;
public DrawListener(Graphics g){
this.g=g;
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
draw3();
}
public void draw2() {
double x1,x2;
double y1,y2;
y1=0;
x1=0;
double a=-2;
double b=-2;
double c=-1.2;
double d=2;
g.setColor(Color.magenta);
for(int i=0;i<100000;i++){
x2=x1;y2=y1;
// if(i%10==0)
// g.setColor(new Color((i*10+10)%256,(i*10+80)%256,(i*10)%256));
g.setColor(new Color((i*100+10)%256,(i*100+50)%256,(i*100+100)%256));
g.fillOval((int)((x1)*300+500),(int)((y1)*300+350),1,1);
x1=Math.sin(a*y2)-Math.cos(b*x2);
y1=Math.sin(c*x2)-Math.cos(d*y2);
}
}
public void draw3(){
double x1,x2,y1,y2;
y1=0;
x1=0;
double a=1.40;
double b=1.56;
double c=1.40;
double d=-6.56;
// g.setColor(Color.gray);
for(int i=0;i<100000;i++){
x2=x1;y2=y1;
if(i%10==0){
// g.setColor(new Color((i)%256,(i+200)%256,(i+100)%256));}
// g.setColor(new Color((i+50)%200,(i+200)%128,(i+100)%256));}//紫色
// g.setColor(Color.now);
g.fillOval((int)(x1*50+500),(int)(y1*50+350),1,1);
g.drawLine((int)(x1*60+500),(int)(y1*60+350),(int)(x1*60+500),(int)(y1*60+350));}
x1=d*Math.sin(a*x2)-Math.sin(b*y2);
y1=c*Math.cos(a*x2)+Math.cos(b*y2);
}
}
public void draw(double e,double f){
int a=-2;
int b=-2;
double c=-1.2;
int d=2;
g.drawOval((int)e,(int)f,1,1);
if (e<100&&f<100)
draw(Math.sin(a*f)-Math.cos(b*e),Math.sin(c*e)-Math.cos(d*f));}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
//draw(100,100);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
}
监听器draw1:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
public class draw1 extends JFrame{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
draw1 df=new draw1();
df.initGUI();
// draw(0,0);
}
private void initGUI() {
// TODO Auto-generated method stub
//本身继承了一个类,已经是一个窗体
this.setSize(new Dimension(1000,700));
this.setTitle("简单画板");
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
this.setVisible(true);
Graphics g=this.getGraphics();
MouseListener dl=new DrawListener(g);
this.addMouseListener(dl);
}
}