PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : doEvents in Java


Kinman
2004-06-20, 19:38:25
Hi, gibt es in Java soetwas wie unter visual Basic doEvents ist?
Bzw eine refresh funktion?

Danke, mfg Kinman

ethrandil
2004-06-20, 20:52:16
Ja und Nein :)

Es gibt die Möglichkeit selber events zu feuern.

Ansonsten werden Events ohnehin unverzüglich ausgelöst.

Was genau ist denn dein Problem? In Java sollte eigentlich alles ohne ein solches 'doEvents' zu realisieren sein.

- Eth

Kinman
2004-06-20, 21:39:05
ich ändere mittels button die farbe eines panles (setBackground) und mach nachernd ein sleep.
und dann wechsel ich die farbe wieder und so weiter.

Jedoch wird immer erst die letzte Farbe angezeigt.

Hier mal alles:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;

public class FarbSpiel extends JFrame implements ActionListener
{
Container cp;

//PANELS
JPanel southP = new JPanel();
JPanel northP = new JPanel();
JPanel eastP = new JPanel();
JPanel westP = new JPanel();
JPanel centerP = new JPanel();

//CONTROLS
JButton cmdRed = new JButton("Rot");
JButton cmdGreen = new JButton("Grün");
JButton cmdBlue = new JButton("Blau");
JButton cmdYellow = new JButton("Gelb");
JButton cmdStart = new JButton("Start");

JLabel lblHead = new JLabel("Farbspiel - (c) 2004 by Dietrichsteiner Emanuel");

//PROBERITES
int leftColor = 1;
int rightColor = 0;
int Colors[] = new int[100];
int UserColors[] = new int[100];
int i = 0;
int Counter = 0;
int UserCounter = 0;
int Final = 0;
boolean inProcess = false;
Random temp = new Random();


public FarbSpiel()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cp = getContentPane();
cp.setLayout(new BorderLayout());

//-----------------------------------------
//Adding Controls

cp.add(centerP,BorderLayout.CENTER);
cp.add(southP,BorderLayout.SOUTH);
cp.add(northP,BorderLayout.NORTH);
cp.add(eastP,BorderLayout.EAST);
cp.add(westP,BorderLayout.WEST);

centerP.add(lblHead);

southP.add(cmdRed);
northP.add(cmdBlue);
eastP.add(cmdGreen);
westP.add(cmdYellow);
centerP.add(cmdStart);

//-----------------------------------------
//Adding controls to the ActionListener

cmdRed.addActionListener(this);
cmdGreen.addActionListener(this);
cmdBlue.addActionListener(this);
cmdYellow.addActionListener(this);
cmdStart.addActionListener(this);

//-----------------------------------------
//Setting predefinied colors



//-----------------------------------------
//Setup JFrame
setTitle("Farbspiel");
setBounds(0,0,480,300);
setVisible(true);

neu();
}

public void neu()
{

southP.setBackground(Color.GRAY);
northP.setBackground(Color.GRAY);
eastP.setBackground(Color.GRAY);
westP.setBackground(Color.GRAY);
centerP.setBackground(Color.WHITE);
Counter = 0;
Final = 0;

for (i = 0; i < 100; i++)
{
Colors[i] = Math.abs(temp.nextInt()) % 4;
}
inProcess = false;
cmdStart.setVisible(true);
cmdRed.setVisible(false);
cmdBlue.setVisible(false);
cmdGreen.setVisible(false);
cmdYellow.setVisible(false);
}

