Results 1 to 4 of 4
  1. #1
    kingjin is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Question Help with GUI and Eclipse

    First of all Hi, I'm new to this forum and got some problems programming a small game (like the german "Moorhuhn"). For this I use Eclipse, but I got a problem painting the Moorhuhn image in the frame.

    Sourcecodes:

    Class Moorhuhn

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Moorhuhn {
    	private int x = 0;
    	private int y = (int) (Math.random()*800);
    	private int n = 0;
    	private boolean getroffen = false;
    	
    	public void fliegen(Moorhuhn moorhuhn)  {
    		if(getroffen)  {moorhuhn.absturz(moorhuhn);}
    		else {moorhuhn.normalFliegen(moorhuhn);}
    	}
    	
    	private void normalFliegen(Moorhuhn moorhuhn) {
    		moorhuhn.setX(n);
    		n++;
    	}
    
    	private void absturz(Moorhuhn moorhuhn) {
    		moorhuhn.setY(n);
    		n++;
    	}
    	
    	public boolean outOfRange(Moorhuhn moorhuhn) {
    		if(moorhuhn.getX() > 900 || moorhuhn.getX() < -77 || moorhuhn.getY() > 700 || moorhuhn.getY() < -49)  {
    			return true;
    		}	
    		return false;
    	}
    	
    	public void zeichneDich (JFrame frame)  {
    		MoorhuhnPanel moorhuhnPanel = new MoorhuhnPanel();
    		frame.getContentPane().add(BorderLayout.CENTER, moorhuhnPanel);
    		frame.repaint();
    	}
    	
    	class MoorhuhnPanel extends JPanel  {
    		/**
    		 * 
    		 */
    		private static final long serialVersionUID = 1L;
    
    		public void paintComponent (Graphics g)  {
    			Image image = new ImageIcon("mohrhuhn2.gif").getImage();
    			g.drawImage(image,x,y,this);
    		}
    	}
    
    
    
    
    
    
    
    	public int getX() {
    		return x;
    	}
    	public void setX(int x) {
    		this.x = x;
    	}
    	public int getY() {
    		return y;
    	}
    	public void setY(int y) {
    		this.y = y;
    	}
    	public int getN() {
    		return n;
    	}
    	public void setN(int n) {
    		this.n = n;
    	}
    	public boolean isGetroffen() {
    		return getroffen;
    	}
    	public void setGetroffen(boolean getroffen) {
    		this.getroffen = getroffen;
    	}
    	
    }
    Class MoorhuhnSpiel

    Java Code:
    package version2;
    
    import java.awt.BorderLayout;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.ArrayList;
    import java.util.Iterator;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class MoorhuhnSpiel {
    	JFrame frame;
    	JLabel label;
    	ArrayList<Moorhuhn> moorhuhnListe = new ArrayList<Moorhuhn>();
    	
    	public static void main (String[] args)  { 
    		MoorhuhnSpiel Spiel = new MoorhuhnSpiel();
    		Spiel.los();
    	}
    
    	private void los() {
    		frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(900,900);
    		frame.addMouseListener(new MouseKlick());
    		label = new JLabel("Triff das Moorhuhn");
    		frame.getContentPane().add(BorderLayout.SOUTH, label);
    		frame.setVisible(true);	
    		this .spiel();
    	}
    	
    	
    	
    	private void spiel() {
    		while (true) {
    			while (moorhuhnListe.size()<10){
    				moorhuhnListe.add(new Moorhuhn());
    			}
    		
    			for (Iterator<Moorhuhn> iterator = moorhuhnListe.iterator(); iterator.hasNext();) {
    				Moorhuhn moorhuhn = iterator.next();
    				moorhuhn.fliegen(moorhuhn);
    				moorhuhn.zeichneDich(frame);
    				if (moorhuhn.outOfRange(moorhuhn))  {
    					moorhuhnListe.remove(moorhuhn);
    				}
    			}
    			try{ Thread.sleep(50);} catch (Exception e) {}
    		}	
    	}
    
    
    
    	public 
    	
    	class MouseKlick implements MouseListener {
    
    		public void mouseClicked(MouseEvent e) {}
    
    		public void mouseEntered(MouseEvent e) {}
    
    		public void mouseExited(MouseEvent e) {}
    
    		public void mousePressed(MouseEvent e) {}
    
    		public void mouseReleased(MouseEvent e) {
    			for (Iterator<Moorhuhn> iterator = moorhuhnListe.iterator(); iterator.hasNext();) {
    				Moorhuhn moorhuhn = (Moorhuhn) iterator.next();
    				if (e.getX()<moorhuhn.getX()||e.getX()>moorhuhn.getX()+77||e.getY()<moorhuhn.getY()||e.getY()>moorhuhn.getY()+49)  {
    					return;
    				} else {moorhuhn.setGetroffen(true); moorhuhn.setN(moorhuhn.getY());}
    			}
    		}
    
    	}
    
    }
    Moorhuhn Image: (The Moorhuhn image)

    If any one can help me feel free to comment.

    P.S The windown shown when run as application:
    (What it now looks like.)

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    There is no need to create a custom component to display an image. You can just use a JLabel.

    How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    If you ever do need to use custom painting you should never read the image in the paintComponent() method.

  3. #3
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    I tried your app, but it has a lot of strange problems that I tried to fix first:

    Your class Moorhuhn has a series of private and public methods that have a Mohrhuhn-parameter. That is very strangly contocted. You say things like “Let the huhn fly by letting this same huhn fly”.
    You should remove this “double referencing”: make methods that do something on the instance you are using without providing the same instance in the parameters of the method.

    Your Moorhuhns are put in a list by MoorhuhnSpiel. Each makes its own Jpanel with its own paintComponent. You probably want one Jpanel with a series of Huhns drawn in it. (Or use JLabels like camickr suggested)

    I get a java.util.ConcurrentModificationException. The reason probably is that you try to remove an item from the list within a loop iterating over it.

    Also I have the impression (but might be wrong) that you assembled a lot of coding without compiling in small steps in the process. I think that it didn’t compile or work for the last several iterations (of adding code, compile and check the result).

    If you have trouble locating the image you can use a small diagnostic librarymethod like the next to find out where the app is looking for it
    (remove the "static" if using it from the class itself):
    Java Code:
    	public static void testFile( String filePathAndName){
    		File file =new File( filePathAndName);
    		String path = file.getAbsolutePath();
    		System.out.println( "File: " + filePathAndName + "\n" + "AbsPath: " + path );
    	}
    (Edit: I forgot to say "welcome to this forum", so forgive me being rude.)
    Last edited by Jodokus; 04-29-2011 at 06:06 PM.

  4. #4
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

Similar Threads

  1. Replies: 4
    Last Post: 12-21-2010, 04:26 PM
  2. What is Eclipse?
    By tyang in forum New To Java
    Replies: 5
    Last Post: 01-31-2010, 10:43 PM
  3. Replies: 2
    Last Post: 01-27-2009, 03:10 PM
  4. Eclipse Bug - Can't Read From A File Using Eclipse?
    By carlodelmundo in forum New To Java
    Replies: 6
    Last Post: 01-26-2009, 05:25 PM
  5. Need Help in IDE Eclipse
    By jazz2k8 in forum New To Java
    Replies: 2
    Last Post: 07-21-2008, 05:52 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •