Moin,
so nun sitze ich schon 2 Tage davor, Foren und Dokus hab ich auch annähernd alle abgesucht, leider aber keine Lösung gefunden. Mein Problem ist, dass ich neue Threads erstellen will und die alten gezeichneten Objekte aber dort gespeichert bleiben, wo sie sich als letztes befunden haben.
Kurz: ich will keinen Thread pausieren sondern stoppen und mit einem neuen weiterarbeiten und mit ihm neue Objekte zeichnen. Dann widerum stoppen, Objekte speichern und neuen Thread erstellen.
so nun sitze ich schon 2 Tage davor, Foren und Dokus hab ich auch annähernd alle abgesucht, leider aber keine Lösung gefunden. Mein Problem ist, dass ich neue Threads erstellen will und die alten gezeichneten Objekte aber dort gespeichert bleiben, wo sie sich als letztes befunden haben.
Kurz: ich will keinen Thread pausieren sondern stoppen und mit einem neuen weiterarbeiten und mit ihm neue Objekte zeichnen. Dann widerum stoppen, Objekte speichern und neuen Thread erstellen.
Java:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Random;
import javax.swing.JOptionPane;
/*
* TetrisJPanel.java
*javax.swing.JPanel
* Created on 02.07.2009, 09:38:19
*/
/**
*
* @author Dom
*/
public class Tetris extends Applet implements Runnable {
int GamefieldHeight = 20;
int GamefieldWidth = 20;
int GamefieldPixel = 10;
int BlockXHeight = 40;
int BlockXWidth = 40;
int posx = 200;
int posy = 0;
int x = 0;
int Help = 0;
Thread th = new Thread(this);
Random rand = new Random();
public synchronized void start() {
th.setDaemon(true);
th.start(); // Starten des Threads
}
public synchronized void stop() {
th = null; // dereferenzieren
}
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY); // Erniedrigen der ThreadPriority um zeichnen zu erleichtern
while (true) { // Solange true ist läuft der Thread weiter
posy = posy + 20; // Verändern der x-Koordinate um 20
repaint(); // Neuzeichnen des Applets
try {
Thread.sleep(500); // Stoppen des Threads für in Klammern angegebene Millisekunden
} catch (InterruptedException ex) {
}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY); // Zurücksetzen der ThreadPriority auf Maximalwert
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 400, Short.MAX_VALUE));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 300, Short.MAX_VALUE));
}// </editor-fold>
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
for (int i = 0; i < GamefieldHeight; i++) {
for (int u = 0; u < GamefieldWidth; u++) {
g2.drawRect(GamefieldWidth, GamefieldHeight * i, GamefieldWidth * u, GamefieldHeight);
}
}
// Ab hier Code nur zu Testzwecken
// x = rand.nextInt(5);
// X
if (Help == 0) {
g2.setColor(Color.red);
g2.fillRect(posx, posy, BlockXWidth, BlockXHeight);
}
if (posy == 360) {
g2.setColor(Color.BLACK);
g2.fillRect(posx, posy, BlockXWidth, BlockXHeight);
posx = 200;
posy = 0;
Help = 1;
th.yield();
}
if (Help == 1) {
x = rand.nextInt(5);
if (x == 0) {
run();
g2.setColor(Color.red);
g2.fillRect(posx, posy, BlockXWidth, BlockXHeight);
posx = 200;
posy = 0;
//J
} else if (x == 1) {
run();
g2.setColor(Color.BLUE);
g2.drawRect(200, 0, 20, 60);
g2.drawRect(180, 60, 20, 20);
g2.fillRect(180, 40, 20, 20);
g2.fillRect(200, 0, 20, 60);
posx = 200;
posy = 0;
//T
} else if (x == 2) {
run();
g2.setColor(Color.GREEN);
g2.drawRect(200, 0, 20, 60);
g2.drawRect(180, 20, 20, 20);
g2.fillRect(200, 0, 20, 60);
g2.fillRect(180, 20, 20, 20);
posx = 200;
posy = 0;
//I
} else if (x == 3) {
run();
g2.setColor(Color.YELLOW);
g2.drawRect(200, 0, 20, 80);
g2.fillRect(200, 0, 20, 80);
posx = 200;
posy = 0;
//L
} else if (x == 4) {
run();
g2.setColor(Color.ORANGE);
g2.drawRect(200, 0, 20, 60);
g2.drawRect(220, 40, 20, 20);
g2.fillRect(200, 0, 20, 60);
g2.fillRect(220, 40, 20, 20);
posx = 200;
posy = 0;
}
}
}
// Variables declaration - do not modify
// End of variables declaration
}