import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.Math.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;



public class jiba extends JApplet{
    int V[][];

    class HensuuSlider extends JPanel {
        int a;
        JLabel l;
        JLabel num;
        JSlider sl;
        String name;
        HensuuSlider(String s,int min, int max, int now,Color c){
            name =s;
            l = new JLabel(s);
            num = new JLabel("0");
            l.setForeground(c);
            sl= new JSlider(min,max,now);
            sl.setSnapToTicks(true);
            a=now;
            setLayout(new BorderLayout());
            add("West",l);
            add("Center",sl);
            add("East",num);

            Change();
            sl.addChangeListener(new ChangeListener() {
		public void stateChanged(ChangeEvent evt) {
                    Change();
		}
	    });
        }
        int get() { return(a); }
        double getD() { return((double)a); }

        public void set(int n) {
            sl.setValue(n);
            Change();
        }

        public void Change() {
            a= sl.getValue();
            String str=Integer.toString(a);
            num.setText(str);
            canvas.paintImmediately(0,0,canvas.getWidth(),canvas.getHeight());
            //canvas.repaint();
        }
    }


    int hankei=10;
    int x1=150;
    int x2=300;
    int y1=140;
    int y2=180;
    int x3=120;
    int x4=330;
    int y3=170;
    int y4=130;

    int xmin,xmax,ymin,ymax,xv,yv;

    int nowDrag=0;

    GraphPanel canvas;
    HensuuSlider sbM1,sbM2;
    HensuuSlider sbI1,sbI2;
    HensuuSlider sbE;


    JLabel l1;

    int w,h;
    int pw=-1,ph=-1;
    double delta;
    