public void doNext()
{
int j = 0;

UserCounter = 0;
for (i = 0; i < 100; i++)
{
UserColors[i] = 4;
}


while (j <= Counter)
{
if (Colors[j] == 0)
{
southP.setBackground(Color.RED);
northP.setBackground(Color.GRAY);
eastP.setBackground(Color.GRAY);
westP.setBackground(Color.GRAY);
System.out.println("Rot");
}

if (Colors[j] == 1)
{
northP.setBackground(Color.BLUE);
southP.setBackground(Color.GRAY);
eastP.setBackground(Color.GRAY);
westP.setBackground(Color.GRAY);
System.out.println("Blau");
}

if (Colors[j] == 2)
{
eastP.setBackground(Color.GREEN);
southP.setBackground(Color.GRAY);
northP.setBackground(Color.GRAY);
westP.setBackground(Color.GRAY);
System.out.println("Grün");
}

if (Colors[j] == 3)
{
westP.setBackground(Color.YELLOW);
southP.setBackground(Color.GRAY);
northP.setBackground(Color.GRAY);
eastP.setBackground(Color.GRAY);
System.out.println("Gelb");
}
Toolkit.getDefaultToolkit().beep();
j++;
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
}
}
System.out.println(Counter);
Counter++;
}

public void actionPerformed(ActionEvent e)
{

if (e.getSource() == cmdStart)
{
if (inProcess == false)
{
doNext();
inProcess = true;
cmdStart.setVisible(false);
cmdRed.setVisible(true);
cmdBlue.setVisible(true);
cmdGreen.setVisible(true);
cmdYellow.setVisible(true);

}
}
if (e.getSource() == cmdRed)
{
if (UserCounter < Counter - 1)
{
if (Colors[UserCounter] != 0)
{
Final = Counter - 1;
JOptionPane.showMessageDialog(this, "Fehler! Sie haben " + Final + " Schritte geschafft.\nStart klicken für neues Spiel","FarbTest",JOptionPane.INFORMATION_MESSAGE);
neu();
}
else
{
UserCounter++;
}
}
else
{
doNext();
}
}

if (e.getSource() == cmdBlue)
{
if (UserCounter < Counter - 1)
{
if (Colors[UserCounter] != 1)
{
Final = Counter - 1;
JOptionPane.showMessageDialog(this, "Fehler! Sie haben " + Final + "Schritte geschaft.\nStart klicken für neues Spiel","FarbTest",JOptionPane.INFORMATION_MESSAGE);
neu();
}
else
{
UserCounter++;
}
}
else
{
doNext();
}

}

if (e.getSource() == cmdGreen)
{
if (UserCounter < Counter - 1)
{
if (Colors[UserCounter] != 2)
{
Final = Counter - 1;
JOptionPane.showMessageDialog(this, "Fehler! Sie haben " + Final + "Schritte geschaft.\nStart klicken für neues Spiel","FarbTest",JOptionPane.INFORMATION_MESSAGE);
neu();
}
else
{
UserCounter++;
}
}
else
{
doNext();
}

}

if (e.getSource() == cmdYellow)
{
if (UserCounter < Counter - 1)
{
if (Colors[UserCounter] != 3)
{
Final = Counter - 1;
JOptionPane.showMessageDialog(this, "Fehler! Sie haben " + Final + "Schritte geschaft.\nStart klicken für neues Spiel","FarbTest",JOptionPane.INFORMATION_MESSAGE);
neu();
}
else
{
UserCounter++;
}
}
else
{
doNext();
}

}
}

public static void main(String args[])
{
FarbSpiel FS = new FarbSpiel();
}
}


mfg Kinman

ethrandil
2004-06-20, 22:08:23
Hmm... ich kann dein Spiel spielen....
Und ich sehe keinen Fehler...


Welche Java-Version hast du?

- Eth

EDIT: okay, ic glaub ich weiß nun den Fehler den ich sehen sollte. ich such mal.
EDIT2:

Ich denke ich kenne nun den Fehler.

Das hängt mit den Threads unter Java zusammen.

Du führst deine Schleife im Event-Thread aus. In dem selbnen Thread laufen aber auch die Aktualisierungen für die Panels. Da der Thread aber ausgelastet ist bis deine Schleife fertig ist kommt die aktualisierung auch erst danach zum Zug.

Ich erarbeite mal eine Lösung :)

ethrandil
2004-06-20, 22:15:56
hier, ich glaube das ist nicht das schönste (HellHorse???), aber es funktioniert und verdeutlicht das Prinzip.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class FarbSpiel extends JFrame implements ActionListener
{
Container cp;

//PANELS
JPanel southP = new JPanel();
JPanel northP = new JPanel();
JPanel eastP = new JPanel();
JPanel westP = new JPanel();
JPanel centerP = new JPanel();

//CONTROLS
JButton cmdRed = new JButton("Rot");
JButton cmdGreen = new JButton("Grün");
JButton cmdBlue = new JButton("Blau");
JButton cmdYellow = new JButton("Gelb");
JButton cmdStart = new JButton("Start");

JLabel lblHead = new JLabel("Farbspiel - (c) 2004 by Dietrichsteiner Emanuel");

//PROBERITES
int leftColor = 1;
int rightColor = 0;
int Colors[] = new int[100];
int UserColors[] = new int[100];
int i = 0;
int Counter = 0;
int UserCounter = 0;
int Final = 0;
boolean inProcess = false;
Random temp = new Random();


public FarbSpiel()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cp = getContentPane();
cp.setLayout(new BorderLayout());

//-----------------------------------------
//Adding Controls

cp.add(centerP,BorderLayout.CENTER);
cp.add(southP,BorderLayout.SOUTH);
cp.add(northP,BorderLayout.NORTH);
cp.add(eastP,BorderLayout.EAST);
cp.add(westP,BorderLayout.WEST);

centerP.add(lblHead);

southP.add(cmdRed);
northP.add(cmdBlue);
eastP.add(cmdGreen);
westP.add(cmdYellow);
centerP.add(cmdStart);

//-----------------------------------------
//Adding controls to the ActionListener

cmdRed.addActionListener(this);
cmdGreen.addActionListener(this);
cmdBlue.addActionListener(this);
cmdYellow.addActionListener(this);
cmdStart.addActionListener(this);

//-----------------------------------------
//Setting predefinied colors



//-----------------------------------------
//Setup JFrame
setTitle("Farbspiel");
setBounds(0,0,480,300);
setVisible(true);

neu();
}

public void neu()
{

southP.setBackground(Color.GRAY);
northP.setBackground(Color.GRAY);
eastP.setBackground(Color.GRAY);
westP.setBackground(Color.GRAY);
centerP.setBackground(Color.WHITE);
Counter = 0;
Final = 0;

for (i = 0; i < 100; i++)
{
Colors[i] = Math.abs(temp.nextInt()) % 4;
}
inProcess = false;
cmdStart.setVisible(true);
cmdRed.setVisible(false);
cmdBlue.setVisible(false);
cmdGreen.setVisible(false);
cmdYellow.setVisible(false);
}

private void doNext(){
new Thread(new Runnable(){
public void run() {
doNextEx();
}}).start();
}

public void doNextEx()
{
int j = 0;

UserCounter = 0;
for (i = 0; i < 100; i++)
{
UserColors[i] = 4;
}


while (j <= Counter)
{
if (Colors[j] == 0)
{
southP.setBackground(Color.RED);
northP.setBackground(Color.GRAY);
eastP.setBackground(Color.GRAY);
westP.setBackground(Color.GRAY);
System.out.println("Rot");
}

if (Colors[j] == 1)
{
northP.setBackground(Color.BLUE);
southP.setBackground(Color.GRAY);
eastP.setBackground(Color.GRAY);
westP.setBackground(Color.GRAY);
System.out.println("Blau");
}

if (Colors[j] == 2)
{
eastP.setBackground(Color.GREEN);
southP.setBackground(Color.GRAY);
northP.setBackground(Color.GRAY);
westP.setBackground(Color.GRAY);
System.out.println("Grün");
}

if (Colors[j] == 3)
{
westP.setBackground(Color.YELLOW);
southP.setBackground(Color.GRAY);
northP.setBackground(Color.GRAY);
eastP.setBackground(Color.GRAY);
System.out.println("Gelb");
}
Toolkit.getDefaultToolkit().beep();
j++;
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
}
}
System.out.println(Counter);
Counter++;
}

