Moin Moin
kann mir vielleicht jemanden helfen ich bin Anfänger .ich bin dabei ein Programm für das Ozean mit Fischen zu programmieren . bei drück den button Start sollte die Fische sich bewegen. ich habe folgende Problem bei Start ,die Fischen bewegen sich aber die andere Button wie stop ,delete funktioniert nicht mehr und ich weiß nicht wo den Fehler liegt . danke im voraus
hier ist mein Code :
package infpp.oceanlife;
//import infpp.oceanlife.OceanlifeView.MoveThread;
//import infpp.oceanlife.GuiOceanlife.PaintThread;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.ibjectInputStream;
import java.ibjectOutputStream;
import java.iutputStream;
import java.util.LinkedList;
import javax.swing.JOptionPane;
public class OceanlifeController
{
private OceanlifeView view ;
private Ocean ocean;
//private OceanModel_Model;
public OceanlifeController(OceanlifeView view,Ocean ocean)
{
this.view = view;
this.ocean= ocean;
addListener();
addKeyListener();
}
private void addListener(){
this.view.setQuittButtonListener(new ActionListenerEx());
this.view.setInsertButtonListener(new ActionListenerInsert());
this.view.setDeleteButtonListener( new ActionListenerDelete());
this.view.setSaveButtonListener(new ActionListenerSave());
this.view.setLoadButtonListener(new ActionListenerLoad());
this.view.setStepButtonListener(new ActionListenerStep());
this.view.setGoButtonListener(new ActionListenerGo());
this.view.setStopButtonListener(new ActionListenerStop());
}
private void addKeyListener(){
this.view.setxValueKeyListener(new TextKeyListener());
this.view.setyValueKeyListener( new TextKeyListener());
}
/*
* thread to paint while running
*/
class PaintThread extends Thread {
@Override
public void run() {
while (view.running) {
synchronized (ocean.getOceanObjects()) {
try {
Thread.sleep(25);
} catch (Exception e) {
e.printStackTrace();
}
view.repaint();
}
}
}
}
/*
* thread to make the motion of the objects
*/
class MoveThread extends Thread {
public OceanObject object;
MoveThread(OceanObject object) {
this.object = object;
}
@Override
public void run() {
while (view.running) {
try {
Thread.sleep(25);
} catch (Exception e) {
e.printStackTrace();
}
if (object.checkClass().equals("Fish")) {
try {
// checking which direction the fish is headed
if (object.getDirectionX() == 2) {
object.move(object, view.imgFishRight, ocean.getDepth(),
ocean.getWidth());
} else {
object.move(object, view.imgFishLeft, ocean.getDepth(),
ocean.getWidth());
}
} catch (Exception e) {
}
} else if (object.checkClass().equals("Stone")) {
try {
object.move(object, view.imgStone, ocean.getDepth(),
ocean.getWidth());
} catch (Exception e) {
}
} else if (object.checkClass().equals("Plant")) {
try {
object.move(object, view.imgPlant, ocean.getDepth(),
ocean.getWidth());
} catch (Exception e) {
}
} else if (object.checkClass().equals("Bubble")) {
try {
object.move(object, view.imgBubble, ocean.getDepth(),
ocean.getWidth());
} catch (Exception e) {
// deleting the bubble if it reached the top
ocean.oceanObjects.remove(object);
view.deleteList.removeItem(object);
}
}
}
}
}
/*
* actionlistener for the goButton
*/
class ActionListenerGo implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (!view.running) {
view.running = true;
// starting the threads for each object
for (int i = 0; i < ocean.getOceanObjects().size(); i++) {
OceanObject object = ocean.getOceanObjects().get(i);
MoveThread move = new MoveThread(object);
move.start();
}
// starting one thread to paint the objects
PaintThread paint = new PaintThread();
paint.start();
}
}
}
/*
* actionlistener for the stopButton
*/
class ActionListenerStop implements ActionListener {
public void actionPerformed(ActionEvent event) {
view.running = false;
}
}
/*
* actionlistener for the stepButton
*/
class ActionListenerStep implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (!view.running) {
for (int i = 0; i < ocean.getOceanObjects().size(); i++) {
OceanObject object = ocean.getOceanObjects().get(i);
// checking the type of the current object
if (object.checkClass().equals("Fish")) {
Fish ob = (Fish) object;
// checking direction of object
if (object.getDirectionX() == 2) {
ob.move(ob,view.imgFishRight, ocean.getDepth(),
ocean.getWidth());
} else {
ob.move(ob,view. imgFishLeft, ocean.getDepth(),
ocean.getWidth());
}
} else if (object.checkClass().equals("Plant")) {
Plant ob = (Plant) object;
ob.move(ob, view.imgPlant, ocean.getDepth(),
ocean.getWidth());
} else if (object.checkClass().equals("Stone")) {
Stone ob = (Stone) object;
ob.move(ob, view.imgStone, ocean.getDepth(),
ocean.getWidth());
} else if (object.checkClass().equals("Bubble")) {
Bubble ob = (Bubble) object;
try {
ob.move(ob, view.imgBubble, ocean.getDepth(),
ocean.getWidth());
} catch (Exception e) {
// deleting the object
ocean.oceanObjects.remove(object);
view.deleteList.removeItem(object);
}
}
view.repaint();
}
}
}
}
/*
* keylistener to prevent users from typing others characters than letters
*/
class TextKeyListener extends KeyAdapter {
@Override
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (!view.running) {
if (!((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)))) {
e.consume();
}
} else {
e.consume();
}
}
}
/*
* actionlistener for the loadButton
*/
class ActionListenerLoad implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (!view.running) {
try {
// deleting the old ocean
ocean.oceanObjects.clear();
view.deleteList.removeAllItems();
view.repaint();
// making new inputstream
FileInputStream fis = new FileInputStream("save.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
// setting new list of objects
@SuppressWarnings("unchecked")
LinkedList<OceanObject> list = (LinkedList<OceanObject>) ois
.readObject();
ocean.setOceanObjects(list);
for (int i = 0; i < list.size(); i++) {
view.deleteList.addItem(list.get(i));
}
ois.close();
view.repaint();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,
"The ocean could not be loaded!", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
/*
* actionlistener for the saveButton
*/
class ActionListenerSave implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (!view.running) {
try {
OutputStream os = new FileOutputStream("save.txt");
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(ocean.getOceanObjects());
oos.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null,
"The ocean could not be saved.", "Warning",
JOptionPane.WARNING_MESSAGE);
}
}
}
}
/*
* actionlistener for the exitButton
*/
class ActionListenerEx implements ActionListener {
public void actionPerformed(ActionEvent e) {
while (!view.running) {
System.exit(0);
}
}
}
/*
* actionlistener for the deleteButton
*/
class ActionListenerDelete implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (!view.running) {
try {
// deleting the selected object
ocean.deleteOceanObject(view.deleteList.getSelectedIndex());
view.deleteList.removeItem(view.deleteList.getSelectedItem());
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "No Objects existing!",
"Error", JOptionPane.ERROR_MESSAGE);
}
view.repaint();
view.xValue.setText("");
view.yValue.setText("");
}
}
}
/*
* actionlistener for the insertButton
*/
class ActionListenerInsert implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (!view.running) {
try {
// parsing the coordinates
int xvalue = Integer.parseInt(view.xValue.getText());
int yvalue = Integer.parseInt(view.yValue.getText());
// parsing the selected object
Object listObject = view.insertList.getSelectedItem();
String nameOfObject = (String) listObject;
// checking which objects has to be created
if (nameOfObject.equals("Fish")) {
try {
// check position of object and add to the list
Fish object = new Fish(xvalue, yvalue);
// setting direction of the fish
object.setDirectionX((int) (Math.random() * 2 + 1));
object.setDirectionY((int) (Math.random() * 2 + 1));
ocean.checkCollision(object,
ocean.getOceanObjects(), view.imgFishRight);
ocean.addOceanObject(object,view.imgFishRight);
view.deleteList.addItem(object);
} catch (Exception e) {
JOptionPane
.showMessageDialog(
null,
"Object is either outside the ocean or its blocked by an already existing object!",
"Warning",
JOptionPane.WARNING_MESSAGE);
}
} else if (nameOfObject.equals("Plant")) {
try {
// check position of object and add to the list
Plant object = new Plant(xvalue, yvalue);
ocean.checkCollision(object,
ocean.getOceanObjects(),view.imgPlant);
ocean.addOceanObject(object, view.imgPlant);
view.deleteList.addItem(object);
} catch (Exception e) {
JOptionPane
.showMessageDialog(
null,
"Object is either outside the ocean or its blocked by an already existing object!",
"Warning",
JOptionPane.WARNING_MESSAGE);
}
} else if (nameOfObject.equals("Stone")) {
try {
// check position of object and add to the list
Stone object = new Stone(xvalue, yvalue);
ocean.checkCollision(object,
ocean.getOceanObjects(),view.imgStone);
ocean.addOceanObject(object, view.imgStone);
view.deleteList.addItem(object);
} catch (Exception e) {
JOptionPane
.showMessageDialog(
null,
"Object is either outside the ocean or its blocked by an already existing object!",
"Warning",
JOptionPane.WARNING_MESSAGE);
}
} else {
try {
// check position of object and add to the list
Bubble object = new Bubble(xvalue, yvalue);
ocean.checkCollision(object,
ocean.getOceanObjects(), view.imgBubble);
ocean.addOceanObject(object, view.imgBubble);
view.deleteList.addItem(object);
} catch (Exception e) {
JOptionPane
.showMessageDialog(
null,
"Object is either outside the ocean or its blocked by an already existing object!",
"Warning",
JOptionPane.WARNING_MESSAGE);
}
}
view.xValue.setText("");
view.yValue.setText("");
view.repaint();
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,
"Plese insert Integer as values.", "Warning",
JOptionPane.WARNING_MESSAGE);
}
}
}
}
}
kann mir vielleicht jemanden helfen ich bin Anfänger .ich bin dabei ein Programm für das Ozean mit Fischen zu programmieren . bei drück den button Start sollte die Fische sich bewegen. ich habe folgende Problem bei Start ,die Fischen bewegen sich aber die andere Button wie stop ,delete funktioniert nicht mehr und ich weiß nicht wo den Fehler liegt . danke im voraus
hier ist mein Code :
package infpp.oceanlife;
//import infpp.oceanlife.OceanlifeView.MoveThread;
//import infpp.oceanlife.GuiOceanlife.PaintThread;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.ibjectInputStream;
import java.ibjectOutputStream;
import java.iutputStream;
import java.util.LinkedList;
import javax.swing.JOptionPane;
public class OceanlifeController
{
private OceanlifeView view ;
private Ocean ocean;
//private OceanModel_Model;
public OceanlifeController(OceanlifeView view,Ocean ocean)
{
this.view = view;
this.ocean= ocean;
addListener();
addKeyListener();
}
private void addListener(){
this.view.setQuittButtonListener(new ActionListenerEx());
this.view.setInsertButtonListener(new ActionListenerInsert());
this.view.setDeleteButtonListener( new ActionListenerDelete());
this.view.setSaveButtonListener(new ActionListenerSave());
this.view.setLoadButtonListener(new ActionListenerLoad());
this.view.setStepButtonListener(new ActionListenerStep());
this.view.setGoButtonListener(new ActionListenerGo());
this.view.setStopButtonListener(new ActionListenerStop());
}
private void addKeyListener(){
this.view.setxValueKeyListener(new TextKeyListener());
this.view.setyValueKeyListener( new TextKeyListener());
}
/*
* thread to paint while running
*/
class PaintThread extends Thread {
@Override
public void run() {
while (view.running) {
synchronized (ocean.getOceanObjects()) {
try {
Thread.sleep(25);
} catch (Exception e) {
e.printStackTrace();
}
view.repaint();
}
}
}
}
/*
* thread to make the motion of the objects
*/
class MoveThread extends Thread {
public OceanObject object;
MoveThread(OceanObject object) {
this.object = object;
}
@Override
public void run() {
while (view.running) {
try {
Thread.sleep(25);
} catch (Exception e) {
e.printStackTrace();
}
if (object.checkClass().equals("Fish")) {
try {
// checking which direction the fish is headed
if (object.getDirectionX() == 2) {
object.move(object, view.imgFishRight, ocean.getDepth(),
ocean.getWidth());
} else {
object.move(object, view.imgFishLeft, ocean.getDepth(),
ocean.getWidth());
}
} catch (Exception e) {
}
} else if (object.checkClass().equals("Stone")) {
try {
object.move(object, view.imgStone, ocean.getDepth(),
ocean.getWidth());
} catch (Exception e) {
}
} else if (object.checkClass().equals("Plant")) {
try {
object.move(object, view.imgPlant, ocean.getDepth(),
ocean.getWidth());
} catch (Exception e) {
}
} else if (object.checkClass().equals("Bubble")) {
try {
object.move(object, view.imgBubble, ocean.getDepth(),
ocean.getWidth());
} catch (Exception e) {
// deleting the bubble if it reached the top
ocean.oceanObjects.remove(object);
view.deleteList.removeItem(object);
}
}
}
}
}
/*
* actionlistener for the goButton
*/
class ActionListenerGo implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (!view.running) {
view.running = true;
// starting the threads for each object
for (int i = 0; i < ocean.getOceanObjects().size(); i++) {
OceanObject object = ocean.getOceanObjects().get(i);
MoveThread move = new MoveThread(object);
move.start();
}
// starting one thread to paint the objects
PaintThread paint = new PaintThread();
paint.start();
}
}
}
/*
* actionlistener for the stopButton
*/
class ActionListenerStop implements ActionListener {
public void actionPerformed(ActionEvent event) {
view.running = false;
}
}
/*
* actionlistener for the stepButton
*/
class ActionListenerStep implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (!view.running) {
for (int i = 0; i < ocean.getOceanObjects().size(); i++) {
OceanObject object = ocean.getOceanObjects().get(i);
// checking the type of the current object
if (object.checkClass().equals("Fish")) {
Fish ob = (Fish) object;
// checking direction of object
if (object.getDirectionX() == 2) {
ob.move(ob,view.imgFishRight, ocean.getDepth(),
ocean.getWidth());
} else {
ob.move(ob,view. imgFishLeft, ocean.getDepth(),
ocean.getWidth());
}
} else if (object.checkClass().equals("Plant")) {
Plant ob = (Plant) object;
ob.move(ob, view.imgPlant, ocean.getDepth(),
ocean.getWidth());
} else if (object.checkClass().equals("Stone")) {
Stone ob = (Stone) object;
ob.move(ob, view.imgStone, ocean.getDepth(),
ocean.getWidth());
} else if (object.checkClass().equals("Bubble")) {
Bubble ob = (Bubble) object;
try {
ob.move(ob, view.imgBubble, ocean.getDepth(),
ocean.getWidth());
} catch (Exception e) {
// deleting the object
ocean.oceanObjects.remove(object);
view.deleteList.removeItem(object);
}
}
view.repaint();
}
}
}
}
/*
* keylistener to prevent users from typing others characters than letters
*/
class TextKeyListener extends KeyAdapter {
@Override
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (!view.running) {
if (!((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)))) {
e.consume();
}
} else {
e.consume();
}
}
}
/*
* actionlistener for the loadButton
*/
class ActionListenerLoad implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (!view.running) {
try {
// deleting the old ocean
ocean.oceanObjects.clear();
view.deleteList.removeAllItems();
view.repaint();
// making new inputstream
FileInputStream fis = new FileInputStream("save.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
// setting new list of objects
@SuppressWarnings("unchecked")
LinkedList<OceanObject> list = (LinkedList<OceanObject>) ois
.readObject();
ocean.setOceanObjects(list);
for (int i = 0; i < list.size(); i++) {
view.deleteList.addItem(list.get(i));
}
ois.close();
view.repaint();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,
"The ocean could not be loaded!", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
/*
* actionlistener for the saveButton
*/
class ActionListenerSave implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (!view.running) {
try {
OutputStream os = new FileOutputStream("save.txt");
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(ocean.getOceanObjects());
oos.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null,
"The ocean could not be saved.", "Warning",
JOptionPane.WARNING_MESSAGE);
}
}
}
}
/*
* actionlistener for the exitButton
*/
class ActionListenerEx implements ActionListener {
public void actionPerformed(ActionEvent e) {
while (!view.running) {
System.exit(0);
}
}
}
/*
* actionlistener for the deleteButton
*/
class ActionListenerDelete implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (!view.running) {
try {
// deleting the selected object
ocean.deleteOceanObject(view.deleteList.getSelectedIndex());
view.deleteList.removeItem(view.deleteList.getSelectedItem());
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "No Objects existing!",
"Error", JOptionPane.ERROR_MESSAGE);
}
view.repaint();
view.xValue.setText("");
view.yValue.setText("");
}
}
}
/*
* actionlistener for the insertButton
*/
class ActionListenerInsert implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (!view.running) {
try {
// parsing the coordinates
int xvalue = Integer.parseInt(view.xValue.getText());
int yvalue = Integer.parseInt(view.yValue.getText());
// parsing the selected object
Object listObject = view.insertList.getSelectedItem();
String nameOfObject = (String) listObject;
// checking which objects has to be created
if (nameOfObject.equals("Fish")) {
try {
// check position of object and add to the list
Fish object = new Fish(xvalue, yvalue);
// setting direction of the fish
object.setDirectionX((int) (Math.random() * 2 + 1));
object.setDirectionY((int) (Math.random() * 2 + 1));
ocean.checkCollision(object,
ocean.getOceanObjects(), view.imgFishRight);
ocean.addOceanObject(object,view.imgFishRight);
view.deleteList.addItem(object);
} catch (Exception e) {
JOptionPane
.showMessageDialog(
null,
"Object is either outside the ocean or its blocked by an already existing object!",
"Warning",
JOptionPane.WARNING_MESSAGE);
}
} else if (nameOfObject.equals("Plant")) {
try {
// check position of object and add to the list
Plant object = new Plant(xvalue, yvalue);
ocean.checkCollision(object,
ocean.getOceanObjects(),view.imgPlant);
ocean.addOceanObject(object, view.imgPlant);
view.deleteList.addItem(object);
} catch (Exception e) {
JOptionPane
.showMessageDialog(
null,
"Object is either outside the ocean or its blocked by an already existing object!",
"Warning",
JOptionPane.WARNING_MESSAGE);
}
} else if (nameOfObject.equals("Stone")) {
try {
// check position of object and add to the list
Stone object = new Stone(xvalue, yvalue);
ocean.checkCollision(object,
ocean.getOceanObjects(),view.imgStone);
ocean.addOceanObject(object, view.imgStone);
view.deleteList.addItem(object);
} catch (Exception e) {
JOptionPane
.showMessageDialog(
null,
"Object is either outside the ocean or its blocked by an already existing object!",
"Warning",
JOptionPane.WARNING_MESSAGE);
}
} else {
try {
// check position of object and add to the list
Bubble object = new Bubble(xvalue, yvalue);
ocean.checkCollision(object,
ocean.getOceanObjects(), view.imgBubble);
ocean.addOceanObject(object, view.imgBubble);
view.deleteList.addItem(object);
} catch (Exception e) {
JOptionPane
.showMessageDialog(
null,
"Object is either outside the ocean or its blocked by an already existing object!",
"Warning",
JOptionPane.WARNING_MESSAGE);
}
}
view.xValue.setText("");
view.yValue.setText("");
view.repaint();
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,
"Plese insert Integer as values.", "Warning",
JOptionPane.WARNING_MESSAGE);
}
}
}
}
}
Zuletzt bearbeitet: