Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.applet.Applet;
- import java.awt.*;
- import java.awt.event.*;
- public final class Mandelbrot1 extends Applet implements MouseListener,ActionListener {
- Button paint;
- Button repaint;
- TextField textfield;
- int iter=0;
- private int max_iter=100;
- private Color[] colors=new Color[75];
- private double centerX=0.0;
- private double centerY=0.0;
- private double zoom=1.0;
- private int mouseX;
- private int mouseY;
- public void init() {
- setName("Mandelbrot");
- setSize(800,600);
- paint=new Button();
- paint.setLabel("Paint");
- repaint=new Button();
- repaint.setLabel("Repaint");
- textfield=new TextField();
- textfield.setText("");
- add(textfield);
- add(paint);
- add(repaint);
- paint.addActionListener(this);
- repaint.addActionListener(this);
- addMouseListener(this);
- for(int i=0; i<colors.length; i++) {
- int c=2*i*256/colors.length;
- if(c>255)
- c=511-c;
- colors[i] = new Color(c, c, 78);
- }
- }
- public void paint(Graphics g){
- Dimension size=getSize();
- this.paint.setSize(60,20);
- this.paint.setLocation(340, 0);
- this.repaint.setSize(60,20);
- this.repaint.setLocation(370,580);
- this.textfield.setSize(60,20);
- this.textfield.setLocation(400, 0);
- for (int y=0; y<size.height; y++) {
- for (int x=0; x<size.width; x++) {
- double r=zoom / Math.min(size.width, size.height);
- Complex z=new Complex(2.5*(x*r+centerX)-2.0, 1.25-2.5*(y*r+centerY));
- int value=mandelbrot(z);
- g.setColor(colors[value%colors.length]);
- g.drawLine(x, y, x, y);
- }
- }
- }
- public void update(Graphics g) {
- paint(g);
- }
- private int mandelbrot(Complex z0) { //pętla do poprawienia
- Complex z=new Complex(z0);
- for(int t=0; t<iter; t++)
- {
- if(z.abs()>2.0) return t;
- z.mul(z).add(z0);
- }
- return max_iter;
- }
- public void mousePressed(MouseEvent e) {
- mouseX=e.getX();
- mouseY=e.getY();
- }
- public void mouseReleased(MouseEvent e) {
- int x=e.getX();
- int y=e.getY();
- if((e.getModifiers()&InputEvent.BUTTON1_MASK)!= 0) {
- if(x!= mouseX&&y!=mouseY) {
- int w=getSize().width;
- int h=getSize().height;
- centerX+=zoom*Math.min(x, mouseX)/Math.min(w, h);
- centerY+=zoom*Math.min(y, mouseY)/Math.min(w, h);
- zoom*=Math.max((double)Math.abs(x-mouseX)/w,(double)Math.abs(y-mouseY)/h);
- }
- }
- repaint();
- }
- public void mouseClicked(MouseEvent e) {}
- public void mouseEntered(MouseEvent e) {}
- public void mouseExited(MouseEvent e) {}
- @Override
- public void actionPerformed(ActionEvent e) {
- if(e.getSource()==paint){
- int temp=toInt(textfield,textfield.getText());
- if(temp>max_iter){
- System.out.println("Liczba przekracza maksymalną liczbę iteracji.");
- }
- if(temp<0){
- System.out.println("Liczba mniejsza niż minimalna liczba iteracji.");
- }
- else{
- iter=temp;
- repaint();
- }
- }
- else if(e.getSource()==repaint){
- centerX=centerY=0.0;
- zoom=1.0;
- repaint();
- }
- }
- private int toInt(TextField input, String message)
- {
- try
- {
- int num=Integer.parseInt(input.getText());
- System.out.println("Input: "+num+" is a number");
- return num;
- }catch(NumberFormatException e)
- {
- System.out.println("Error: "+message+ " is not a number");
- return 0;
- }
- }
- }
Add Comment
Please, Sign In to add comment