/* polynomial */ import java.awt.*; import java.applet.*; import java.awt.*; import java.applet.*; import java.awt.event.*; public class poly extends Applet{ polyAnim anim=null; public static Thread animthread; public static double a,b; public static double x0,y0,x1,y1; public static boolean checkFlag; int ix0,ix1,iy0,iy1; Point pressed; Label label9; static Button ssButton; static nCanvas pic; Label label1; static TextField bText; Label label2; static TextField aText; Label label3; static TextField y0Text; Label label4; static TextField x0Text; Label label5; static TextField y1Text; Label label6; static TextField x1Text; static Button resButton; Label label7; static TextField xCurr; Label label8; static TextField yCurr; static Button resabButton; Label label10; static Checkbox chkbox; public void init() { setLayout(null); setSize(605,450); setBackground(new Color(0.8F,0.8F,0.8F)); pic = new nCanvas(); pic.setBounds(159,6,440,440); pic.setBackground(new Color(1.0F,1.0F,1.0F)); pic.addMouseMotionListener(new mmaCanvas()); pic.addMouseListener(new maCanvas()); add(pic); pic.makeBuffer(); label9 = new Label("x1 = b*x + a - y*y"); label9.setBounds(12,13,135,22); add(label9); label10 = new Label("y1 = x"); label10.setBounds(12,33,135,22); add(label10); label2 = new Label("a"); label2.setBounds(9,77,30,23); add(label2); aText = new TextField(); aText.setText("0.95"); aText.setBounds(40,76,110,24); aText.setBackground(new Color(1.0F,1.0F,1.0F)); add(aText); label1 = new Label("b"); label1.setBounds(9,107,30,23); add(label1); bText = new TextField(); bText.setText("0.49"); bText.setBounds(40,106,110,24); bText.setBackground(new Color(1.0F,1.0F,1.0F)); add(bText); label4 = new Label("x0"); label4.setBounds(9,217,30,23); add(label4); x0Text = new TextField(); x0Text.setText("-1.0"); x0Text.setBounds(40,216,110,24); x0Text.setBackground(new Color(1.0F,1.0F,1.0F)); add(x0Text); label3 = new Label("y0"); label3.setBounds(9,247,30,23); add(label3); y0Text = new TextField(); y0Text.setText("-1.0"); y0Text.setBounds(40,246,110,24); y0Text.setBackground(new Color(1.0F,1.0F,1.0F)); add(y0Text); label6 = new Label("x1"); label6.setBounds(9,277,30,23); add(label6); x1Text = new TextField(); x1Text.setText("2.0"); x1Text.setBounds(40,276,110,24); x1Text.setBackground(new Color(1.0F,1.0F,1.0F)); add(x1Text); label5 = new Label("y1"); label5.setBounds(9,307,30,23); add(label5); y1Text = new TextField(); y1Text.setText("2.0"); y1Text.setBounds(40,306,110,24); y1Text.setBackground(new Color(1.0F,1.0F,1.0F)); add(y1Text); ssButton = new Button(); ssButton.setLabel("start"); ssButton.setBounds(37,139,83,28); ssButton.setForeground(new Color(0)); ssButton.setBackground(new Color(1.0F,1.0F,1.0F)); ssButton.addMouseListener(new maButton()); add(ssButton); resabButton = new Button(); resabButton.setLabel("Reset a,b"); resabButton.setBounds(45,178,75,19); resabButton.setForeground(new Color(0)); resabButton.setBackground(new Color(1.0F,1.0F,1.0F)); resabButton.addMouseListener(new maButton()); add(resabButton); resButton = new Button(); resButton.setLabel("Reset x,y"); resButton.setBounds(45,338,75,19); resButton.setForeground(new Color(0)); resButton.setBackground(new Color(1.0F,1.0F,1.0F)); resButton.addMouseListener(new maButton()); add(resButton); label7 = new Label("x"); label7.setBounds(9,377,30,23); add(label7); xCurr = new TextField(); xCurr.setBounds(40,376,110,24); xCurr.setBackground(new Color(1.0F,1.0F,1.0F)); add(xCurr); label8 = new Label("y"); label8.setBounds(9,402,30,24); add(label8); yCurr = new TextField(); yCurr.setBounds(40,402,110,24); yCurr.setBackground(new Color(1.0F,1.0F,1.0F)); add(yCurr); chkbox = new Checkbox("Out of Range"); chkbox.setBounds(9,428,140,22); chkbox.addItemListener(new chkBox()); add(chkbox); getNumbers(); } // MouseAdapter for button class maButton extends MouseAdapter{ public void mouseClicked(MouseEvent e){ Object object = e.getSource(); if(object == ssButton) ssButton_Clicked(); if(object == resButton) resButton_Clicked(); if(object == resabButton) resabButton_Clicked(); } } void ssButton_Clicked() { if(ssButton.getLabel()=="start"){ anim=new polyAnim(); anim.myStart(); } else { anim.myStop(); } } void resButton_Clicked(){ if((anim!=null) && anim.isAlive()) anim.myStop(); x0Text.setText("-1.0"); y0Text.setText("-1.0"); x1Text.setText("2.0"); y1Text.setText("2.0"); getNumbers(); } void resabButton_Clicked(){ if((anim!=null) && anim.isAlive()) anim.myStop(); aText.setText("0.95"); bText.setText("0.49"); getNumbers(); } // MouseAdapter for Canvas class maCanvas extends MouseAdapter{ public void mousePressed(MouseEvent e){ pic_MousePressed(e); } public void mouseReleased(MouseEvent e){ pic_MouseReleased(e); } } void pic_MousePressed(MouseEvent event) { if(anim.isAlive()) anim.myStop(); pressed=event.getPoint(); } void pic_MouseReleased(MouseEvent event) { Dimension ps = pic.getSize(); Point p0=pressed; Point p1=event.getPoint(); int tmp; if(p0.x > p1.x){ tmp=p0.x; p0.x=p1.x; p1.x=tmp;}; if(p0.y < p1.y){ tmp=p0.y; p0.y=p1.y; p1.y=tmp;}; if((p0.x != p1.x) && (p0.y != p1.y)){ //Set pressed point to (x0,y0) x0Text.setText(Double.toString(x0 + ((double)p0.x)/((double)ps.width)*(x1-x0))); y0Text.setText(Double.toString(y1 - ((double)p0.y)/((double)ps.height)*(y1-y0))); //Set released point to (x1,y1) x1Text.setText(Double.toString(x0+((double)p1.x)/((double)ps.width)*(x1-x0))); y1Text.setText(Double.toString(y1-((double)p1.y)/((double)ps.height)*(y1-y0))); } } //MouseMotionAdapter for Canvas class mmaCanvas extends MouseMotionAdapter{ public void mouseMoved(MouseEvent e){ pic_MouseMoved(e); } public void mouseDragged(MouseEvent e){ pic_MouseDragged(e); } } void pic_MouseMoved(MouseEvent event){ Dimension ps = pic.getSize(); Point tmp=event.getPoint(); xCurr.setText(Double.toString(x0 + ((double)tmp.x)/((double)ps.width)*(x1-x0))); yCurr.setText(Double.toString(y1 - ((double)tmp.y)/((double)ps.height)*(y1-y0))); } void pic_MouseDragged(MouseEvent event) { Dimension ps; int tx0,tx1,ty0,ty1,tmp; //temporary variables tx0=pressed.x; ty0=pressed.y; tx1=event.getX(); ty1=event.getY(); if(tx0>tx1){tmp=tx0; tx0=tx1; tx1=tmp; } if(ty0>ty1){tmp=ty0; ty0=ty1; ty1=tmp; } if((tx0!=tx1) && (ty0!=ty1)){ Graphics gtmp; pic.repaint(); gtmp=pic.getGraphics(); gtmp.setColor(new Color(1.0F,0.0F,0.0F)); gtmp.drawRect(tx0,ty0,tx1-tx0,ty1-ty0); } } // MouseAdapter for checkbox class chkBox implements ItemListener{ public void itemStateChanged(ItemEvent e){ if(e.getStateChange()==ItemEvent.SELECTED){ checkFlag=true; }else{ checkFlag=false; } } } ////////////////////////////////////////////////////////// // Get numbers static void getNumbers(){ try{ a=Double.valueOf(aText.getText()).doubleValue(); }catch(NumberFormatException e){ aText.setText("0.95"); a=0.95; }; try{ b=Double.valueOf(bText.getText()).doubleValue(); }catch(NumberFormatException e){ bText.setText("0.49"); b=0.49; }; try{ x0=Double.valueOf(x0Text.getText()).doubleValue(); }catch(NumberFormatException e){ x0Text.setText("-1.0"); x0=-1.0; }; try{ y0=Double.valueOf(y0Text.getText()).doubleValue(); }catch(NumberFormatException e){ y0Text.setText("-1.0"); y0=-1.0; }; try{ x1=Double.valueOf(x1Text.getText()).doubleValue(); }catch(NumberFormatException e){ x1Text.setText("2.0"); x1=2.0; }; try{ y1=Double.valueOf(y1Text.getText()).doubleValue(); }catch(NumberFormatException e){ y1Text.setText("2.0"); y1=2.0; }; } }