250 likes | 376 Views
Verteilte Software - Java - Grafik - Ereignisse 1. java.lang.Object. java.awt.Applet. java.awt.Window. java.awt.Frame. java.awt.Dialog. javax.swing.JFrame. javax.swing.JWindow. javax.swing.JApplet. javax.swing.JDialog. Verteilte Software - Java - Grafik - Ereignisse 2.
E N D
Verteilte Software - Java - Grafik - Ereignisse 1 java.lang.Object java.awt.Applet java.awt.Window java.awt.Frame java.awt.Dialog javax.swing.JFrame javax.swing.JWindow javax.swing.JApplet 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 1 <<uses>> java.awt.Frame <<interface>> WindowListener <<interface>> Runnable Head windowActivated() windowClosed() windowClosing() windowDeactivated() windowDeiconified() windowIconified() windowOpened() run() 1 1 Smile 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() 1 1 <<uses>> java.awt.Frame <<interface>> Runnable SmileHead 1 1 SHWindowListener windowClosing() <<interface>> WindowListener WindowAdapter 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.*; 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(); } 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); } } } 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); } } ); thread = threadFinished = new Thread(this); thread.start(); } public void paint( Graphics g ) { g.drawImage( bilder[select], 50, 70, null ); } public void run() { while (thread == threadFinished) { select++; select %= anzahl; try {thread.sleep(2000);} catch (InterruptedException e) { System.out.println(e); } repaint(); } } } Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
java.awt.Container add(Component c) <<interface>> MouseListener mousePressed() mouseReleased() mouseClicked() mouseEntered() mouseExited() Verteilte Software - Java - Grafik - Ereignisse 15 java.lang.Object <<uses>> java.awt.Component addMouseListener() 1 1 java.awt.Window java.awt.Canvas java.awt.Frame 1 1 Display 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
java.awt.Container add(Component c) Verteilte Software - Java - Grafik - Ereignisse 19 java.lang.Object <<implements>> <<uses>> <<interface>> ActionListener java.awt.Component java.awt.Button actionPerformed() addActionListener() 1 5 5 <<uses>> <<interface>> AdjustmentListener java.awt.Scrollbar java.awt.Window AdjustmentValueChanged() addAdjustmentListener() 1 1 java.awt.Frame <<uses>> <<interface>> ItemListener 1 java.awt.Checkbox Oszilloscop itemStateChanged() addItemListener() 1 1 1 1 <<uses>> <<interface>> TextListener Display java.awt.TextField textValueChanged() addActionListener() 1 1 java.awt.Canvas 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; tf.setText("0"); sb.setValue(phi); cb.setState(false); } disp_new(); } } public void itemStateChanged(ItemEvent e) { Object obj = e.getSource(); if (obj == cb) { 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 Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik
Verteilte Software - Java - Grafik - Ereignisse 25 Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik