TECHNISCHE UNIVERSITÄT MÜNCHEN FAKULTÄT FÜR INFORMATIK Praktikum Grundlagen der Programmierung Lösungsvorschläge zu Blatt 10 Robert Eigner, Florian Forster SS 2010 05.07.2010 Praktikum Grundlagen der Programmierung Aufgabe 48 Mondrian (Lösungsvorschlag) package de . tum . in . pgdpSS2010 . blatt10 . aufgabe48 ; import java . applet . Applet ; import java . awt . Color ; import java . awt . Graphics ; import java . awt . event . MouseEvent ; import java . awt . event . MouseListener ; import java . util . Random ; public class Mondrian extends Applet implements MouseListener { private static final long serialVersionUID = 1L; private Random rand = new Random (); private int i = 0; public void init () { setSize (600 , 800); addMouseListener (this); } public void paint ( Graphics g ){ Color [] colors = new Color [3]; colors [0] = Color . RED ; colors [1] = Color . BLUE ; colors [2] = Color . YELLOW ; g. fillRect (0 , 0, 10 , 800); g. fillRect (150 , 0, 10 , 800); g. fillRect (450 , 600 , 10 , 200); g. fillRect (590 , 0, 10 , 800); g. fillRect (0 , 0, 600 , 10); g. fillRect (0 , 200 , 150 , 20); g. fillRect (0 , 600 , 600 , 10); g. fillRect (450 , 700 , 150 , 10); Lösung 10/ Seite 2 g. fillRect (0 , 790 , 600 , 10); g. setColor ( colors [i ]); g. fillRect (160 , 10 , 430 , 590); i ++; i=i %3; g. setColor ( colors [i ]); g. fillRect (10 , 610 , 140 , 180); i ++; i=i %3; g. setColor ( colors [i ]); g. fillRect (460 , 710 , 130 , 80); } public void mouseClicked ( MouseEvent arg0 ) { if( arg0 . getButton () == MouseEvent . BUTTON1 ) { i = rand . nextInt (3); repaint (); } } public public public public void void void void mouseEntered ( MouseEvent arg0 ) {} mouseExited ( MouseEvent arg0 ) {} mousePressed ( MouseEvent arg0 ) {} mouseReleased ( MouseEvent arg0 ) {} } Der Quelltext der dazugehörenden HTML-Seite lautet: <! DOCTYPE HTML PUBLIC " -// W3C // DTD HTML 4.01 Transitional // EN " " http :// www . w3 . org / TR / html4 / loose . dtd " > <html > <head > <title > Mondrian als Java - Applet </ title > </ head > <body > <h1 > Ein & quot ; Mondrian - Bild - Generator & quot ; </h1 > <p > Klicken Sie ins Bild , um die Farben zu & auml ; ndern </p > <p > < applet code =" Mondrian . class " width =" 600 " height =" 800 " alt =" Ein & quot ; Mondrian - Bild - Generator & quot ;" ></ applet > </p > </ body > </ html > Aufgabe 49 Fotoalbum (Lösungsvorschlag) import import import import java . awt . BorderLayout ; java . awt . GridBagLayout ; java . io . File ; java . io . FileFilter ; Lösung 10/ Seite 3 import import import import import import import javax . swing . ImageIcon ; javax . swing . JButton ; javax . swing . JFileChooser ; javax . swing . JFrame ; javax . swing . JLabel ; javax . swing . JPanel ; javax . swing . JScrollPane ; public class FotoAlbum extends JFrame { private private private private private private private private private private private private private static final long serialVersionUID = 1L; JPanel jContentPane = null; JButton okButton = null; JButton previousButton = null; JButton nextButton = null; JScrollPane scrollPane = null; JFileChooser chooser = new JFileChooser (); File dir ; File files [] = null; int i = 0; JLabel imageLabel ; JLabel textLabel ; ImageIcon images [] = null; public FotoAlbum () { super(); initialize (); } private void initialize () { this. setSize (900 , 700); this. setContentPane ( getJContentPane ()); this. setTitle (" Fotoalbum " ); } private JPanel getJContentPane () { if ( jContentPane == null) { jContentPane = new JPanel (new BorderLayout ()); JPanel jp = new JPanel (); jp . add ( getOKButton ()); jContentPane . add (jp , BorderLayout . NORTH ); jp = new JPanel (new GridBagLayout ()); jp . add ( getPreviousButton ()); jContentPane . add (jp , BorderLayout . WEST ); jp = new JPanel (new GridBagLayout ()); jp . add ( getNextButton ()); jContentPane . add (jp , BorderLayout . EAST ); jp = new JPanel (); getTextLabel (). setText (" Please select directory first !" ); jp . add ( getTextLabel ()); jContentPane . add (jp , BorderLayout . SOUTH ); jContentPane . add ( getJScrollPane ()); } return jContentPane ; Lösung 10/ Seite 4 } private JButton getOKButton () { if ( okButton == null) { okButton = new JButton (" Open " ); okButton . addActionListener (new java . awt . event . ActionListener () { public void actionPerformed ( java . awt . event . ActionEvent e) { chooser . setCurrentDirectory (new java . io . File ("." )); chooser . setFileSelectionMode ( JFileChooser . DIRECTORIES_ONLY ); chooser . setAcceptAllFileFilterUsed (false); if ( chooser . showOpenDialog ( FotoAlbum .this) == JFileChooser . APPROVE_OPTION ) { dir = chooser . getSelectedFile (); files = dir . listFiles (new FileFilter () { public boolean accept ( File file ) { return ( file . getName (). toLowerCase (). endsWith (". gif ") || file . getName (). toLowerCase (). endsWith (". jpg ") || file . getName (). toLowerCase (). endsWith (". png " )); } }); images = new ImageIcon [ files . length ]; for (int j = 0; j < files . length ; j ++) { images [j] = new ImageIcon ( files [j ]. getAbsolutePath ()); images [j ]. setDescription ( files [j ]. getAbsolutePath ()); i = 0; } getTextLabel (). setText ( images [i ]. getDescription ()); getImageLabel (). setIcon ( images [i ]); getJScrollPane (). getViewport (). add ( getImageLabel ()); } } }); } return okButton ; } private JButton getPreviousButton () { if ( previousButton == null) { previousButton = new JButton (" previous " ); previousButton . addActionListener (new java . awt . event . ActionListener () { public void actionPerformed ( java . awt . event . ActionEvent e) { i - -; if (i == -1) i = images . length - 1; getTextLabel (). setText ( images [i ]. getDescription ()); getImageLabel (). setIcon ( images [i ]); getImageLabel (). repaint (); } }); } Lösung 10/ Seite 5 return previousButton ; } private JButton getNextButton () { if ( nextButton == null) { nextButton = new JButton (" next " ); nextButton . addActionListener (new java . awt . event . ActionListener () { public void actionPerformed ( java . awt . event . ActionEvent e) { i ++; i = i % files . length ; getTextLabel (). setText ( images [i ]. getDescription ()); getImageLabel (). setIcon ( images [i ]); getImageLabel (). repaint (); } }); } return nextButton ; } private JScrollPane getJScrollPane () { if ( scrollPane == null) { scrollPane = new JScrollPane (); } return scrollPane ; } private JLabel getTextLabel () { if ( textLabel == null) { textLabel = new JLabel (); } return textLabel ; } private JLabel getImageLabel () { if ( imageLabel == null) { imageLabel = new JLabel (); } return imageLabel ; } }