17.01.2014 Views

BA Karlsruhe, Vorlesung Programmieren, „Threads“

BA Karlsruhe, Vorlesung Programmieren, „Threads“

BA Karlsruhe, Vorlesung Programmieren, „Threads“

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

DHBW <strong>Karlsruhe</strong>, <strong>Vorlesung</strong> <strong>Programmieren</strong>, „Threads“ (1)<br />

Musterlösungen<br />

Aufgabe 1 „Ampel” (Lösung 1)<br />

import java.awt.Color;<br />

import java.awt.Graphics;<br />

import javax.swing.JApplet;<br />

public class Ampel extends JApplet implements Runnable {<br />

private Ampelphase[] phasen;<br />

private Ampelphase aktuellePhase;<br />

private Thread thread;<br />

@Override public void init() {<br />

phasen = new Ampelphase[4];<br />

phasen[3] = new Ampelphase("Rotgelb", true, true, false, 1);<br />

phasen[2] = new Ampelphase("Rot", true, false, false, 10, phasen[3]);<br />

phasen[1] = new Ampelphase("Gelb", false, true, false, 2, phasen[2]);<br />

phasen[0] = new Ampelphase("Grün", false, false, true, 10, phasen[1]);<br />

phasen[3].setNext(phasen[0]);<br />

aktuellePhase = phasen[0];<br />

}<br />

public void run() {<br />

while (true)<br />

try {<br />

this.repaint();<br />

Thread.sleep(aktuellePhase.getDuration() * 500);<br />

aktuellePhase = aktuellePhase.getNext();<br />

} catch (InterruptedException ex) {}<br />

}<br />

@Override public void stop() {<br />

if (thread != null) thread = null;<br />

}<br />

@Override public void start() {<br />

if (thread == null) {<br />

thread = new Thread(this);<br />

thread.start();<br />

}<br />

}<br />

}<br />

@Override public void paint(Graphics g) {<br />

g.setColor(Color.BLACK);<br />

g.fillRect(10, 10, 80, 195);<br />

g.setColor(Color.WHITE);<br />

g.fillOval(23, 23, 54, 54);<br />

g.fillOval(23, 83, 54, 54);<br />

g.fillOval(23, 143, 54, 54);<br />

g.setColor(Color.RED);<br />

if (aktuellePhase.isRed()) g.fillOval(25, 25, 50, 50);<br />

g.setColor(Color.YELLOW);<br />

if (aktuellePhase.isYellow()) g.fillOval(25, 85, 50, 50);<br />

g.setColor(Color.GREEN);<br />

if (aktuellePhase.isGreen()) g.fillOval(25, 145, 50, 50);<br />

}


public class Ampelphase {<br />

}<br />

private String name;<br />

private boolean red;<br />

private boolean yellow;<br />

private boolean green;<br />

private int duration;<br />

private Ampelphase next;<br />

public Ampelphase(String name, boolean red, boolean yellow, boolean green,<br />

int duration, Ampelphase next) {<br />

this.name=name;<br />

this.red=red;<br />

this.yellow=yellow;<br />

this.green=green;<br />

this.duration=duration;<br />

this.next=next;<br />

}<br />

public Ampelphase(String name, boolean red, boolean yellow, boolean green,<br />

int duration) {<br />

this.name=name;<br />

this.red=red;<br />

this.yellow=yellow;<br />

this.green=green;<br />

this.duration=duration;<br />

this.next=null;<br />

}<br />

public String getName() { return name; }<br />

public void setName(String name) { this.name = name; }<br />

public boolean isRed() { return red; }<br />

public void setRed(boolean red) { this.red = red; }<br />

public boolean isYellow() { return yellow; }<br />

public void setYellow(boolean yellow) { this.yellow = yellow; }<br />

public boolean isGreen() { return green; }<br />

public void setGreen(boolean green) { this.green = green; }<br />

public int getDuration() { return duration; }<br />

public void setDuration(int duration) { this.duration = duration; }<br />

public Ampelphase getNext() { return next; }<br />

public void setNext(Ampelphase next) { this.next = next; }


Aufgabe 1 „Ampel” (Lösung 2 mit enum)<br />

import java.awt.Color;<br />

import java.awt.Graphics;<br />

import javax.swing.JApplet;<br />

public class Ampel extends JApplet implements Runnable {<br />

}<br />

private int state = 0;<br />

private Thread thread;<br />

@Override<br />

public void run() {<br />

while (true)<br />

try {<br />

state = (++state) % Ampelphase.values().length;<br />

this.repaint();<br />

Thread.sleep(Ampelphase.values()[state].getDuration() * 500);<br />

} catch (InterruptedException ex) {}<br />

}<br />

@Override public void stop() {<br />

if (thread != null) thread = null;<br />

}<br />

@Override public void start() {<br />

if (thread == null) {<br />

thread = new Thread(this);<br />

thread.start();<br />

}<br />

}<br />