public void actionPerformed(ActionEvent e)
{

if (e.getSource() == cmdStart)
{
if (inProcess == false)
{
doNext();
inProcess = true;
cmdStart.setVisible(false);
cmdRed.setVisible(true);
cmdBlue.setVisible(true);
cmdGreen.setVisible(true);
cmdYellow.setVisible(true);

}
}
if (e.getSource() == cmdRed)
{
if (UserCounter < Counter - 1)
{
if (Colors[UserCounter] != 0)
{
Final = Counter - 1;
JOptionPane.showMessageDialog(this, "Fehler! Sie haben " + Final + " Schritte geschafft.\nStart klicken für neues Spiel","FarbTest",JOptionPane.INFORMATION_MESSAGE);
neu();
}
else
{
UserCounter++;
}
}
else
{
doNext();
}
}

if (e.getSource() == cmdBlue)
{
if (UserCounter < Counter - 1)
{
if (Colors[UserCounter] != 1)
{
Final = Counter - 1;
JOptionPane.showMessageDialog(this, "Fehler! Sie haben " + Final + "Schritte geschaft.\nStart klicken für neues Spiel","FarbTest",JOptionPane.INFORMATION_MESSAGE);
neu();
}
else
{
UserCounter++;
}
}
else
{
doNext();
}

}

if (e.getSource() == cmdGreen)
{
if (UserCounter < Counter - 1)
{
if (Colors[UserCounter] != 2)
{
Final = Counter - 1;
JOptionPane.showMessageDialog(this, "Fehler! Sie haben " + Final + "Schritte geschaft.\nStart klicken für neues Spiel","FarbTest",JOptionPane.INFORMATION_MESSAGE);
neu();
}
else
{
UserCounter++;
}
}
else
{
doNext();
}

}

if (e.getSource() == cmdYellow)
{
if (UserCounter < Counter - 1)
{
if (Colors[UserCounter] != 3)
{
Final = Counter - 1;
JOptionPane.showMessageDialog(this, "Fehler! Sie haben " + Final + "Schritte geschaft.\nStart klicken für neues Spiel","FarbTest",JOptionPane.INFORMATION_MESSAGE);
neu();
}
else
{
UserCounter++;
}
}
else
{
doNext();
}

}
}

public static void main(String args[])
{
FarbSpiel FS = new FarbSpiel();
}
}

Kinman
2004-06-20, 22:27:49
super, großes Danke.

Ich beschäftig mich persönlich nicht viel mit Java (mags net). Ist nur für einen Kollegen ;)

THX noch einmal

PH4Real
2004-06-21, 00:30:51
Original geschrieben von ethrandil
hier, ich glaube das ist nicht das schönste (HellHorse???), aber es funktioniert und verdeutlicht das Prinzip.
[...]


Hmm... ich würde die ganzen GUI Update Sachen mittels invokeLater() aufrufen, also in etwa so:


SwingUtilities.invokeLater(new Runnable() {
public void run() {
eastP.setBackground(Color.GREEN);
southP.setBackground(Color.GRAY);
northP.setBackground(Color.GRAY);
westP.setBackground(Color.GRAY);
}
});


siehe dazu auch:
How to Use Threads - The Event-Dispatching Thread (http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html#invokeLater)

HellHorse
2004-06-21, 09:08:26
Da du überhaupt nichts grossartiges rechnest, kannst du das sehr gut im EventDispatch-Thread machen. Bloss einfach das Warten nicht.
Es bietet sicht die Verwendung eines Swing Timers (http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/Timer.html) an.
Siehe auch:
http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html