1 / 34

Grafikprogrammierung

Grafikprogrammierung. Programm der nächsten zwei Wochen. 2., 3. und 10. 2. Grafikprogrammierung 9. 2. Exkursion zum WIAS 8.30 Allgemeines über das WIAS 9.00 Vortrag Dr. Jürgen Fuhrmann 10.00 Besichtigung. W ierstraß- I nstitut für Angewandte A nalysis und S tochastik. Mohrenstr. 39

Télécharger la présentation

Grafikprogrammierung

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Grafikprogrammierung DVG1 - Grafikprogrammierung

  2. Programm der nächsten zwei Wochen • 2., 3. und 10. 2. Grafikprogrammierung • 9. 2. Exkursion zum WIAS • 8.30 Allgemeines über das WIAS • 9.00 Vortrag Dr. Jürgen Fuhrmann • 10.00 Besichtigung DVG1 - Grafikprogrammierung

  3. Wierstraß-Institut für Angewandte Analysis und Stochastik Mohrenstr. 39 U2 Hausvogteiplatz U6 Stadtmitte Bus 142 Jerusalemer Str. Bus 147, 257 Oberwallstr. DVG1 - Grafikprogrammierung

  4. Grafikprogrammierung • Als standardisierte portable Grafikschnittstelle existiert in JAVA das Package „AWT“ - „Abstract Window Toolkit“. • AWT enthält Klassen zur Programmierung allgemeiner Grafikelemente, z.B.: • Fenster • Menüs • Druckeranschluß • Drag and Drop • Mausanschluß • Grafikimport, -export • Farben • Fonts • Geometrieobjekte DVG1 - Grafikprogrammierung

  5. Einfachstes Grafikprogramm • Einfachstes Grafikelement „Frame“ import java.awt.*; public class einfachsteGrafik { public static void main (String [] args) { Frame f = new Frame("einfachste Grafik"); f.setSize(600,400); f.show(); } } DVG1 - Grafikprogrammierung

  6. Methode der Klasse Frame • public Frame(String titel) • erzeugt einen neuen Frame mit dem Titel titel • public void setTitle(String titel) • setzt den Titel des Frames • public void setSize(int breite, int hoehe) • setzt die Rahmengröße des Frames • public void show() • zeigt den Frame an • public void setResizable(boolean resizable) • true : Größe kann vom Nutzer verändert werden. • false : Größe kann vom Nutzer nicht verändert werden. • public void paint(Graphics g) • zeichnet den Inhalt des Frames, wird bei jeder Änderung des Frames aufgerufen DVG1 - Grafikprogrammierung

  7. import java.awt.*; public class FunktionsGrafik01 extends Frame { public void paint (Graphics g) { g.drawString("Funktions Grafik",50,75); } public static void main (String [] args) { Frame f = new FunktionsGrafik01(); f.setTitle("Funktions Grafik Version 1"); f.setSize(600,400); f.show(); } } DVG1 - Grafikprogrammierung

  8. Methoden der Klasse Graphics • public void drawString(String str, int ix, int iy) • zeichnet den Text str an die Position (ix,iy) • public void drawLine(int ix1, int iy1, int ix2, int iy2) • zeichnet eine Linie von (ix1,iy1) nach (ix2,iy2) • public void drawRect(int ix, int iy, int ib, int ih) • zeichnet ein Rechteck mit der linken oberen Ecke (ix,iy) und der Breite ib und der Höhe ih • public void fillRect(int ix, int iy, int ib, int ih) • zeichnet ein ausgefülltes Rechteck • public void drawOval(int ix, int iy, int ib, int ih) • zeichnet ein Oval innerhalb des beschriebenen Rechtecks • public void fillOval(int ix, int iy, int ib, int ih) • zeichnet ein ausgefülltes Oval DVG1 - Grafikprogrammierung

  9. public void drawArc(int ix, int iy, int ib, int ih, int start, int arc) • zeichnet einen Auschnitt eines Ovals innerhalb des beschriebenen Rechtecks mit dem Startwinkel start und dem Endwinkel arc • public void fillArc(int ix, int iy, int ib, int ih, int start, int arc) • zeichnet einen ausgefüllten Auschnitt eines Ovals • public void drawPolygon(int[] ix, int[] iy, int n) • zeichnet ein geschlossenes Polygon mit den Ecken (ix[i],iy[i]) für i=0...n-1 • public void fillPolygon(int[] ix, int[] iy, int n) • zeichnet ein geschlossenes ausgefülltes Polygon • public void drawPolyline(int[] ix, int[] iy, int n) • zeichnet einen geschlossene Polygonzug mit den Ecken (ix[i],iy[i]) für i=0...n-1 DVG1 - Grafikprogrammierung

  10. ymax ymin xmin xmax ixmin ixmax iymin iymax Koordinatentransformationen • Zwei Koordinatensysteme: • Anwendungssystem: • reelle (x,y)-Koordinate • variable Grenzen • x wächst von links nach rechts • y wächst von unten nach oben • Bildschirmsystem: • ganzzahlige (x,y)-Koordinate • feste Grenzen, abhängig von Fenstergröße und Auflösung • x wächst von links nach rechts • y wächst von oben nach unten DVG1 - Grafikprogrammierung

  11. Lineare Abbildung (xmin, xmax) ==> (ixmin, ixmax) Ansatz: ix=a+b*x ixmin = a + b*xmin ixmax = a + b*xmax ==> ixmax-ixmin = b*(xmax-xmin) ==> b = (ixmax-ixmin)/(xmax-xmin) ==> a = ixmin - b*xmin = ixmin - xmin*(ixmax-ixmin)/(xmax-xmin) ==> ix = ixmin - xmin*(ixmax-ixmin)/(xmax-xmin) + x*(ixmax-ixmin)/(xmax-xmin) ix = ixmin + (x-xmin)*(ixmax-ixmin)/(xmax-xmin) analog: lineare Abbildung (ymin,ymax) ==> (iymax,iymin) iy = iymax + (y-ymin)*(iymin-iymax)/(ymax-ymin) DVG1 - Grafikprogrammierung

  12. ix = ixmin + (x-xmin)*(ixmax-ixmin)/(xmax-xmin) iy = iymax + (y-ymin)*(iymin-iymax)/(ymax-ymin) x = xmin + (ix-ixmin)*(xmax-xmin)/(ixmax-ixmin) y = ymin + (iy-iymax)*(ymax-ymin)/(iymin-iymax) DVG1 - Grafikprogrammierung

  13. public void paint (Graphics g) { int points = 100000; double xmin = -1.0; double xmax = 10.0; double ymin = -1.0; double ymax = 1.0; double h = (xmax-xmin)/points; int ixmin = 25; int ixmax = 575; int iymin = 50; int iymax = 375; double x1 = xmin; double y1 = function(x1); int ix1 = (int)(ixmin+(x1-xmin)*(ixmax-ixmin)/(xmax-xmin)); int iy1 = (int)(iymax+(y1-ymin)*(iymin-iymax)/(ymax-ymin)); double x2 = x1+h; double y2; int ix2; int iy2; DVG1 - Grafikprogrammierung

  14. while (x2<=xmax) { y2 = function(x2); ix2 = (int)(ixmin+(x2-xmin)*(ixmax-ixmin)/(xmax-xmin)); iy2 = (int)(iymax+(y2-ymin)*(iymin-iymax)/(ymax-ymin)); g.drawLine(ix1,iy1,ix2,iy2); iy1=iy2; ix1=ix2; x2+=h; } } public static void main (String [] args) { Frame f = new FunktionsGrafik02(); f.setTitle("Funktions Grafik Version 2"); f.setSize(600,400); f.show(); } } DVG1 - Grafikprogrammierung

  15. public void drawFunction (Graphics g, double xmin, double xmax, double ymin, double ymax, int points, int ixmin, int ixmax, int iymin, int iymax) { double h = (xmax-xmin)/points; double x1 = xmin; double y1 = function(x1); int ix1 = (int)(ixmin+(x1-xmin)*(ixmax-ixmin)/(xmax-xmin)); int iy1 = (int)(iymax+(y1-ymin)*(iymin-iymax)/(ymax-ymin)); double x2 = x1+h; double y2; int ix2; int iy2; while (x2<=xmax) { y2 = function(x2); ix2 = (int)(ixmin+(x2-xmin)*(ixmax-ixmin)/(xmax-xmin)); iy2 = (int)(iymax+(y2-ymin)*(iymin-iymax)/(ymax-ymin)); g.drawLine(ix1,iy1,ix2,iy2); iy1=iy2; ix1=ix2; x2+=h; } } DVG1 - Grafikprogrammierung

  16. public void paint (Graphics g) { drawFunction(g, -1.0, 10.0, -1.0, +1.0, 100000, 25, 575, 50, 375); } public static void main (String [] args) { Frame f = new FunktionsGrafik03(); f.setTitle("Funktions Grafik Version 3"); f.setSize(600,400); f.show(); } } DVG1 - Grafikprogrammierung

  17. Attribute der Klasse Dimension • width • Breite des Grafikobjektes, z.B. des Frames • height • Höhe des Grafikobjektes , z.B. des Frames • Grafikobjekte besitzen die Methode public Dimension getSize() • Dimension d=getSize() • d.height - Höhe • d.width -Breite DVG1 - Grafikprogrammierung

  18. Attribute der Klasse Insets • left, right • linker bzw. rechter Rand des Frames • bottom, top • unterer bzw. oberer Rand des Frames • Grafikobjekte besitzen die Methode public Insets getInsets() • Insets i=getInsets() • i.left - linker Rand • i.right - rechter Rand • i.bottom - unterer Rand • i.top - oberer Rand DVG1 - Grafikprogrammierung

  19. public void paint (Graphics g) { Dimension d = getSize(); Insets saum = getInsets(); int ix0 = saum.left; int ix1 = d.width - saum.right; int iy0 = saum.top; int iy1 = d.height - saum.bottom; drawFunction(g, -1.0, 10.0, -1.0, +1.0, 10000, ix0, ix1, iy0, iy1); } public static void main (String [] args) { Frame f = new FunktionsGrafik04(); f.setTitle("Funktions Grafik Version 4"); f.setSize(600,400); f.show(); } } DVG1 - Grafikprogrammierung

  20. void drawCoordinateSystem (Graphics g, double xmin, double xmax, double ymin, double ymax, double xstep, double ystep, double x0, double y0, int ixmin, int ixmax, int iymin, int iymax) { int ix = (int)(ixmin+(x0-xmin)*(ixmax-ixmin)/(xmax-xmin)); int iy = (int)(iymax+(y0-ymin)*(iymin-iymax)/(ymax-ymin)); g.drawLine(ixmin, iy, ixmax, iy); g.drawLine(ix, iymin, ix, iymax); double x = x0+xstep; while ( x < xmax ) { int ixx = (int)(ixmin+(x-xmin)*(ixmax-ixmin)/(xmax-xmin)); g.drawLine(ixx, iy-5, ixx, iy+5); g.drawString(InOut.toString(x,9,2),ixx-10,iy+15); x+=xstep; } x = x0-xstep; while ( x > xmin ) { int ixx = (int)(ixmin+(x-xmin)*(ixmax-ixmin)/(xmax-xmin)); g.drawLine(ixx, iy-5, ixx, iy+5); g.drawString(InOut.toString(x,9,2),ixx-10,iy+15); x-=xstep; } DVG1 - Grafikprogrammierung

  21. double y = y0; while ( y < ymax ) { int iyy = (int)(iymax-(y-ymin)*(iymax-iymin)/(ymax-ymin)); g.drawLine(ix-5, iyy, ix+5, iyy); g.drawString(InOut.toString(y,9,2),ix+7,iyy); y+=ystep; } y = y0-ystep; while ( y > ymin ) { int iyy = (int)(iymax-(y-ymin)*(iymax-iymin)/(ymax-ymin)); g.drawLine(ix-5, iyy, ix+5, iyy); g.drawString(InOut.toString(y,9,2),ix+7,iyy); y-=ystep; } } DVG1 - Grafikprogrammierung

  22. public void paint (Graphics g) { Dimension d = getSize(); Insets saum = getInsets(); int ixmin = saum.left; int ixmax = d.width - saum.right; int iymin = saum.top; int iymax = d.height - saum.bottom; drawFunction(g, -2.5, 10.0, -1.0, +1.0, 10000, ixmin, ixmax, iymin, iymax); drawCoordinateSystem(g, -2.5, 10.0, -1.0, +1.0, 1.0, 0.2, 0.0, 0.0, ixmin, ixmax, iymin, iymax); } DVG1 - Grafikprogrammierung

  23. Methoden der Klasse Font • public Font(String name, int style, int size) • name - Name des Fonts z.B. • Dialog, DialogInput, Monospaced, Serif, SansSerif, or Symbol • style - Schriftstil des Fonts z.B. • PLAIN, BOLD, ITALIC, BOLD|ITALIC • size - Größe des Fonts in Punkten • Grafikobjekte besitzen die Methode public Font getFont() • Font oldFont = getFont(); DVG1 - Grafikprogrammierung

  24. Methoden der Klasse FontMetrics • public FontMetrics getFontMetrics(Font font) • liefert geometrische Angaben für Fonts und Texte die in diesen Fonts ausgegeben werden • public int stringWidth(String str) • berechnet die Breite des Textes str bei der Darstellung in dem entsprechenden Font Graphics g; Font f = g.getFont(); FontMetrics fm = getFontMetrics(f); int breite = fm.stringWidth("Das ist ein Text"); DVG1 - Grafikprogrammierung

  25. public void drawTitle(Graphics g, int ixmin, int ixmax, int iymin, int iymax, String title) { Font oldFont = g.getFont(); Font newFont = new Font("SansSerif",Font.BOLD,40); FontMetrics fm = getFontMetrics(newFont); int br = fm.stringWidth(title); g.setFont(newFont); g.drawString( title, (ixmin+ixmax-br)/2, iymin+50); g.setFont(oldFont); } public void paint (Graphics g) { Dimension d = getSize(); Insets saum = getInsets(); int ixmin = saum.left; int ixmax = d.width - saum.right; int iymin = saum.top; int iymax = d.height - saum.bottom; drawFunction(g, -2.5, 10.0, -1.0, +1.0, 10000, ixmin, ixmax, iymin, iymax); drawCoordinateSystem(g, -2.5, 10.0, -1.0, +1.0, 1.0, 0.2, 0.0, 0.0, ixmin, ixmax, iymin, iymax); drawTitle(g, ixmin, ixmax, iymin, iymax, "Sinus-Funktion"); } DVG1 - Grafikprogrammierung

  26. Methoden der Klasse Color • public Color(int r, int g, int b) • r, g, b - rot, grün, blau, 0<=r,g,b<=255 • Color brighter() • c.brighter() ist die gleiche Farbe wie c, nur etwas heller (etwa r=r/0.7, g=g/0.7, b=b/0.7) • Color darker() • c.darker() ist die gleiche Farbe wie c, nur etwas dunkler (etwa r=r*0.7, g=g*0.7, b=b*0.7) • int getRed(), int getGreen(), int getBlue() • c.getRed() berechnet den rot-Anteil der Farbe • c.getGreen() berechnet den grün-Anteil der Farbe • c.getBlue() berechnet den blau-Anteil der Farbe • black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, yellow DVG1 - Grafikprogrammierung

  27. Color oldColor = g.getColor(); g.setColor(Color.red); while (x2<=xmax) { y2 = function(x2); ix2 = (int)(ixmin+(x2-xmin)*(ixmax-ixmin)/(xmax-xmin)); iy2 = (int)(iymax+(y2-ymin)*(iymin-iymax)/(ymax-ymin)); g.drawLine(ix1,iy1,ix2,iy2); iy1=iy2; ix1=ix2; x2+=h; } g.setColor(oldColor); DVG1 - Grafikprogrammierung

  28. Die Klasse Panel • Panel stellt einen Bereich zur Verfügung,in dem Grafikelemente plaziert werden können. • Component add(Component comp) • fügt eine Componente comp zum Panel hinzu DVG1 - Grafikprogrammierung

  29. Die Klasse Button • public Button(String str) • erzeugt eine Schaltfläche mit der Beschriftung str • void addActionListener(ActionListener l) • fügt eine Klasse zur Behandlung der Aktion beim Betätigen der Schaltfläche hinzu z.B.: Panel Feld = new Panel(); Button Ende = new Button("Ende"); Feld.add(Ende); Ende.addActionListner(this); DVG1 - Grafikprogrammierung

  30. Methoden der Klasse ActionListener • void actionPerformed(ActionEvent e) • definiert die Aktion beim Betätigen einer Schalfläche • e.getActionCommand() liefert die Zeichenkette die zu der Schaltfläche gehört, die betätigt wurde DVG1 - Grafikprogrammierung

  31. public class FunktionsGrafik08 extends Frame implements ActionListener { private boolean draw = true; public FunktionsGrafik08() { Panel Feld = new Panel(); Button Ende = new Button("Ende"); Button Redraw = new Button("Redraw"); Feld.add(Ende); Feld.add(Redraw); Ende.addActionListener(this); Redraw.addActionListener(this); add(Feld,BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e) { String kommando = e.getActionCommand(); if (kommando.equals("Ende")) System.exit(0); if (kommando.equals("Redraw")) { draw=true; paint(this.getGraphics()); return; } } DVG1 - Grafikprogrammierung

  32. public void paint (Graphics g) { if ( !draw ) return; draw = false; DVG1 - Grafikprogrammierung

  33. Methoden der Klasse WindowAdapter • void windowClosing(WindowEvent e) • definiert die Aktion beim Schließen des Fensters DVG1 - Grafikprogrammierung

  34. public FunktionsGrafik09() { Panel Feld = new Panel(); Button Ende = new Button("Ende"); Button Redraw = new Button("Redraw"); Feld.add(Ende); Feld.add(Redraw); Ende.addActionListener(this); Redraw.addActionListener(this); add(Feld,BorderLayout.SOUTH); addWindowListener(new FensterBeenden()); } class FensterBeenden extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } DVG1 - Grafikprogrammierung

More Related