    public void init(){
	canvas=new GraphPanel();

	Container cont=getContentPane();
	cont.setLayout(new BorderLayout());

	sbM1=new HensuuSlider("¼§²Ù£±",-5,5,1,Color.BLUE);
	sbM2=new HensuuSlider("¼§²Ù£²",-5,5,-1,Color.MAGENTA);
	sbI1=new HensuuSlider("ÅÅÎ®£±",-5,5,0,Color.GREEN);
	sbI2=new HensuuSlider("ÅÅÎ®£²",-5,5,0,Color.CYAN);
	sbE=new HensuuSlider("³°Éô¼§¾ì",-5,5,0,Color.BLACK);
    
        canvas.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                mPressed(e);
            }
            public void mouseReleased(MouseEvent e) {
                mReleased(e);
            }
        });
        canvas.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                mDragged(e);
            }
        });


	JPanel p=new JPanel();
	p.setLayout( new GridLayout(3,2));
    

        p.add(sbM1);
        p.add(sbI1);
        p.add(sbM2);
        p.add(sbI2);
        p.add(sbE);

	cont.add("South",p);
	cont.add("Center",canvas);

	start();
    }

    class GraphPanel extends JPanel {
	public void paint(Graphics g){
            if ( g != null ) {
                writeCanvas(g);
            }
	}
    }
    
    

    public void mPressed(MouseEvent e) {
        int mx=e.getX();
        int my=e.getY();
        int xx=x1;
        int yy=y1;
        if( mx > xx-hankei && mx < xx+hankei && my > yy-hankei && my < yy+hankei ) {
            nowDrag=1;
        }

        xx=(int)x2;
        yy=(int)y2;
        if( mx > xx-hankei && mx < xx+hankei && my > yy-hankei && my < yy+hankei ) {
            nowDrag=2;
        }

        xx=(int)x3;
        yy=(int)y3;
        if( mx > xx-hankei && mx < xx+hankei && my > yy-hankei && my < yy+hankei ) {
            nowDrag=3;
        }

        xx=(int)x4;
        yy=(int)y4;
        if( mx > xx-hankei && mx < xx+hankei && my > yy-hankei && my < yy+hankei ) {
            nowDrag=4;
        }
    }


    public void mReleased(MouseEvent e) {
        nowDrag = 0;
    }

    public void mDragged(MouseEvent e) {
        if( nowDrag == 0 ) {
            return;
        } else if( nowDrag == 1 ) {
            x1=e.getX();
            y1=e.getY();
            if( x1 < hankei ) {
                x1=hankei;
            }
            if( x1 > w-hankei ) {
                x1= w-hankei;
            }
            if( y1 < hankei ) {
                y1=hankei;
            }
            if( y1 > h-hankei ) {
                y1= h-hankei;
            }
        } else if( nowDrag == 2 ) {
            x2=e.getX();
            y2=e.getY();
            if( x2 < hankei ) {
                x2=hankei;
            }
            if( x2 > w-hankei ) {
                x2= w-hankei;
            }
            if( y2 < hankei ) {
                y2=hankei;
            }
            if( y2 > h-hankei ) {
                y2= h-hankei;
            }
        } else if( nowDrag == 3 ) {
            x3=e.getX();
            y3=e.getY();
            if( x3 < hankei ) {
                x3=hankei;
            }
            if( x3 > w-hankei ) {
                x3= w-hankei;
            }
            if( y3 < hankei ) {
                y3=hankei;
            }
            if( y3 > h-hankei ) {
                y3= h-hankei;
            }
        } else if( nowDrag == 4 ) {
            x4=e.getX();
            y4=e.getY();
            if( x4 < hankei ) {
                x4=hankei;
            }
            if( x4 > w-hankei ) {
                x4= w-hankei;
            }
            if( y4 < hankei ) {
                y4=hankei;
            }
            if( y4 > h-hankei ) {
                y4= h-hankei;
            }
        }
        repaint();
    }




    public void writeCanvas(Graphics gc){
        if( gc == null ) {
            return;
        }

	w=canvas.getWidth();
	h=canvas.getHeight();

        int x,y;

	if( w !=pw || h != ph) {
            pw=w;
            ph=h;
            V= new int [w+2][h+2];
        }

        int i,j;
 
        for(i=0 ; i<= w+1 ; i++ ) {
            for(j=0 ; j<= h+1 ; j++ ) {
                x=i-1;
                y=j-1;
                double v=sbE.get()*i/100.0; // ³°Éô¼§¾ì
                v += sbM1.get()*Math.atan2((double)(x-x1),(double)(y-y1));
                v += sbM2.get()*Math.atan2((double)(x-x2),(double)(y-y2));
                if( x != x3 || y != y3 ) {
                    v += sbI1.get()*Math.log((double)((x-x3)*(x-x3)+(y-y3)*(y-y3)))/5.0;
                }
                if( x != x4 || y != y4 ) { 
                    v += sbI2.get()*Math.log((double)((x-x4)*(x-x4)+(y-y4)*(y-y4)))/5.0;
                }
                v *= 10.0;
                v /= Math.PI;
                V[i][j] = (int)Math.floor(v);
            }
        }

	gc.setColor(Color.WHITE);
	gc.fillRect(0,0,w,h);

        int diff;
        for( x=0 ; x< w ; x++ ) {
            for( y=0 ; y< h ; y++ ) {
                if( (x-x1)*(x-x1)+(y-y1)*(y-y1) > hankei*hankei 
                    &&(x-x2)*(x-x2)+(y-y2)*(y-y2) > hankei*hankei 
                    &&(x-x3)*(x-x3)+(y-y3)*(y-y3) > hankei*hankei 
                    &&(x-x4)*(x-x4)+(y-y4)*(y-y4) > hankei*hankei 
                    ) {

                    diff = V[x+1][y+1] - V[x][y+1];
                    

                    if( x==x1 && y<y1 ) {
                        diff -= sbM1.get()*20;
                    }
                    if( x==x2 && y<y2 ) {
                        diff -= sbM2.get()*20;
                    }

                    if( diff != 0 ) {
                        gc.setColor(Color.RED);
                        gc.drawRect(x,y,1,1);
                    } else {
                        
                        diff = V[x+1][y+1] - V[x+1][y];
                        
                        if( diff != 0 ) {
                            gc.setColor(Color.RED);
                            gc.drawRect(x,y,1,1);
                        }
                    }
                }
            }
        }
        gc.setColor(Color.BLUE);
        gc.fillOval(x1-hankei,y1-hankei,hankei*2,hankei*2);
        if( sbM1.get() != 0 ) {
            gc.setColor(Color.WHITE);
            gc.drawLine(x1-hankei,y1,x1+hankei,y1);
            if( sbM1.get()>0 ) {
                gc.drawLine(x1,y1-hankei,x1,y1+hankei);
            }
        }
        gc.setColor(Color.MAGENTA);
        gc.fillOval(x2-hankei,y2-hankei,hankei*2,hankei*2);
        if( sbM2.get() != 0 ) {
            gc.setColor(Color.WHITE);
            gc.drawLine(x2-hankei,y2,x2+hankei,y2);
            if( sbM2.get()>0 ) {
                gc.drawLine(x2,y2-hankei,x2,y2+hankei);
            }
        }
        gc.setColor(Color.GREEN);
        gc.fillOval(x3-hankei,y3-hankei,hankei*2,hankei*2);
        if( sbI1.get() > 0 ) {
            gc.setColor(Color.BLACK);
            gc.drawLine(x3-hankei/2,y3-hankei/2,x3+hankei/2,y3+hankei/2);
            gc.drawLine(x3+hankei/2,y3-hankei/2,x3-hankei/2,y3+hankei/2);
        }
        if( sbI1.get() < 0 ) {
            gc.setColor(Color.BLACK);
            gc.fillOval(x3-hankei/2,y3-hankei/2,hankei,hankei);
        }
        gc.setColor(Color.CYAN);
        gc.fillOval(x4-hankei,y4-hankei,hankei*2,hankei*2);
        if( sbI2.get() > 0 ) {
            gc.setColor(Color.BLACK);
            gc.drawLine(x4-hankei/2,y4-hankei/2,x4+hankei/2,y4+hankei/2);
            gc.drawLine(x4+hankei/2,y4-hankei/2,x4-hankei/2,y4+hankei/2);
        }
        if( sbI2.get() < 0 ) {
            gc.setColor(Color.BLACK);
            gc.fillOval(x4-hankei/2,y4-hankei/2,hankei,hankei);
        }
    }
}

