Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: Awww Errors!!!

  1. #1
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default Awww Errors!!!

    I'm trying to make my program where it displays 3 characters, you select one of them, then it clears the whole JFrame. I'm not sure what's wrong... Here's my coding.

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.ImageIcon;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;


    public class KaratePaint extends Applet implements ActionListener
    {
    JButton terminator = new JButton(new ImageIcon("KarateTerminator.jpg"));
    JButton mrT = new JButton(new ImageIcon("KarateMrT.jpg"));
    JButton hulkHogan = new JButton(new ImageIcon("KarateHulkHogan.jpg"));

    JLabel you = new JLabel("Select Your Character");
    JLabel blank = new JLabel(" ");
    JLabel blank2 = new JLabel(" ");
    JPanel panel = new JPanel();

    public void paint()
    {
    JFrame frame = new JFrame();

    JButton terminator = new JButton(new ImageIcon("KarateTerminator.jpg"));
    JButton mrT = new JButton(new ImageIcon("KarateMrT.jpg"));
    JButton hulkHogan = new JButton(new ImageIcon("KarateHulkHogan.jpg"));

    JLabel you = new JLabel("Select Your Character");
    JLabel blank = new JLabel(" ");
    JLabel blank2 = new JLabel(" ");
    JPanel panel = new JPanel();

    panel.add(you.CENTER_ALIGNMENT);
    panel.add(blank2);
    panel.add(terminator);
    panel.add(mrT);
    panel.add(hulkHogan);
    frame.add(panel);

    terminator.addActionListener(this);

    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource() == terminator)
    {
    frame.repaint();
    }
    terminator.doClick();

    ActionListener listener = new paint();
    terminator.addActionListener(listener);
    }

    private static final int FRAME_WIDTH = 500;
    private static final int FRAME_HEIGHT = 500;
    }





    public class KarateTester {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    KaratePaint paint = new KaratePaint();
    paint.paint();
    }

    }

  2. #2
    Xyle's Avatar
    Xyle is offline Member
    Join Date
    Feb 2009
    Location
    Nevada
    Posts
    34
    Rep Power
    0

    Default

    Might help a bit if you use "["code"]" your code here... "["/code"]" blocks (without quote marks) with good indenting technique. Makes the code look cleaner and a bit easier for newbs like me to read it. :)

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.ImageIcon;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class KaratePaint extends Applet implements ActionListener{
    	JButton terminator = new JButton(new ImageIcon("KarateTerminator.jpg"));
    	JButton mrT = new JButton(new ImageIcon("KarateMrT.jpg"));
    	JButton hulkHogan = new JButton(new ImageIcon("KarateHulkHogan.jpg"));
    	
    	JLabel you = new JLabel("Select Your Character");
    	JLabel blank = new JLabel(" ");
    	JLabel blank2 = new JLabel(" ");
    	JPanel panel = new JPanel();
    
    	public void paint(){
    		JFrame frame = new JFrame();
    		
    		JButton terminator = new JButton(new ImageIcon("KarateTerminator.jpg"));
    		JButton mrT = new JButton(new ImageIcon("KarateMrT.jpg"));
    		JButton hulkHogan = new JButton(new ImageIcon("KarateHulkHogan.jpg"));
    		
    		JLabel you = new JLabel("Select Your Character");
    		JLabel blank = new JLabel(" ");
    		JLabel blank2 = new JLabel(" ");
    		JPanel panel = new JPanel();
    		
    		panel.add(you.CENTER_ALIGNMENT);
    		panel.add(blank2);
    		panel.add(terminator);
    		panel.add(mrT);
    		panel.add(hulkHogan);
    		frame.add(panel);
    		
    		terminator.addActionListener(this);
    		
    		frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    		frame.setVisible(true);
    	}
    	
    	public void actionPerformed(ActionEvent e){
    		if(e.getSource() == terminator){
    			frame.repaint();
    		}
    		
    		terminator.doClick();
    		
    		ActionListener listener = new paint();
    		terminator.addActionListener(listener);
    	}
    	
    	private static final int FRAME_WIDTH = 500;
    	private static final int FRAME_HEIGHT = 500;
    }
    
    
    
    
    
    public class KarateTester {
    	/**
    	* @param args the command line arguments
    	*/
    	public static void main(String[] args){
    		KaratePaint paint = new KaratePaint();
    		paint.paint();
    	}
    }
    Last edited by Xyle; 03-25-2009 at 02:33 AM.

  3. #3
    Xyle's Avatar
    Xyle is offline Member
    Join Date
    Feb 2009
    Location
    Nevada
    Posts
    34
    Rep Power
    0

    Default

    Other than that, read each error carefully. They usually pinpoint the exact spot where some thing is wrong.

    1 thing right off the bat, your trying to repaint frame from actionevent when the frame variable is not available to actionevent, its declared only in the paint method so can only be used by the paint method, public or not. Declare it before the paint method in your global variable declarations.

    ActionListener listener = new paint();
    Im not sure what your trying to do here. Instantiate a paint object that you havent created? create an actionlistener object? Might want to read up on a few things here.

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    Typo here!
    Last edited by Xyle; 03-25-2009 at 02:55 AM.

  4. #4
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.ImageIcon;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class KaratePaint extends Applet implements ActionListener
    {
        JButton terminator = new JButton(new ImageIcon("KarateTerminator.jpg"));
        JButton mrT = new JButton(new ImageIcon("KarateMrT.jpg"));
        JButton hulkHogan = new JButton(new ImageIcon("KarateHulkHogan.jpg"));
    
        JLabel you = new JLabel("Select Your Character");
        JLabel blank = new JLabel("                             ");
        JLabel blank2 = new JLabel("                             ");
        JPanel panel = new JPanel();
    
        public void paint()
        {
        JFrame frame = new JFrame();
    
            JButton terminator = new JButton(new ImageIcon("KarateTerminator.jpg"));
            JButton mrT = new JButton(new ImageIcon("KarateMrT.jpg"));
            JButton hulkHogan = new JButton(new ImageIcon("KarateHulkHogan.jpg"));
    
            JLabel you = new JLabel("Select Your Character");
            JLabel blank = new JLabel("                             ");
            JLabel blank2 = new JLabel("                             ");
            JPanel panel = new JPanel();
    
            panel.add(you.CENTER_ALIGNMENT);
            panel.add(blank2);
            panel.add(terminator);
            panel.add(mrT);
            panel.add(hulkHogan);
            frame.add(panel);
    
            terminator.addActionListener(this);
    
            frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() == terminator)
            {
                frame.repaint();          //problem here
            }
            terminator.doClick();
    
            ActionListener listener = new paint();       //problem here
            terminator.addActionListener(listener);
        }
    
        private static final int FRAME_WIDTH = 500;
        private static final int FRAME_HEIGHT = 500;
    }
    
    
    
    public class KarateTester {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            KaratePaint paint = new KaratePaint();
            paint.paint();
        }
    
    }

    I'm sure that there's more errors for me to get this working, but that's all that's showing up for now.

  5. #5
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    Ok, I fixed all of the errors, except now when the JFrame pops up and I pick the terminator, it just freezes. Also, should I add anything to the tester to make the repaint work?
    Last edited by AWPtic; 03-25-2009 at 03:12 AM.

  6. #6
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    Here's my new corrected code that displays the JFrame and everything, but then freezes when I click on the terminator picture.

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.ImageIcon;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class KaratePaint extends Applet implements ActionListener
    {
        JButton terminator = new JButton(new ImageIcon("KarateTerminator.jpg"));
        JButton mrT = new JButton(new ImageIcon("KarateMrT.jpg"));
        JButton hulkHogan = new JButton(new ImageIcon("KarateHulkHogan.jpg"));
    
        JLabel you = new JLabel("Select Your Character");
        JLabel blank = new JLabel("                             ");
        JLabel blank2 = new JLabel("                             ");
        JPanel panel = new JPanel();
    
        JFrame frame = new JFrame();
    
        public void paint()
        {
    
            JButton terminator = new JButton(new ImageIcon("KarateTerminator.jpg"));
            JButton mrT = new JButton(new ImageIcon("KarateMrT.jpg"));
            JButton hulkHogan = new JButton(new ImageIcon("KarateHulkHogan.jpg"));
    
            JLabel you = new JLabel("Select Your Character");
            JLabel blank = new JLabel("                             ");
            JLabel blank2 = new JLabel("                             ");
            JPanel panel = new JPanel();
    
            panel.add(blank);
            panel.add(you);
            panel.add(blank2);
            panel.add(terminator);
            panel.add(mrT);
            panel.add(hulkHogan);
            frame.add(panel);
    
            terminator.addActionListener(this);
    
            frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() == terminator)
            {
                frame.repaint();
            }
            terminator.doClick();
    
            ActionListener listener = new KaratePaint();
            terminator.addActionListener(listener);
        }
    
        private static final int FRAME_WIDTH = 500;
        private static final int FRAME_HEIGHT = 500;
    }

  7. #7
    Xyle's Avatar
    Xyle is offline Member
    Join Date
    Feb 2009
    Location
    Nevada
    Posts
    34
    Rep Power
    0

    Default

    ActionListener listener = new KaratePaint();
    terminator.addActionListener(listener);

    I still think there are some problems with what your trying to achieve here, what your saying is, when user clicks terminator button launch a new instance of KaratePaint() but retype it as a listener and attach this program/listener instance to the existing terminator button. I dont really think this is doable. :)

    On the terminator button click, make a hidden button visible or display some text on a button. Just to get your code working. Keep it as simple as possible, get it working, then add new or better features.

    Write it down on paper what your trying to achieve, draw arrows pointing to changes in your program, type lists of what your trying to do, etc. Think your program through before you try coding it.

  8. #8
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    I don't know... All I'm trying to do with the Action Listener is make it where when I click on the button that it makes all of the components in the JFrame disappear. Also, the button is an image.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You appear to be defining your terminator button twice, once in the class -- which is never displayed, and once in the paint method -- which is displayed. You understand of course that these are two completely different objects. Then your actionPerformed method attempts to test whether the button pushed is the one defined in the class -- which again is never displayed, so this will never work. This is called shadowing an object and is a fairly common type of error. I strongly suggest that you don't do this, that you define the button once in the class and use it in the paint method but don't redefine it there. Also, why is your class extending Applet? This makes little sense.

    Also your actionPerformed method is a bit strange. Have you gone through the Sun Swing tutorials? That's probably the first thing you should do before trying to write this stuff. It's where most of us have learned to code Swing.
    Last edited by Fubarable; 03-25-2009 at 03:58 AM.

  10. #10
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    I just don't understand how I'm supposed to do this if terminator isn't in the class, since I'm using it for 2 different methods...

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I just don't understand how I'm supposed to do this if terminator isn't in the class, since I'm using it for 2 different methods.
    Please re-read my post. I never told you to take terminator out of the class.

    Here's an example of shadowing:
    Java Code:
    public class ShadowVarEg
    {
      int shadowVar = 3;
      
      public void shadowMethod()
      {
        int shadowVar = 5;  // shadowVar redeclared!
      }
      
      public void nonShadowMethod()
      {
        shadowVar = 20;  // here it's not redeclared
      }
      
      public int getShadowVar()
      {
        return shadowVar;
      }
      
      public static void main(String[] args)
      {
        ShadowVarEg shadowvarEg = new ShadowVarEg();
        shadowvarEg.shadowMethod(); // this will not change shadowVar
        System.out.println(shadowvarEg.getShadowVar());
        shadowvarEg.nonShadowMethod(); // but this will change it.
        System.out.println(shadowvarEg.getShadowVar());
      }
    }
    Your paint method is like the shadowMethod above. Just like shadowMethod redeclares shadowVar, your paint method redeclares your button so that the button seen in the display is not the same one in the class. Again, don't redeclare your variable!
    Last edited by Fubarable; 03-25-2009 at 04:36 AM.

  12. #12
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    Ok Thanks. I made it to where its only defined in the class. It still freezes on me, but I guess I'll have to figure that one out tomorrow... Also, I was reading on the Java Sun Site that NetBeans has a bunch of different addons that does a bunch of the work for me like creating a JFrame, labels, etc. So, maybe that will help me out. I'm just not sure if I'll have enough time to complete this project by the 2nd of April... It's a 510 point project... So, if anyone would like to help out or whatever (for pay or not for pay) then just tell me, because I would greatly appreciate it. Thanks everyone for helping me out so far.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Now you appear to be asking for someone to do this for you (payed or not) so that you can submit their work as yours. This is called cheating is amoral and will not be condoned here. Do this again and I will have to report you to the forum moderators which can get you banned here. Is this really what you want to achieve???
    Last edited by Fubarable; 03-25-2009 at 05:05 AM.

  14. #14
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    lol no, I don't want someone to do my work for me I'm just asking if someone would kinda like to be my assistant. My teacher allows others to help out, but not others to write all of my code for me. So, it wouldn't be cheating. I'm saying that if someone would be dedicated enough to help me out the whole way, then I'd be more than happy to pay them a little something.

  15. #15
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    Here's the new coding that I have... Btw, that Java tutorial really didn't help me... There's not any errors that come up, but when I click on a button, nothing happens (it doesn't repaint the JFrame/remove all of it's components).

    Java Code:
    package karate;
    
    /*
     * KaratePaint.java
     *
     * Created on March 6, 2009, 8:15 AM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    
    /**
     *
     * @Shane Stapleton 0122
     */
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.ImageIcon;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class KaratePaint implements ActionListener
    {
        JButton terminator = new JButton(new ImageIcon("KarateTerminator.jpg"));
        JButton mrT = new JButton(new ImageIcon("KarateMrT.jpg"));
        JButton hulkHogan = new JButton(new ImageIcon("KarateHulkHogan.jpg"));
    
        JLabel you = new JLabel("Select Your Character");
        JLabel blank = new JLabel("                             ");
        JLabel blank2 = new JLabel("                             ");
        JPanel panel = new JPanel();
    
        JFrame frame = new JFrame();
    
        public void paint()
        {
            panel.add(blank);
            panel.add(you);
            panel.add(blank2);
            panel.add(terminator);
            panel.add(mrT);
            panel.add(hulkHogan);
            frame.add(panel);
    
            terminator.addActionListener(this);
    
            frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    
        public void actionPerformed(ActionEvent e)
        {
            frame.repaint();
        }
    
        private static final int FRAME_WIDTH = 500;
        private static final int FRAME_HEIGHT = 500;
    }

  16. #16
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Why do you assume it's "hanging". According to what you have here, you probably would not notice any difference in the gui at all, except seeing the button depress than return to normal.

    Repaint means, essentially, to show any changes you've made to the GUI, but you are not making any, here.

  17. #17
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    Oh, well then what's the command to remove all the components from the JFrame? Because I tried frame.removeAll() and that didn't work.

  18. #18
    Xyle's Avatar
    Xyle is offline Member
    Join Date
    Feb 2009
    Location
    Nevada
    Posts
    34
    Rep Power
    0

    Default

    I haven't used it or tried it, but the results from a simple internet search (which should be your best friend by now) for "java jframe remove button" gave the following result...

    jFrame.remove( child )

    So in your actionevent code...
    Java Code:
        public void actionPerformed(ActionEvent e)
        {
            jFrame.remove( terminator);
            jFrame.remove( hulk );
            jFrame.remove( mrT );
            frame.repaint();
        }
    It may help the people understand if you describe what your trying to achieve. By clicking one of the buttons what is your goal?

  19. #19
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    Ok, I'm making this game called Karate.

    1st Select Character - you get to select your character (in my case it's Mr. T, Hulk Hogan, and the Terminator). Then once you select your character it'll go onto the fight.

    2nd Fighting - after you've selected your character it then removes everything and adds your character, the boss, a button to click that makes you attack. Once you click the button that makes you attack it displays the amount of health taken from the boss. Then the boss attacks back and it shows how much health was taken from you. You and the boss each have 100 hp and the amount of damage taken from each other is generated randomly.

    3rd Leveling - after you have defeated a boss, it then displays where you can select your upgrade. You can choose between receiving more health or being able to attack higher. Each level, the boss gains 10 more health. The game goes on for only 3 levels.

    Also, if you haven't figured this out yet... There's not any real movements... It just has images of you and the boss, then of course it displaying the amount taken from each.

    That's my whole project... Also, what you said Xyle didn't do anything.

  20. #20
    Xyle's Avatar
    Xyle is offline Member
    Join Date
    Feb 2009
    Location
    Nevada
    Posts
    34
    Rep Power
    0

    Default

    You did change the code to...

    frame.remove(terminator);

    instead of jFrame.remove(terminator);

    Right?

    Have you done a search yet on removing components from a jFrame or from a jPanel? You will have to learn to look things up.

    I found this in a search for "java remove jbutton from jpanel"...
    JPanel.remove(Component)

    so since this didnt work...
    Java Code:
        public void actionPerformed(ActionEvent e)
        {
            frame.remove( terminator);
            frame.remove( hulk );
            frame.remove( mrT );
            frame.repaint();
        }
    try this...
    Java Code:
        public void actionPerformed(ActionEvent e)
        {
            panel.remove( terminator);
            panel.remove( hulk );
            panel.remove( mrT );
            frame.repaint();
        }
    If things dont work, you have to figure out why, look things up, look at other code, do searches for the basic things your trying to do. Most of all, keep trying. I spend 90% of my time debugging my code and 10% of my time actually writing code. Hopefully some day I will be able to reverse these numbers, hehehehe. :)
    Last edited by Xyle; 03-26-2009 at 03:05 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 03-08-2009, 01:51 AM
  2. 2 errors with Scanner , help
    By enghar in forum New To Java
    Replies: 3
    Last Post: 01-25-2009, 03:03 AM
  3. Getting errors for some reason
    By Swarvy in forum New To Java
    Replies: 7
    Last Post: 09-30-2008, 02:45 PM
  4. help with these errors
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 04:55 PM
  5. Errors in constructor
    By ai_2007 in forum Advanced Java
    Replies: 0
    Last Post: 07-01-2007, 05:35 PM

Posting Permissions

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