Praktikum Informatik II Musterlösungen Teil 7

Werbung
Praktikum Informatik II
FB Physikalische Technik
Prof. Dr. Martin Trauth, Dr. Michael Männel
Musterlösungen
Teil 7
Aufgabe 1a
package teil7;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import teil2.Wuerfel;
public class Info2_5_16a extends JFrame implements ActionListener {
private
private
private
Wuerfel
JPanel myPanel;
JButton myButton1, myButton2;
JLabel label1;
wuerfel1 = new Wuerfel();
/* Konstruktor */
public Info2_5_16a(String str1){
super(str1);
myPanel = new JPanel();
myButton1 = new JButton("würfeln");
myButton2 = new JButton("löschen");
label1 = new JLabel("der erste Text im Label");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 300);
myPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 50,20));
this.setVisible(true);
this.setContentPane(myPanel);
myPanel.setBackground(Color.GREEN);
myPanel.add(label1);
myPanel.add(myButton1);
myPanel.add(myButton2);
myButton1.addActionListener(this);
myButton2.addActionListener(this);
}
public void actionPerformed(ActionEvent x){
Object quelle = x.getSource();
if (quelle == myButton1) {
wuerfel1.wuerfeln();
label1.setText("Augenzahl: " + wuerfel1.augenZahl);
}
if (quelle == myButton2) label1.setText("Augenzahl:");
}
1
public static void main(String[] args) {
Info2_5_16a myFrame = new Info2_5_16a("Beispiel Info2_5_16");
} // Ende main()
} // Ende Klasse
Hinweis: die Klasse Wuerfel wird in diesem Beispiel importiert, d.h. sie liegt nicht im
gleichen Package. In einem solchen Fall muss die Eigenschaft augenzahl und die
Methode wuerfeln() mit dem Zugriffs-Modifikator public deklariert werden.
2
Teil 8
Aufgabe 1
import javax.swing.*;
import java.awt.*; import java.awt.event.*;
public class GraphicTest extends JFrame implements ActionListener {
private JPanel myPanel, buttonPanel, drawPanel;
private JButton myButton;
private Graphics stift;
public GraphicTest(String str){
super(str);
myPanel = new JPanel();
myPanel.setLayout(new FlowLayout());
myButton = new JButton("malen");
myButton.addActionListener(this);
setContentPane(myPanel);
this.setSize(500, 350);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawPanel = new JPanel();
buttonPanel = new JPanel();
drawPanel.setPreferredSize(new Dimension(500, 250));
drawPanel.setBackground(Color.WHITE);
buttonPanel.setPreferredSize(new Dimension(500, 50));
myPanel.add(drawPanel);
myPanel.add(buttonPanel);
buttonPanel.add(myButton);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
stift.setColor(Color.BLUE);
stift.drawOval(100, 50, 100, 50);
stift.setColor(Color.GREEN);
stift.drawLine(0, 0, 150, 80);
stift.setColor(Color.RED);
stift.drawLine(0, 250, 500, 0);
stift.setColor(Color.BLACK);
stift.setFont(new Font("Arial", Font.PLAIN, 24));
stift.drawString("Martin Trauth", 50, 200);
}
public static void main(String[] args) {
GraphicTest myGraf = new GraphicTest("GraphicTest");
myGraf.stift = myGraf.drawPanel.getGraphics();
//myGraf.myButton.doClick();
}
} // Ende Klasse
3
Aufgabe 2
package teil8;
import javax.swing.*;
import java.awt.*; import java.awt.event.*;
public class ShowWuerfel extends JFrame implements ActionListener {
private JPanel myPanel, buttonPanel, drawPanel;
private JButton myButton;
private JLabel myLabel;
private Graphics stift;
public ShowWuerfel(String str){
super(str);
myPanel = new JPanel();
myPanel.setLayout(new FlowLayout());
myButton = new JButton("würfeln");
myLabel = new JLabel("");
myButton.addActionListener(this);
setContentPane(myPanel);
this.setSize(500, 350);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawPanel = new JPanel();
buttonPanel = new JPanel();
drawPanel.setPreferredSize(new Dimension(500, 250));
drawPanel.setBackground(Color.WHITE);
buttonPanel.setPreferredSize(new Dimension(500, 50));
myPanel.add(drawPanel);
myPanel.add(buttonPanel);
buttonPanel.add(myButton);
buttonPanel.add(myLabel);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
int augenzahl;
stift.setColor(Color.BLACK);
stift.fillRect(50, 25, 200, 200);
augenzahl = (int) (Math.random() * 6.) + 1;
stift.setColor(Color.WHITE);
switch (augenzahl) {
case 5:
stift.fillOval(70, 45, 40, 40);
stift.fillOval(190, 165, 40, 40);
case 3:
stift.fillOval(190, 45, 40, 40);
stift.fillOval(70, 165, 40, 40);
case 1:
stift.fillOval(130, 105, 40, 40);
break;
case 6:
stift.fillOval(70, 105, 40, 40);
stift.fillOval(190, 105, 40, 40);
case 4:
stift.fillOval(70, 45, 40, 40);
stift.fillOval(190, 165, 40, 40);
4
case 2:
stift.fillOval(190, 45, 40, 40);
stift.fillOval(70, 165, 40, 40);
}
myLabel.setText("Augenzahl: " + augenzahl);
}
public static void main(String[] args) throws InterruptedException {
ShowWuerfel myGraf = new ShowWuerfel("Würfeln");
myGraf.stift = myGraf.drawPanel.getGraphics();
}
} // Ende Klasse
Hinweis: hier wurde keine Klasse Wuerfel importiert, sondern die Augenzahl wird direkt
in der actionPerformed()-Methode berechnet.
5
Teil 9
Aufgabe 1
package teil9;
import javax.swing.*;
import java.awt.*; import java.awt.event.*;
public class AppSpiel extends JApplet implements ActionListener {
private JPanel myPanel, buttonPanel, drawPanel;
private JButton myButton;
private JLabel myLabel;
private Graphics stift;
public AppSpiel() {
myPanel = new JPanel();
myPanel.setLayout(new FlowLayout());
myButton = new JButton("würfeln");
myLabel = new JLabel("");
myButton.addActionListener(this);
setContentPane(myPanel);
drawPanel = new JPanel();
buttonPanel = new JPanel();
drawPanel.setPreferredSize(new Dimension(500, 250));
drawPanel.setBackground(Color.WHITE);
buttonPanel.setPreferredSize(new Dimension(500, 50));
myPanel.add(drawPanel);
myPanel.add(buttonPanel);
buttonPanel.add(myButton);
buttonPanel.add(myLabel);
}
public void actionPerformed(ActionEvent e){
int augenzahl;
stift = drawPanel.getGraphics();
stift.setColor(Color.BLACK);
stift.fillRect(50, 25, 200, 200);
augenzahl = (int) (Math.random() * 6.) + 1;
stift.setColor(Color.WHITE);
switch (augenzahl) {
case 5:
stift.fillOval(70, 45, 40, 40);
stift.fillOval(190, 165, 40, 40);
case 3:
stift.fillOval(190, 45, 40, 40);
stift.fillOval(70, 165, 40, 40);
case 1:
stift.fillOval(130, 105, 40, 40);
break;
case 6:
stift.fillOval(70, 105, 40, 40);
stift.fillOval(190, 105, 40, 40);
case 4:
stift.fillOval(70, 45, 40, 40);
stift.fillOval(190, 165, 40, 40);
6
case 2:
stift.fillOval(190, 45, 40, 40);
stift.fillOval(70, 165, 40, 40);
}
myLabel.setText("Augenzahl: " + augenzahl);
}
public void init() {
this.setVisible(true);
this.setSize(500, 350);
}
} // Ende Klasse
7
Herunterladen