Results 1 to 4 of 4
Thread: Help with GUI and Eclipse
- 04-29-2011, 11:10 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 1
- Rep Power
- 0
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
Class MoorhuhnSpielJava 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; } }
Moorhuhn Image: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());} } } } }
(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.)
- 04-29-2011, 03:11 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
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.
- 04-29-2011, 04:22 PM #3
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):
(Edit: I forgot to say "welcome to this forum", so forgive me being rude.)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 ); }Last edited by Jodokus; 04-29-2011 at 05:06 PM.
- 04-30-2011, 01:24 PM #4
See this thread. I saw it today explaining the ConcurrentModificationException:
[HELP] Removing objects from an ArrayList!
Similar Threads
-
Eclipse Plugin Development: How to register SVN actions inside an Eclipse plugin
By olzibambolzi in forum EclipseReplies: 4Last Post: 12-21-2010, 03:26 PM -
What is Eclipse?
By tyang in forum New To JavaReplies: 5Last Post: 01-31-2010, 09:43 PM -
Is it possible to run eclipse plugins without using eclipse?
By ambar in forum EclipseReplies: 2Last Post: 01-27-2009, 02:10 PM -
Eclipse Bug - Can't Read From A File Using Eclipse?
By carlodelmundo in forum New To JavaReplies: 6Last Post: 01-26-2009, 04:25 PM -
Need Help in IDE Eclipse
By jazz2k8 in forum New To JavaReplies: 2Last Post: 07-21-2008, 04:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks