package ComplPlane; import java.applet.*; import java.awt.*; import java.awt.event.*; /** *

Überschrift: Übung Java

*

Beschreibung: Applet-Beispiel, Darstellung der komplexen * Zahlenebene, MouseClick- und MouseMotion-Events werden über innere * und anonyme Adapterklassen behandelt. * Zusätzlich Operatoren und Darstellung des komplexen Winkels. *

*

Copyright: (c) 2003

*

Organisation:

* @author Wolfgang Konen * @version 1.0 */ public class GUIComplexPlane3 extends Applet { static final int I_KONJ=1, I_HOCH2=2, I_HOCH3=3; int istate; double re; double im; TextField tfRe; TextField tfIm; Choice cbox; MyWindow win; public GUIComplexPlane3() { this.re=0.0; this.im=0.0; } public void init() { // enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK); setBackground(Color.white); // Einlesen der Initialparameter aus HTML-Datei String strRe = getParameter("Realteil"); String strIm = getParameter("Imagteil"); if (strRe==null) strRe = "0.0"; if (strIm==null) strIm = "0.0"; this.re = Double.valueOf(strRe).doubleValue(); this.im = Double.valueOf(strIm).doubleValue(); this.setLayout(null); tfRe = new TextField(strRe); tfIm = new TextField(strIm); this.add(tfRe); tfRe.setBounds(107,252,40,20); this.add(tfIm); tfIm.setBounds(107,272,40,20); Label lRe = new Label("Real:"); lRe.setBounds( 63,252,35,20); lRe.setBackground(new Color(250,250,230)); this.add(lRe); Label lIm = new Label("Imag:"); lIm.setBounds( 63,272,35,20); lIm.setBackground(new Color(250,250,230)); this.add(lIm); Label lChoice = new Label("Operator:"); lChoice.setBounds(50,20,60,20); this.add(lChoice); cbox = new Choice(); cbox.setBounds(120,20,90,20); cbox.addItem("konjugieren"); cbox.addItem("Quadrat"); cbox.addItem("hoch 3"); cbox.addItemListener(new MyItemListener()); this.add(cbox); cbox.select("konjugieren"); istate = I_KONJ; this.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent evt) { int X = evt.getX(); int Y = evt.getY(); re = win.sx(X); im = win.sy(Y); repaint(); } }); this.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { int X = evt.getX(); int Y = evt.getY(); re = win.sx(X); im = win.sy(Y); repaint(); } }); } class MyItemListener implements ItemListener { public void itemStateChanged(ItemEvent ie) { if (ie.getItem().equals("konjugieren")) istate=I_KONJ; if (ie.getItem().equals("Quadrat")) istate=I_HOCH2; if (ie.getItem().equals("hoch 3")) istate=I_HOCH3; repaint(); } } public void paint(Graphics g) { int clip = 1; int width = getSize().width; int height = getSize().height; g.setColor(Color.black); g.drawRect(clip,clip, width-2*clip, height-2*clip ); g.setClip(clip,clip, width - 2*clip, height - 2* clip); double xmin=-3.0, xmax=3.0; double ymin=-3.0, ymax=3.0; this.setBackground(new Color(250,250,230)); // helles gelb this.win = new MyWindow(g,xmin,ymin,xmax,ymax); // Koordinatengitter malen for (double i=xmin; i<(xmax+1);i+=0.25) { g.setColor(new Color(200,200,250)); if (i%1.0==0.0) g.setColor(new Color(40,170,40)); if (i==0.0) g.setColor(Color.blue); win.line(i,ymin,i,ymax); } for (double i=xmin; i<(xmax+1);i+=0.25) { g.setColor(new Color(200,200,250)); if (i%1.0==0.0) g.setColor(new Color(40,170,40)); if (i==0.0) g.setColor(Color.blue); win.line(xmin,i,xmax,i); } // komplexe Zahl zeigen g.setColor(Color.red); win.line(0.0,0.0,re,im); win.point(re,im); tfRe.setText(new Double(re).toString()); tfIm.setText(new Double(im).toString()); // Winkel zeichnen double angle = Math.atan2(im,re)/Math.PI*180.0; if (angle<0.0) angle = 360.0+angle+1.0; win.arc(0,0,0.5,angle); // Operation berechnen double re2=0.0,im2=0.0; switch (istate) { case I_KONJ: re2=re; im2=-im; break; case I_HOCH2: re2 = (re*re)-(im*im); im2 = 2*re*im; break; case I_HOCH3: double re22,im22; re22 = (re*re)-(im*im); im22 = 2*re*im; re2 = re22*re - im22*im; im2 = re22*im + im22*re; break; } // Operand malen g.setColor(Color.black); win.line(0.0,0.0,re2,im2); win.point(re2,im2); // Winkel zeichnen double angle2 = Math.atan2(im2,re2)/Math.PI*180.0; if (angle2<0.0) angle2 = 360.0+angle2+1.0; win.arc(0,0,0.3,angle2); // diese Befehle sind streng verboten in paint(): // this.setVisible(true); // this.setSize(600,400); } /* --- obsolet --- // einfache Methode, um auf Events zu reagieren public void processMouseEvent(MouseEvent evt) { if(evt.getID() == MouseEvent.MOUSE_PRESSED) { int X = evt.getX(); int Y = evt.getY(); this.re = win.sx(X); this.im = win.sy(Y); repaint(); } super.processMouseEvent(evt); } public void processMouseMotionEvent(MouseEvent evt) { if(evt.getID() == MouseEvent.MOUSE_DRAGGED) { int X = evt.getX(); int Y = evt.getY(); this.re = win.sx(X); this.im = win.sy(Y); repaint(); } super.processMouseMotionEvent(evt); }} --- obsolet --- */ }