@Override public void paint(Graphics g) {<br />

Ampelphase a = Ampelphase.values()[state];<br />

g.setColor(Color.BLACK);<br />

g.fillRect(10, 10, 80, 195);<br />

g.setColor(Color.WHITE);<br />

g.fillOval(23, 23, 54, 54);<br />

g.fillOval(23, 83, 54, 54);<br />

g.fillOval(23, 143, 54, 54);<br />

g.setColor(Color.RED);<br />

if (a.isRed()) g.fillOval(25, 25, 50, 50);<br />

g.setColor(Color.YELLOW);<br />

if (a.isYellow()) g.fillOval(25, 85, 50, 50);<br />

g.setColor(Color.GREEN);<br />

if (a.isGreen()) g.fillOval(25, 145, 50, 50);<br />

}


public enum Ampelphase {<br />

}<br />

GRUEN("Grün",false,false,true,10),<br />

GELB("Gelb",false,true,false,2),<br />

ROT("Rot",true,false,false,10),<br />

GELBROT("Gelbrot",true,true,false,1);<br />

private String name;<br />

private boolean red;<br />

private boolean yellow;<br />

private boolean green;<br />

private int duration;<br />

Ampelphase(String name, boolean red, boolean yellow,<br />

boolean green, int duration) {<br />

this.name=name;<br />

this.red=red;<br />

this.yellow=yellow;<br />

this.green=green;<br />

this.duration=duration;<br />

}<br />

public String getName() { return name; }<br />

public void setName(String name) { this.name = name; }<br />

public boolean isRed() { return red; }<br />

public void setRed(boolean red) { this.red = red; }<br />

public boolean isYellow() { return yellow; }<br />

public void setYellow(boolean yellow) { this.yellow = yellow; }<br />

public boolean isGreen() { return green; }<br />

public void setGreen(boolean green) { this.green = green; }<br />

public int getDuration() { return duration; }<br />

public void setDuration(int duration) { this.duration = duration; }


Aufgabe 2 „Tanzende Schrift” (Lösung A)<br />

import javax.swing.JApplet;<br />

import java.awt.*;<br />

public class Schrift extends JApplet implements Runnable {<br />

}<br />

int red, green, blue, x, y;<br />

int sleep = 50;<br />

String text = "Mein Name";<br />

Thread t;<br />

@Override<br />

public void init() {<br />

red = (int) (Math.random()*256);<br />

green = (int) (Math.random()*256);<br />

blue = (int) (Math.random()*256);<br />

x = this.getWidth() / 3;<br />

y = this.getHeight() / 2;<br />

t = new Thread(this);<br />

t.start();<br />

}<br />

public void run() {<br />

while (true) {<br />

try {<br />

x = newRandomN(x,0,this.getWidth()-200,6);<br />

y = newRandomN(y,24,this.getHeight(),6);<br />

repaint();<br />

Thread.sleep(sleep);<br />

} catch (InterruptedException ex) { }<br />

}<br />

}<br />

@Override<br />

public void paint(Graphics g) {<br />

g.setColor(Color.WHITE); // vorherigen Fensterinhalt löschen<br />

g.fillRect(0, 0, this.getWidth(), this.getHeight());<br />

g.setFont(new Font("Arial",Font.BOLD,30));<br />

for (int i=0;i


Aufgabe 2 „Tanzende Schrift” (Lösung B)<br />

import javax.swing.JApplet;<br />

import java.awt.*;<br />

public class TanzendeSchrift extends JApplet implements Runnable {<br />

static Color[] Palette = {Color.black, Color.blue, Color.cyan, Color.darkGray,<br />

Color.gray, Color.green, Color.magenta, Color.orange, Color.pink,<br />

Color.red, Color.white, Color.yellow<br />

};<br />

Thread thread;<br />

@Override<br />

public void start() {<br />

if (thread == null) {<br />

setSize(220, 100);<br />

thread = new Thread(this, "Schrift");<br />

thread.start();<br />

}<br />

}<br />

public void run() {<br />

while (thread != null)<br />

try {<br />

repaint();<br />

Thread.sleep(200);<br />

} catch (InterruptedException e) { }<br />

}<br />

@Override<br />

public void paint(Graphics g) {<br />

g.setColor(Color.WHITE); // vorherigen Fensterinhalt löschen<br />

g.fillRect(0, 0, this.getWidth(), this.getHeight());<br />

char text[] = "Tanzende Schrift".toCharArray();<br />

g.setFont(new Font("Helvetica", Font.BOLD, 20));<br />

FontMetrics m = g.getFontMetrics();<br />

int height = m.getHeight() + 5;<br />

int width = m.charWidth(text[0]);<br />

for (int i = 1; i < text.length; i++)<br />

if (m.charWidth(text[i]) > width)<br />

width = m.charWidth(text[i]);<br />

for (int i = 0; i < text.length; i++) {<br />

int start = (int) Math.round(20 * Math.random());<br />

g.setColor(Palette[start % Palette.length]);<br />

g.drawChars(text, i, 1, i * width, height + start);<br />

}<br />

}<br />

}<br />

@Override<br />

public void stop() {<br />

if (thread != null) {<br />

thread = null;<br />

}<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!