Results 1 to 13 of 13
Thread: need help with code
- 05-31-2011, 02:10 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
need help with code
Hi,
I've been working on trying to solve this for like 8 hours and no matter what I do I can't get the button to start the game.
I've looked up so many tutorials on using button but the action doesn't seem to be getting passed on. help aprreciated. ThanksJava Code:public class Main extends JFrame implements ActionListener{ JFrame frame; JButton singlePlayer; JButton versus; final Container contain = getContentPane(); public Main() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 300); setLocationRelativeTo(null); setTitle("R - Type"); setResizable(false); setVisible(true); singlePlayer = new JButton("Single Player"); versus = new JButton("Versus"); singlePlayer.setActionCommand("single"); singlePlayer.setEnabled(true); singlePlayer.addActionListener(this); versus.setActionCommand("versus"); versus.setEnabled(true); versus.addActionListener(this); contain.setLayout(new FlowLayout()); contain.add(singlePlayer); contain.add(versus); contain.setBackground(Color.BLACK); } public static void main(String[] args) { new Main(); } public void actionPerformed(ActionEvent e) { if ("single".equals(e.getActionCommand())) { add(new Board()); } } }
-
How about adding some debug statements to see if the actionPerformed is actually being called, something like:
Java Code:public void actionPerformed(ActionEvent e) { System.out.println("in actionPerformed"); if ("single".equals(e.getActionCommand())) { System.out.println("in if single equals block"); add(new Board()); }
- 05-31-2011, 02:48 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
kk i added and it displays that so its getting to it.
this is the display of the eclispe console when i click the button:
in if single equals block
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:155)
at Spacesships.Ship.<init>(Ship.java:17)
at Spacesships.Board.<init>(Board.java:28)
at Spacesships.Main.actionPerformed(Main.java:58)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2012)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2335)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:404)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:253)
at java.awt.Component.processMouseEvent(Component.jav a:6268)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
at java.awt.Component.processEvent(Component.java:603 3)
at java.awt.Container.processEvent(Container.java:204 5)
at java.awt.Component.dispatchEventImpl(Component.jav a:4629)
at java.awt.Container.dispatchEventImpl(Container.jav a:2103)
at java.awt.Component.dispatchEvent(Component.java:44 55)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4633)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4297)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4227)
at java.awt.Container.dispatchEventImpl(Container.jav a:2089)
at java.awt.Window.dispatchEventImpl(Window.java:2517 )
at java.awt.Component.dispatchEvent(Component.java:44 55)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:649)
at java.awt.EventQueue.access$000(EventQueue.java:96)
at java.awt.EventQueue$1.run(EventQueue.java:608)
at java.awt.EventQueue$1.run(EventQueue.java:606)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:105)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:116)
at java.awt.EventQueue$2.run(EventQueue.java:622)
at java.awt.EventQueue$2.run(EventQueue.java:620)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 619)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:138)
-
That probably should have been in your initial post as it's critically important information. Looks like your ship's ImageIcon is null. Are you sure that you're reading in an image file properly? Time to do some more debugging it seems.
- 05-31-2011, 03:11 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
yea sry about it shouldve been the first. Heres my reading in the image part:
And my image is saved into a resources folder thats project level and isn't located in the package. Thanks for the helpJava Code:ImageIcon ii = new ImageIcon(this.getClass().getResource("/resources/craft.png")); image = ii.getImage();
-
It looks like it should be in a subdirectory off of the class files. Should ("/resources/craft.png") have the leading backslash? What if you do instead "(resources/craft.png)"?
- 05-31-2011, 03:57 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
i've tried tons of different paths for the image but that error always shows up. Is there something wrong with using imageIcon, should i use a different method of reading in an image?
-
I usually use the ImageIO class to read in images. Then if I need an ImageIcon, I create one with the BufferedImage obtained.
- 06-02-2011, 03:01 AM #9
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
ok i switched to using buffered image and ImageIO:
I'm getting a can't read input file error. Does this mean I still have the wrong file path or that theirs something wrong with the image?Java Code:try { BufferedImage bi = ImageIO.read(new File("/resources/craft.png")); image = bi; } catch (IOException e) { System.out.println("BufferedImage error:" + e); e.printStackTrace(); }
- 06-08-2011, 11:44 PM #10
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
bumppppppp
-
Make the simplest of programs that does nothing but read in an image, create in ImageIcon with it, place that ImageIcon in a JLabel and display the JLabel in a JOptionPane and let's see what happens.
- 06-09-2011, 04:50 AM #12
How are you running that code? Command line? IDE? Which IDE?
db
- 06-13-2011, 09:12 PM #13
Member
- Join Date
- May 2011
- Posts
- 56
- Rep Power
- 0
Hey did you tried atleast ones by giving manual path to get the image from that file???yea sry about it shouldve been the first. Heres my reading in the image part:
And my image is saved into a resources folder thats project level and isn't located in the package. Thanks for the helpJava Code:ImageIcon ii = new ImageIcon(this.getClass().getResource("/resources/craft.png")); image = ii.getImage();
Also, check the path by taking it as a output on console......
Similar Threads
-
look @ code and give ideas! Install new code....
By turbowhat in forum New To JavaReplies: 2Last Post: 04-27-2011, 03:08 AM -
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
Code to check if a piece of code is legal.
By vahshir in forum New To JavaReplies: 3Last Post: 08-30-2010, 04:21 AM -
can any one pls send me a sample code for calling a jsp code in swings
By sniffer139 in forum AWT / SwingReplies: 1Last Post: 03-04-2010, 11:19 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks