Klasse Ballon
import java.awt.*;
public class Ballon
{
private int durchmesser;
private int xKoord, yKoord;
private Color ffarbe;
public Ballon(int anfangsDurchmesser, int anfangsX, int
anfangsY,Color farbe)
{
durchmesser =
anfangsDurchmesser;
xKoord = anfangsX;
yKoord = anfangsY;
ffarbe = farbe;
}
public void groesseVeraendern(int veraenderung)
{
durchmesser =
durchmesser + veraenderung;
}
public void anzeigen(Graphics g)
{
g.setColor(ffarbe);
g.fillOval(xKoord,yKoord,durchmesser,durchmesser);
}
}
Applet Spielball (Benutzt die Klasse Ballon)
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Kreis extends Applet {
Button schrumpfen = new
Button();
Button wachsen = new
Button();
private Ballon meinBallon;
private Ballon meinBallon1;
public void init() {
try {
jbInit();
}
catch(Exception
e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setLayout(null);
schrumpfen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e)
{
schrumpfen_actionPerformed(e);
}
}
});
wachsen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e)
{
wachsen_actionPerformed(e);
}
});
this.add(wachsen,
null);
this.add(schrumpfen,
null);
schrumpfen.setLabel("Schrumpfen");
schrumpfen.setBounds(new Rectangle(63, 29, 102, 27));
wachsen.setLabel("Wachsen");
wachsen.setBounds(new Rectangle(230, 26, 109, 27));
meinBallon =
new Ballon(100, 80, 120, Color.blue);
meinBallon1 = new Ballon(100,
200, 120, Color.red);
}
// Ende jbInit()
public void paint (Graphics g) {
meinBallon.anzeigen(g);
meinBallon1.anzeigen(g);
}
void schrumpfen_actionPerformed(ActionEvent
e) {
meinBallon.groesseVeraendern(-10);
meinBallon1.groesseVeraendern(-10);
repaint();
}
void wachsen_actionPerformed(ActionEvent
e) {
meinBallon.groesseVeraendern(10);
meinBallon1.groesseVeraendern(10);
repaint();
}
}
// Ende class SpielBall
//Beispiel Logische
Verknüpfungen:
//Aufgabe 1
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class und extends Applet {
Button eins = new
Button();
Label und = new
Label();
Button zwei = new
Button();
Button drei = new
Button();
Button vier = new
Button();
Label undoben = new Label();
Label undunten = new Label();
Label undoben2 = new
Label();
Label ergebniss = new Label();
Label oben = new
Label();
Label unten = new
Label();
boolean e1,e2,a;
//Das Applet initialisieren
public void init() {
try {
jbInit();
}
catch(Exception
e) {
e.printStackTrace();
}
}
//Initialisierung der Komponenten
private void jbInit() throws Exception {
eins.setLabel("1");
eins.setBounds(new Rectangle(58, 48, 72, 25));
eins.addActionListener(new und_eins_actionAdapter(this));
this.setLayout(null);
und.setBackground(Color.green);
und.setFont(new java.awt.Font("Dialog",
0, 30));
und.setText(" &");
und.setBounds(new Rectangle(180, 47, 65, 127));
zwei.setLabel("0");
zwei.setBounds(new Rectangle(59, 74, 71, 25));
zwei.addActionListener(new und_zwei_actionAdapter(this));
drei.setLabel("1");
drei.setBounds(new Rectangle(60, 122, 71, 25));
drei.addActionListener(new und_drei_actionAdapter(this));
vier.setLabel("0");
vier.setBounds(new Rectangle(60, 147, 71, 25));
vier.addActionListener(new und_vier_actionAdapter(this));
undoben.setBackground(Color.green);
undoben.setText("");
undoben.setBounds(new Rectangle(128, 65, 56, 15));
undunten.setBounds(new Rectangle(129, 140, 56, 15));
undunten.setText("");
undunten.setBackground(Color.green);
undoben2.setBounds(new
Rectangle(241, 105, 56, 15));
undoben2.setText("");
undoben2.setBackground(Color.green);
ergebniss.setBackground(Color.white);
ergebniss.setFont(new java.awt.Font("Dialog",
0, 20));
//ergebniss.setText("label1");
ergebniss.setBounds(new Rectangle(300, 96, 37, 30));
//oben.setText("label1");
oben.setBounds(new Rectangle(143, 49, 28, 15));
unten.setBounds(new Rectangle(143, 122, 28, 15));
//unten.setText("label1");
this.add(eins,
null);
this.add(zwei,
null);
this.add(drei,
null);
this.add(vier,
null);
this.add(und,
null);
this.add(undunten, null);
this.add(ergebniss, null);
this.add(undoben2,
null);
this.add(undoben, null);
this.add(oben,
null);
this.add(unten,
null);
}
void eins_actionPerformed(ActionEvent
e) {
oben.setText("1");
e1=true;
if(e1==true&&e2==true){
ergebniss.setText("1");
}
else{
ergebniss.setText("0");
}
}
void zwei_actionPerformed(ActionEvent
e) {
oben.setText("0");
e1=false;
if(e1==true&&e2==true){
ergebniss.setText("1");
}
else{
ergebniss.setText("0");
}
}
void drei_actionPerformed(ActionEvent
e) {
unten.setText("1");
e2=true;
if(e1==true&&e2==true){
ergebniss.setText("1");
}
else{
ergebniss.setText("0");
}
}
void vier_actionPerformed(ActionEvent
e) {
unten.setText("0");
e2=false;
if(e1==true&&e2==true){
ergebniss.setText("1");
}
else{
ergebniss.setText("0");
}
}
class und_drei_actionAdapter implements java.awt.event.ActionListener
{
und adaptee;
und_drei_actionAdapter(und
adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.drei_actionPerformed(e);
}
}
class und_eins_actionAdapter implements java.awt.event.ActionListener
{
und adaptee;
und_eins_actionAdapter(und
adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.eins_actionPerformed(e);
}
}
class und_zwei_actionAdapter implements java.awt.event.ActionListener
{
und adaptee;
und_zwei_actionAdapter(und
adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.zwei_actionPerformed(e);
}
}
class und_vier_actionAdapter implements java.awt.event.ActionListener
{
und adaptee;
und_vier_actionAdapter(und
adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.vier_actionPerformed(e);
}
}
}
// Ende Programm
Aufgabe 2
Klasse Und
public class Und {
private boolean
e1,e2,a;
public Und() {
e1 = e2 = a = false;
}
public void setE1( boolean wert ) {
e1 = wert;
a = e1 && e2;
}
public void setE2( boolean wert ) {
e2 = wert;
a = e1 && e2;
}
public boolean getA() {
return a;
}
public boolean getE1() {
return e1;
}
public boolean getE2() {
return e2;
}
}
Klasse Oder
public class Oder {
private boolean
e1,e2,a;
public Oder() {
e1 = e2 = a = false;
}
public void setE1( boolean wert ) {
e1 = wert;
a = e1 || e2;
}
public void setE2( boolean wert ) {
e2 = wert;
a = e1 || e2;
}
public boolean getA() {
return a;
}
public boolean getE1() {
return e1;
}
public boolean getE2() {
return e2;
}
}
Klasse Nicht
public class Nicht {
private boolean
e1,a;
public Nicht() {
e1 = a = false;
}
public void setE1( boolean wert ) {
e1 = wert;
a =
!e1;
}
public boolean getA() {
return a;
}
public boolean getE1() {
return e1;
}
}
Applet zur Logischen Verknüpfung
(und, oder und nicht)
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ooplogig2 extends Applet {
// private Klasse objekt;
private Und und1;
private Oder oder1;
private Nicht nicht1;
TextArea textArea1
= new TextArea();
TextArea textArea2
= new TextArea();
TextArea textArea3
= new TextArea();
public void init() {
try
{jbInit();}
catch(Exception
e)
{e.printStackTrace();}
}
private void jbInit() throws Exception {
this.setLayout(null);
this.setBackground(Color.white);
textArea1.setBounds(new
Rectangle(1, 1, 195, 90));
textArea1.setFont(new
java.awt.Font("Dialog", 0, 14));
// objekt1 = new
Klasse();
und1 = new
Und();
oder1 = new
Oder();
nicht1 = new
Nicht();
textArea2.setFont(new
java.awt.Font("Dialog", 0, 14));
textArea2.setBounds(new
Rectangle(0, 97, 195, 96));
textArea3.setBounds(new
Rectangle(0, 199, 195, 93));
textArea3.setFont(new
java.awt.Font("Dialog", 0, 14));
this.add(textArea1,
null);
this.add(textArea2,
null);
this.add(textArea3,
null);
}
public void paint(Graphics g)
{
//
g.drawImage(getImage (getDocumentBase(),"Name des Bildes"),x,y,Breite,Hoehe,this);
g.drawImage(getImage (getDocumentBase(),"Und.JPG"),200,0,420,91,this);
g.drawImage(getImage (getDocumentBase(),"Oder.JPG"),200,100,420,91,this);
g.drawImage(getImage
(getDocumentBase(),"Nicht.JPG"),200,200,420,91,this);
verarbeiten();
}
private void
verarbeiten() {
// Anweisungen;
und1.setE1(true);
und1.setE2(true);
textArea1.setText(" Und \n E1 =
"+und1.getE1());
textArea1.append("\n E2 =
"+und1.getE2());
textArea1.append("\n A = "+und1.getA());
//textArea1.setText("Textausgabe
Zeile 1 \nZeile 2:");
//textArea1.append("\n "+und1.getA());
oder1.setE1(true);
oder1.setE2(false);
textArea2.setText(" Oder \n E1 =
"+oder1.getE1());
textArea2.append("\n E2 =
"+oder1.getE2());
textArea2.append("\n A = "+oder1.getA());
//textArea2.setText("Textausgabe
Zeile 1 \nZeile 2:");
//textArea2.append("\n "+oder1.getA());
nicht1.setE1(true);
textArea3.setText(" Nicht \n E1
= "+nicht1.getE1());
textArea3.append("\n A = "+nicht1.getA());
//textArea3.setText("Textausgabe Zeile
1 \nZeile 3:");
//textArea3.append("\n "+nicht1.getA());
}
} // Ende Applet