Verteilte Software - Java - Grafik - Ereignisse 1 java.lang.Object java.awt.Applet java.awt.Frame javax.swing.JFrame javax.swing.JApplet java.awt.Window java.awt.Dialog javax.swing.JWindow javax.swing.JDialog Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 2 java.awt.Window addWindowListener() 1 <<uses>> java.awt.Frame <<interface>> Runnable run() 1 Head 1 1 Smile <<interface>> WindowListener windowActivated() windowClosed() windowClosing() windowDeactivated() windowDeiconified() windowIconified() windowOpened() Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 3 import java.awt.*; import java.awt.event.*; public class Smile { public static void main( String argv[] ) { Frame f = new Head(); f.setTitle( "Smile" ); f.setSize( 400, 400 ); f.setVisible(true); f.addWindowListener((WindowListener)f); } } class Head extends Frame implements Runnable, WindowListener { private Thread blink, blinkFinished; boolean auf; Head() { auf = true; blink = blinkFinished = new Thread(this); blink.start(); } public void paint( Graphics g ) { auf = !auf; g.setColor( Color.blue ); g.drawOval( 50, 50, 300, 300 ); g.drawOval( 100, 120, 80, 80 ); if (auf) g.drawOval( 220, 120, 80, 80 ); else g.drawLine( 220, 160, 300, 160 ); g.drawArc ( 100, 200, 200, 100, -20, -140 ); g.drawLine( 200, 200, 200, 230); } Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 4 public void run() { while (blink == blinkFinished) { try {blink.sleep((int)(1000.0 * Math.random()));} catch (InterruptedException e) { System.out.println(e); } repaint(); } } public void windowOpened(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowIconified(WindowEvent e) { blink = null; } public void windowDeiconified(WindowEvent e) { blink = blinkFinished = new Thread(this); blink.start(); } public void windowClosing(WindowEvent e) { blink = null; try {blinkFinished.join();} catch (InterruptedException ie) {System.out.println(ie);} setVisible(false); dispose(); System.exit(0); } public void windowClosed(WindowEvent e) {} } Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 5 java.awt.Window addWindowListener() java.awt.Frame <<interface>> Runnable 1 <<uses>> 1 SmileHead 1 1 SHWindowListener windowClosing() WindowAdapter <<interface>> WindowListener Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 6 import java.awt.*; import java.awt.event.*; public void paint( Graphics g ) { auf = !auf; g.setColor( Color.blue ); g.drawOval( 50, 50, 300, 300 ); g.drawOval( 100, 120, 80, 80 ); if (auf) g.drawOval( 220, 120, 80, 80 ); else g.drawLine( 220, 160, 300, 160 ); g.drawArc ( 100, 200, 200, 100, -20, -140); g.drawLine( 200, 200, 200, 230); } public void run() { while (blink == blinkFinished) { try {blink.sleep((int)(1000.0* Math.random()));} catch (InterruptedException e) System.out.println(e);} repaint(); } } class SHWindowListener extends WindowAdapter { public void windowClosing(WindowEvent e) { blink = null; try {blinkFinished.join();} catch (InterruptedException ie) {System.out.println(ie);} setVisible(false); dispose(); System.exit(0); } } class SmileHead extends Frame implements Runnable { private Thread blink, blinkFinished; boolean auf; public static void main(String argv[]) { new SmileHead(); } SmileHead() { setTitle( "Smile" ); setSize( 400, 400 ); setVisible(true); addWindowListener( new SHWindowListener() ); auf = true; blink = blinkFinished = new Thread(this); blink.start(); } } Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 7 import java.awt.*; import java.awt.event.*; class Text extends Frame { Font foSs, foMs, foSe; FontMetrics fmSs, fmMs, fmSe; String sHead = "TU Bergakademie Freiberg", sFak = "Fakultät ", sInf = "Ein Studium in Freiberg lohnt sich"; int i, xa, ya; public static void main( String argv[] ) { new Text(); } Text() { setTitle( "TU Bergakademie Freiberg" ); setSize( 600, 450 ); setVisible(true); addWindowListener (new WindowAdapter() {public void windowClosing(WindowEvent e) { setVisible(false); dispose(); System.exit(0); } } ); } Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 8 public void paint( Graphics g ) { foSs = new Font( "SansSerif", Font.PLAIN, 36 ); // Helvetica foMs = new Font( "Monospaced", Font.BOLD, 24 ); // Courier foSe = new Font( "Serif", Font.ITALIC, 18 ); // TimesRoman fmSs = g.getFontMetrics( foSs ); fmMs = g.getFontMetrics( foMs ); fmSe = g.getFontMetrics( foSe ); g.setColor( Color.blue ); xa = (getBounds().width - fmSs.stringWidth(sHead)) / 2; g.setFont(foSs); g.drawString(sHead, xa, 2 * fmSs.getHeight()); g.setFont(foMs); g.setColor( Color.black ); ya = 4 * fmSs.getHeight(); xa = 50; for (i = 1; i < 7; i++) { g.drawString(sFak + i, xa, ya); ya += fmMs.getHeight(); } g.setColor( Color.darkGray); g.setFont(foSe); xa = (getBounds().width - fmSe.stringWidth(sInf) - 50); ya += (3 * fmSe.getHeight()); g.drawString(sInf, xa, ya); } } Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 9 import java.awt.*; import java.awt.event.*; class News extends Frame implements Runnable { Font foSs, foMs, foSe; FontMetrics fmSs, fmMs, fmSe; String sHead = "TU Bergakademie Freiberg", sFak = "Fakultät ", s0 = " +++ NEWS +++ ", s1 = "Die Fakultät 1 plant für das nächste Studienjahr, ...", s2 = "Ein Besuch der TU Bergakademie Freiberg lohnt sich immer."; Image bI; Graphics bG; Thread moveThread, moveThreadFinished; int i, xa, ya, breit, hoch, pos, rand; public static void main( String argv[] ) {new News();} Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 10 News() { pos = 100; rand = 50; bI = null; foSs = new Font( "SansSerif", Font.PLAIN, 30 ); // Helvetica foMs = new Font( "Monospaced", Font.BOLD, 20 ); // Courier foSe = new Font( "Serif", Font.ITALIC, 18 ); // TimesRoman fmSs = getToolkit().getFontMetrics( foSs ); fmMs = getToolkit().getFontMetrics( foMs ); fmSe = getToolkit().getFontMetrics( foSe ); breit = fmSe.stringWidth( s0+s1+s0+s2+s0 ); hoch = fmSe.getHeight(); setBackground( Color.lightGray ); setTitle( "TU Bergakademie Freiberg" ); setSize( 500, 400 ); setVisible(true); addWindowListener (new WindowAdapter() {public void windowClosing(WindowEvent e) { moveThread = null; try {moveThreadFinished.join();} catch (InterruptedException ie) {System.out.println(ie);} setVisible(false); dispose(); System.exit(0); } } ); moveThread = moveThreadFinished = new Thread(this); moveThread.start(); } Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 11 void checksize() { if (bI == null || getBounds().width != bI.getWidth(null) || getBounds().height != bI.getHeight(null) ) { bI = createImage (getBounds().width, getBounds().height); bG = bI.getGraphics(); } } public void paint( Graphics g ) { checksize(); if (bG == null) paintD(g); else { paintD(bG); g.drawImage (bI, 0, 0, null); } } public void paintD( Graphics g ) { g.clearRect( 0, 0, getBounds().width, getBounds().height); g.clipRect(rand, rand, getBounds().width - 2 * rand, getBounds().height - 2 * rand); g.setColor( Color.blue ); xa = (getBounds().width – fmSs.stringWidth(sHead)) / 2; g.setFont(foSs); g.drawString(sHead, xa, 2 * fmSs.getHeight()); g.setFont(foMs); g.setColor( Color.black ); ya = 4 * fmSs.getHeight(); xa = 50; for (i = 1; i < 7; i++) { g.drawString(sFak + i, xa, ya); ya += fmMs.getHeight(); } g.setFont(foSe); g.setColor(Color.white); g.fillRect(0, ya + hoch, getBounds().width, 3 * hoch / 2); ya += (2 * hoch ); g.setColor(Color.black); g.drawString(s0+s1+s0+s2+s0, pos, ya); } Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 12 public void update(Graphics g) { paint(g); Toolkit.getDefaultToolkit().sync(); } public void run() { while (moveThread == moveThreadFinished) { if (pos + breit > rand) pos--; else pos = getBounds().width - rand; try {moveThread.sleep(50);} catch (InterruptedException e) { System.out.println(e); } repaint(); } } } Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 13 import java.awt.*; import java.awt.event.*; class SlideShow extends Frame implements Runnable { private Thread thread, threadFinished; Image bilder[]; int i, select, anzahl; public static void main( String argv[] ) { new SlideShow(argv[0], Integer.parseInt(argv[1])); } SlideShow (String b_name, int anzahl) { this.anzahl = anzahl; select = 0; bilder = new Image [anzahl]; Toolkit tk = Toolkit.getDefaultToolkit(); System.out.print(anzahl); System.out.print(" Bilder werden geladen ... "); System.out.flush(); for (i = 0; i < anzahl; i++) bilder[i] = tk.getImage( b_name + i + ".gif" ); MediaTracker mt = new MediaTracker( this ); for (i = 0; i < anzahl; i++) mt.addImage( bilder[i], i ); try { mt.waitForAll(); } catch ( InterruptedException e ) { return; } System.out.println( "fertig" ); Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 14 setBackground( Color.orange ); setTitle( "SlideShow" ); setSize( 450, 350 ); setVisible(true); addWindowListener (new WindowAdapter() { public void windowClosing(WindowEvent e) { thread = null; try {threadFinished.join();} catch (InterruptedException ie) {System.out.println(ie);} setVisible(false); dispose(); System.exit(0); public void run() } { } ); while (thread == threadFinished) thread = threadFinished = new Thread(this); { thread.start(); select++; } select %= anzahl; try {thread.sleep(2000);} public void paint( Graphics g ) catch (InterruptedException e) { { g.drawImage( bilder[select], 50, 70, null ); System.out.println(e); } } repaint(); } } } Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 15 java.lang.Object <<uses>> java.awt.Component java.awt.Container addMouseListener() add(Component c) 1 <<interface>> MouseListener mousePressed() mouseReleased() mouseClicked() mouseEntered() mouseExited() java.awt.Window java.awt.Canvas java.awt.Frame 1 Display 1 1 Disp_F 1 1 MButton <<implements>> java.awt.event.MouseAdapter Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 16 import java.awt.*; import java.awt.event.*; class Display extends Canvas { Polygon pf; int select_f, phi, maxy, mtx, mty, i; Display() { this(1, 0); } Display(int select_f, int phi) { this.select_f = select_f; this.phi = phi; maxy = 100; mtx = 0; setBackground(Color.black); new_function(); addMouseListener(new MButton()); } void new_function() { pf = new Polygon(); switch (select_f) { case 0: break; case 1: case 2: case 3: case 4: pf.addPoint(20, 2 * maxy); for (i = 0; i <= 360; i++) pf.addPoint (i + 20, (int)Math.round (maxy * (2 - fkt(select_f, phi, i)))); pf.addPoint(i + 19, 2 * maxy); break; case 5: case 6: case 7: case 8: for (int i = 0; i <= 360; i++) pf.addPoint ((int)Math.round (maxy * (2 - fkt(1, 0, i))), (int)Math.round (maxy * (2 - fkt(select_f-4, phi, i)))); break; } } void new_function(int s, int p) { select_f = s; phi = p; new_function(); } Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 17 double fkt(int art, int phi, int pos) { double fw = 0.0; switch (art) { case 1 : fw = Math.sin((pos + phi) * 2 * Math.PI / 360 ); break; case 2 : fw = Math.sin((2 * pos + phi) * 2 * Math.PI / 360); break; case 3 : fw = Math.sin ((pos + phi) * 2 * Math.PI / 360) + 1 / 3.0 * Math.sin(3 * (pos + phi) * 2 * Math.PI / 360); break; case 4 : fw = Math.sin((2 * pos + phi) * 2 * Math.PI / 360) + 1 / 3.0 * Math.sin ( 3 * (2 * pos + phi) * 2 * Math.PI / 360); break; } return fw; } public void paint( Graphics g ) { g.setColor( Color.green ); g.drawPolygon( pf ); g.setColor( Color.yellow ); if (mtx > 0) { g.drawLine(mtx, maxy / 2, mtx, 7 * maxy / 2); g.drawLine(mtx - 10, 2 * maxy - mty, mtx + 10, 2 * maxy - mty); g.drawString((new Integer(mty).toString()), mtx+10, maxy / 2); } } public class MButton extends MouseAdapter { public void mousePressed(MouseEvent e) { int x = e.getX() - 20; if ( 0 <= x && x <= 360 && 0 < select_f && select_f < 5) { mtx = x + 20; mty = (int)Math.round (maxy * fkt(select_f, phi, x)); } repaint(); } public void mouseReleased(MouseEvent e) { mtx = 0; repaint(); } } } Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 18 import java.awt.*; import java.awt.event.*; public class Disp_F extends Frame { /* Test der Klasse mit: Disp_F Auswahl Phase int Auswahl 1..8 (1..4 Zeitfunktion 5..8 Lissajous-Figuren) int Phase 0..360 (Phasenwinkel in Grad) */ public static void main( String argv[] ) { new Disp_F(argv); } Disp_F( String argv[] ) { add(new Display(Integer.parseInt(argv[0]), Integer.parseInt(argv[1]))); setTitle("Display Function"); setSize( 420, 400 ); setVisible(true); addWindowListener (new WindowAdapter() {public void windowClosing(WindowEvent e) { setVisible(false); dispose(); System.exit(0); } } ); } } Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 19 java.lang.Object <<implements>> <<interface>> ActionListener java.awt.Component actionPerformed() java.awt.Container add(Component c) <<interface>> AdjustmentListener java.awt.Window AdjustmentValueChanged() java.awt.Frame 1 Oszilloscop 1 1 Display <<interface>> ItemListener itemStateChanged() java.awt.Button 1 addActionListener() 5 <<uses>> java.awt.Scrollbar 1 addAdjustmentListener() 1 <<uses>> java.awt.Checkbox 1 addItemListener() 1 <<interface>> TextListener textValueChanged() java.awt.Canvas <<uses>> <<uses>> java.awt.TextField 1 addActionListener() 1 Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 20 import java.awt.*; import java.awt.event.*; public class Oszilloscop extends Frame implements ActionListener, ItemListener, TextListener, AdjustmentListener { Display disp; Scrollbar sb; Checkbox cb; TextField tf; Button b[] = new Button[5]; int phi, fw, art; public static void main(String argv[]) { new Oszilloscop(); } Oszilloscop () { int i; phi = 0; fw = 1; art = 0; setBackground(Color.lightGray); setLayout(new BorderLayout()); // oben Panel oben = new Panel(); oben.setLayout (new FlowLayout(FlowLayout.CENTER)); b[0] = new Button(" sin( x) "); b[1] = new Button(" sin(2x) "); b[2] = new Button("sin( x) + 1/3 sin(3x)"); b[3] = new Button("sin(2x) + 1/3 sin(6x)"); for ( i = 0; i <= 3; i++) { b[i].addActionListener(this); oben.add(b[i]); } add(oben, BorderLayout.NORTH); //links Panel links = new Panel(); b[4] = new Button("Aus"); b[4].addActionListener(this); links.add(b[4]); add(links, BorderLayout.WEST); //zentral disp = new Display(); add(disp, BorderLayout.CENTER ); //rechts sb = new Scrollbar (Scrollbar.VERTICAL, 0, 20, 0, 380); sb.addAdjustmentListener(this); sb.setUnitIncrement(3); sb.setBlockIncrement(30); add(sb, BorderLayout.EAST); Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 21 //unten Panel unten = new Panel(); cb = new Checkbox("Lissajous"); cb.addItemListener(this); unten.add(cb); unten.add(new Label("Phasenwinkel")); tf = new TextField("0",3); tf.addTextListener(this); unten.add(tf); unten.add(new Label("°")); add(unten, BorderLayout.SOUTH); setBackground(Color.lightGray); setTitle("Oszilloscop"); setSize (600,500); setVisible(true); addWindowListener (new WindowAdapter() {public void windowClosing(WindowEvent e) { setVisible(false); dispose(); System.exit(0); } } ); } Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 22 public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); String arg = e.getActionCommand(); if (obj instanceof Button) { if (arg.equals(" sin( x) ")) fw = 1; if (arg.equals(" sin(2x) ")) fw = 2; if (arg.equals("sin( x) + 1/3 sin(3x)")) fw = 3; if (arg.equals("sin(2x) + 1/3 sin(6x)")) fw = 4; if (arg.equals("Aus")) { fw = 0; art = 0; phi = 0; public void itemStateChanged(ItemEvent e) tf.setText("0"); { sb.setValue(phi); Object obj = e.getSource(); cb.setState(false); if (obj == cb) } { disp_new(); if (cb.getState()) art = 4; } else art = 0; } if (fw != 0) disp_new(); } } Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 23 public void textValueChanged(TextEvent e) { Object obj = e.getSource(); if (obj == tf) { phi = Integer.parseInt(tf.getText()); if (phi < 0) phi = 0; if (phi > 360) phi = 360; sb.setValue(phi); disp_new(); } } public void adjustmentValueChanged(AdjustmentEvent e) { Object obj = e.getSource(); if (obj == sb) { phi = sb.getValue(); disp_new(); tf.setText(new Integer(phi).toString()); } } void disp_new() { disp.new_function(art + fw, phi); disp.repaint(); } } Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 24 Ereignis Listener Methoden Quelle ActionEvent ActionListener actionPerformd Button List MenuItem TextField AdjustmentEvent AdjustmentListener adjustmentValueChanged ScrollBar ComponentEvent ComponentListener componentHidden componentMoved componentResized componentShown Component ContainerEvent ContainerListener componentAdded Container FocusEvent FocusListener focusGained focusLost Component ItemEvent ItemListener itemStateChanged CheckBox Choice List Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik Verteilte Software - Java - Grafik - Ereignisse 25 Ereignis Listener Methoden Quelle KeyEvent KeyListener keyPressed keyReleased keyTyped Component MouseEvent MouseListener mouseClicked mouseEntered mouseExited mousePressed mouseReleased Component MouseEvent MouseMotionListener mouseDragged MouseMoved Component TextEvent TextListener textValueChanged TextComponent WindowEvent WindowListener windowActivated windowClosed windowClosing windowDeactivated windowDeiconified windowIconified windowOpened Window Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik