import javax.swing.*;
import java.awt.*;
import java.io.*;
import javax.imageio.*;
public class MohrhuhnPaint extends JComponent
{
private Image bg;
private Image huhn;
private MohrhuhnModel huhnModel;
private MohrhuhnController controller;
private boolean gestartet = false;
private boolean isInterrupted = false;
MohrhuhnPaint()
{
File bild = new File("image/bg.jpg");
File bild2 = new File("image/huhn.gif");
try
{
bg = ImageIO.read(bild);
huhn = ImageIO.read(bild2);
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
public void start(MohrhuhnModel huhnModel)
{
this.huhnModel = huhnModel;
controller = new MohrhuhnController();
controller.setStartWert(huhnModel);
gestartet = true;
repaint();
}
public void run()
{
String richtung = huhnModel.getRichtung();
int x = huhnModel.getX();
int y = huhnModel.getY();
if(richtung.equals("links"))
{
while((x < 510) && (!huhnModel.getGetroffen()))
{
controller.move(huhnModel, x);
x = x + 2;
try
{
Thread.sleep(50);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
repaint();
}
}
else if(richtung.equals("rechts"))
{
while((x > -30) && (!huhnModel.getGetroffen()))
{
controller.move(huhnModel, x);
x = x - 2;
try
{
Thread.sleep(50);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
repaint();
}
}
}
public void paintComponent(Graphics g)
{
g.drawImage(bg, 0, 0, 500, 330, this);
if(gestartet)
{
if(!huhnModel.getGetroffen())
{
g.drawImage(huhn,huhnModel.getX(), huhnModel.getY(), this);
}
}
if((gestartet) && (huhnModel.getGetroffen()))
{
g.setColor(Color.blue);
g.setFont(new Font("Serif",Font.BOLD, 50));
g.drawString("Treffer", 100, 100);
}
}
}
// ########## und ############
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.imageio.*;
import java.io.*;
public class MohrhuhnView extends JFrame implements ActionListener, MouseListener, Runnable
{
private JMenuItem neu;
private JMenuItem schliessen;
private MohrhuhnPaint zeichnen;
MohrhuhnModel huhnModel;
MohrhuhnView()
{
setTitle("Mohrhun");
setPreferredSize(new Dimension(500,375));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar bar = new JMenuBar();
bar.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
setJMenuBar(bar);
JMenu menu = new JMenu("Game");
bar.add(menu);
neu = new JMenuItem("Neues Game");
neu.addActionListener(this);
neu.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
menu.add(neu);
menu.addSeparator();
schliessen = new JMenuItem("Schliessen");
schliessen.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
menu.add(schliessen);
zeichnen = new MohrhuhnPaint();
zeichnen.addMouseListener(this);
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
add(zeichnen);
pack();
setVisible(true);
}
// ActionEvent
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(neu))
{
huhnModel = new MohrhuhnModel();
zeichnen.start(huhnModel);
new Thread(this).start();
}
}
// MouseEvent's
public void mouseClicked(MouseEvent e)
{
System.out.println(true);
int mouseX = e.getX();
int mouseY = e.getY();
int x = huhnModel.getX();
int y = huhnModel.getY();
if((mouseX >= x) && (mouseX <= (x + 25)))
{
if((mouseY >= y) && (mouseY <= (y + 25)))
{
huhnModel.setGetroffen(true);
}
}
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void run()
{
zeichnen.run();
}
}