Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 41

Thread: Help with racecar applet?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help with racecar applet?

    I am creating a racecar/moving pictures app for my Java class project, and am on a good track, have recently came into some very helpful information from some classmates finished with their projects and some internet sources, but need help figuring out some errors. the code i have is:

    import javax.swing.JApplet;
    import java.awt.Graphics;
    import javax.swing.JTextField;
    import javax.swing.Timer;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import java.awt.*;
    import java.awt.Color;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.Dimension;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
     
    public class superclass extends JApplet
    {
    private Image iron;
    private Image hulk;
    private Image thor;
    private Image cap;
    private Image exp;
     
    private int Car1, Car2, Car3, Car4;
     
     
    private JTextField one;
    private JTextField two;
    private JTextField three;
    private JTextField four;
    private JPanel big;
    private JButton click, unclick;
    private Timer timer1;
     
     
     
     
     
    private JLabel onelabel;
     
    private JLabel twolabel;
     
    private JLabel threelabel;
     
    private JLabel fourlabel; 
     
     
    public void init()
    {
    JPanel big = new JPanel();
     
    int APPLET_WIDTH = 500;
    int APPLET_HEIGHT = 500;
    big.setPreferredSize(new Dimension(APPLET_WIDTH,APPLET_HEIGHT));
    big.setOpaque(true);
     
    Container background = getContentPane();
    background.setBackground(Color.red);
    setLayout(new FlowLayout());
     
    JTextField one = new JTextField(2);
    JTextField two = new JTextField(2);
    JTextField three = new JTextField(2);
    JTextField four = new JTextField(2);
     
     
    JLabel onelabel = new JLabel("Car 1:");
    JLabel twolabel = new JLabel("Car 2:");
    JLabel threelabel = new JLabel("Car 3:");
    JLabel fourlabel = new JLabel("Car 4:");
    JButton click = new JButton();
    JButton unclick = new JButton();
     
    big.add(onelabel);
    big.add(one);
     
    big.add(twolabel);
    big.add(two);
     
    big.add(threelabel);
    big.add(three);
     
    big.add(fourlabel);
    big.add(four);
    big.add(click);
    big.add(unclick);
     
    iron = getImage(getDocumentBase(),"ironman.jpg");
    hulk = getImage(getDocumentBase(), "hulk.jpg");
    thor = getImage(getDocumentBase(), "thor.jpg");
    cap = getImage(getDocumentBase(), "america.jpg");
    exp = getImage(getDocumentBase(), "explosion.jpg");
    Timer timer = new Timer(0, new TimerHandler());
     
    add(big);
     
    JOptionPane.showMessgeDialog(null, "To make the cars move please enter a\n" + 
    "number between 1 and 40 in each text box. \n" + "\n" + 
    "1 = Slowest Speed 40 = Fastest Speed\n" + "\n" + 
    "If you enter a number lower than 1,\n" +
    "your number will round up to 1.\n" + "\n" +
    "If your number entered is higher than 40,\n" +
    "The Number will be converted to 40. ");
    }
    public void start()
    {
     
     
     
            click.addActionListener(new ActionListener()
     
            {
     
                public void actionPerformed(ActionEvent event)
     
                {
     
     
     
                    Car1 = Integer.parseInt(one.getText());
     
                    Car2 = Integer.parseInt(two.getText());
     
                    Car3 = Integer.parseInt(three.getText());
     
                    Car4 = Integer.parseInt(four.getText());
     
     
     
                    if (Car1 > 5)
     
                    {
     
                        Car1 = 5;
     
                    }
     
                    else if (Car1 < 1)
     
                        Car1 = 1;
     
     
     
                    if (Car2 > 40)
     
                    {
                        Car2 = 40;
                    }
     
                    else if (Car2 < 1)
     
                        Car2 = 1;
     
     
                    if (Car3 > 40)
     
                    {
                        Car3 = 5;
     
                    }
     
                    else if (Car3 < 1)
     
                        Car3 = 1;
     
     
     
                    if (Car4 > 40)
     
                    {
                        Car4 = 40;
                    }
     
                    else if (Car4 < 1)
     
                        Car4 = 1;
     
                    if(timer != null)
                                    {
                        timer.start();
     
                    }
                    else
                    {
     
                            timer.restart();
                    }
                            }
            }
        );
            unclick.addActionListener(new ActionListener()        {
     
                public void actionPerformed(ActionEvent event)
                {
     
                    Car1 = 0;
    		Car2 = 0;
    		Car3 = 0;
    		Car4 = 0;
     
                }
            }
        );
     
        }
     
     
     
     
     
    public void paint(Graphics g)
    {
    	super.paint(g);
     
     
    	g.setColor(Color.BLACK);
    	g.drawLine(0, 100, 500, 100);
    	g.drawLine(0, 200, 500, 200);
    	g.drawLine(0, 300, 500, 300);
    	g.drawLine(0, 400, 500, 400);
    	g.drawLine(0, 500, 500, 500);
     
    	g.drawImage(exp, 100, 100, 500, 500, this);
    	g.drawImage(iron, 0, 100, 100, 100, this);
    	g.drawImage(hulk, 0, 200, 100, 100, this);
    	g.drawImage(thor, 0, 300, 100,100, this);
    	g.drawImage(cap, 0,400,100,100, this);
     
    	iron = (Car1);
    	hulk = (Car2);
    	thor = (Car3);
    	cap = (Car4);
     
    	}
     
    }
     
    private class TimerHandler implements ActionListener
    {
    	public void actionPerformed(ActionEvent actionEvent)
    {
    	repaint();
    }
     
    }
    the errors that im gettning are including the (iron = Car1) code along the the other similar ones, and also im having trouble just gettiing this code to run. i have gotten the images up and am in the process of just getting them to move. could anybody help? i will list all errors below.
    Complier Errors:
    superclass.java:97:cannot find symbol
    superclass.java:178: cannot find symbol
    superclass.java:180: cannot find symbol
    superclass.java:186: cannot find symbol
    superclass.java.223: cannot find symbol
    superclass.java:229: incompatible types
    superclass.java:230: incompatible types
    superclass.java:231: incompatible types
    superclass.java:232: incompatible types
    superclass.java:242: cannot find symbol

    im fairly new at this so any help would be greatly appreciated. Thanks!
    p.s: i am also looking into double buffering, but dont know where to start, and the name of my class is superclass


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    Please copy the full text of the error messages and paste them here.
    Here is a sample:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

    The compiler gives the cannot find symbol error because it can not find a definition for the variable(symbol) mentioned in the error message.
    Check that the variable is defined with the exact same spelling and that it is in scope(within the same pair of {}s) where it is being used.

    The incompatible types error is because the data types are not compatible. For example trying to assign a String value to an int variable: int x = "123"
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help with racecar applet?

    Im making a racecar app for my java class for a final exam grade, and i am rushing to get this done, but want it to work well. i consulted my teacher in class and she said that i would need to change my g.drawImages in order to get my pictures to move, but as far as coming up with how im drawing up blanks. I must utilize the Timer in order to get my pictures to move and to get them to go to a certain speed up to 40, depending on what the user enters before pressing the start button. does anybody have any ideas how to get my code to work like that, and if so how would i go about doing that? and also i was thinking about the possibility of putting my pictures in squares and moving the squares across the screen instead of the pictures themselves, but as far as that goes im drawing up blanks abou that one too. i would appreciate any ideas/links that anyone can point me to, and also the help figuring out my code error, the only one that's showing up is aboui the TimerHandler. am i not using it correctly? thanks
    import javax.swing.JApplet;
    import java.awt.Graphics;
    import javax.swing.JTextField;
    import javax.swing.Timer;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import java.awt.*;
    import java.awt.Color;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.Dimension;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
     
    public class superclass extends JApplet
    {
    private Image iron;
    private Image hulk;
    private Image thor;
    private Image cap;
    private Image exp;
     
    private int Car1, Car2, Car3, Car4;
     
     
    private JTextField one;
    private JTextField two;
    private JTextField three;
    private JTextField four;
    private JPanel big;
    private JButton click, unclick;
    private Timer timer1;
     
     
     
     
     
    private JLabel onelabel;
     
    private JLabel twolabel;
     
    private JLabel threelabel;
     
    private JLabel fourlabel; 
     
     
    public void init()
    {
    JPanel big = new JPanel();
     
    int APPLET_WIDTH = 500;
    int APPLET_HEIGHT = 500;
    big.setPreferredSize(new Dimension(APPLET_WIDTH,APPLET_HEIGHT));
    big.setOpaque(true);
     
    Container background = getContentPane();
    background.setBackground(Color.red);
    setLayout(new FlowLayout());
     
    JTextField one = new JTextField(2);
    JTextField two = new JTextField(2);
    JTextField three = new JTextField(2);
    JTextField four = new JTextField(2);
     
     
    JLabel onelabel = new JLabel("Car 1:");
    JLabel twolabel = new JLabel("Car 2:");
    JLabel threelabel = new JLabel("Car 3:");
    JLabel fourlabel = new JLabel("Car 4:");
    JButton click = new JButton();
    JButton unclick = new JButton();
     
    big.add(onelabel);
    big.add(one);
     
    big.add(twolabel);
    big.add(two);
     
    big.add(threelabel);
    big.add(three);
     
    big.add(fourlabel);
    big.add(four);
    big.add(click);
    big.add(unclick);
     
    iron = getImage(getDocumentBase(),"ironman.jpg");
    hulk = getImage(getDocumentBase(), "hulk.jpg");
    thor = getImage(getDocumentBase(), "thor.jpg");
    cap = getImage(getDocumentBase(), "america.jpg");
    exp = getImage(getDocumentBase(), "explosion.jpg");
    Timer timer = new Timer(0, new TimerHandler());
     
    add(big);
     
    JOptionPane.showMessageDialog(null, "To make the cars move please enter a\n" + 
    "number between 1 and 40 in each text box. \n" + "\n" + 
    "1 = Slowest Speed 40 = Fastest Speed\n" + "\n" + 
    "If you enter a number lower than 1,\n" +
    "your number will round up to 1.\n" + "\n" +
    "If your number entered is higher than 40,\n" +
    "The Number will be converted to 40. ");
    }
     
     
     
     
    public void paint(Graphics g)
    {
    	super.paint(g);
     
     
    	g.setColor(Color.BLACK);
    	g.drawLine(0, 100, 500, 100);
    	g.drawLine(0, 200, 500, 200);
    	g.drawLine(0, 300, 500, 300);
    	g.drawLine(0, 400, 500, 400);
    	g.drawLine(0, 500, 500, 500);
     
    	g.drawImage(exp, 100, 100, 500, 500, this);
    	g.drawImage(iron, 0, 100, 100, 100, this);
    	g.drawImage(hulk, 0, 200, 100, 100, this);
    	g.drawImage(thor, 0, 300, 100,100, this);
    	g.drawImage(cap, 0,400,100,100, this);
     
     
    	}
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    help figuring out my code error,
    Please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    I worked out some of the kinks and this is what i have working for right here:
    import javax.swing.JApplet;
    import java.awt.Graphics;
    import javax.swing.JTextField;
    import javax.swing.Timer;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import java.awt.*;
    import java.awt.Color;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.Dimension;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
    import java.awt.image.*;
    import javax.swing.*;
     
    public class superclass extends JApplet
    {
    private Image iron;
    private Image hulk;
    private Image thor;
    private Image cap;
    private Image exp;
     
    private int Car1, Car2, Car3, Car4;
     
     
    private JTextField one;
    private JTextField two;
    private JTextField three;
    private JTextField four;
    private JPanel big;
    private JButton click, unclick;
    private Timer timer1;
     
     
     
     
     
    private JLabel onelabel;
     
    private JLabel twolabel;
     
    private JLabel threelabel;
     
    private JLabel fourlabel; 
     
     
    public void init()
    {
    JPanel big = new JPanel();
     
    int APPLET_WIDTH = 500;
    int APPLET_HEIGHT = 500;
    big.setPreferredSize(new Dimension(APPLET_WIDTH,APPLET_HEIGHT));
    big.setOpaque(true);
     
    Container background = getContentPane();
    background.setBackground(Color.red);
    setLayout(new FlowLayout());
     
    JTextField one = new JTextField(2);
    JTextField two = new JTextField(2);
    JTextField three = new JTextField(2);
    JTextField four = new JTextField(2);
     
     
    JLabel onelabel = new JLabel("Car 1:");
    JLabel twolabel = new JLabel("Car 2:");
    JLabel threelabel = new JLabel("Car 3:");
    JLabel fourlabel = new JLabel("Car 4:");
    JButton click = new JButton();
    JButton unclick = new JButton();
     
    big.add(onelabel);
    big.add(one);
     
    big.add(twolabel);
    big.add(two);
     
    big.add(threelabel);
    big.add(three);
     
    big.add(fourlabel);
    big.add(four);
    big.add(click);
    big.add(unclick);
     
    iron = getImage(getDocumentBase(),"ironman.jpg");
    hulk = getImage(getDocumentBase(), "hulk.jpg");
    thor = getImage(getDocumentBase(), "thor.jpg");
    cap = getImage(getDocumentBase(), "america.jpg");
    exp = getImage(getDocumentBase(), "explosion.jpg");
    Timer timer = new Timer(50, new TimeHandler());
     
    add(big);
     
    JOptionPane.showMessageDialog(null, "To make the cars move please enter a\n" + 
    "number between 1 and 40 in each text box. \n" + "\n" + 
    "1 = Slowest Speed 40 = Fastest Speed\n" + "\n" + 
    "If you enter a number lower than 1,\n" +
    "your number will round up to 1.\n" + "\n" +
    "If your number entered is higher than 40,\n" +
    "The Number will be converted to 40. ");
    }
    public void start()
    {
     
     
     
            click.addActionListener(new ActionListener()
     
            {
     
                public void actionPerformed(ActionEvent event)
     
                {
     
     
     
                    Car1 = Integer.parseInt(one.getText());
     
                    Car2 = Integer.parseInt(two.getText());
     
                    Car3 = Integer.parseInt(three.getText());
     
                    Car4 = Integer.parseInt(four.getText());
     
     
     
                    if (Car1 > 5)
     
                    {
     
                        Car1 = 5;
     
                    }
     
                    else if (Car1 < 1)
     
                        {Car1 = 1;}
     
     
     
                    if (Car2 > 40)
     
                    {
                        Car2 = 40;
                    }
     
                    else if (Car2 < 1)
     
                       { Car2 = 1;}
     
     
                    if (Car3 > 40)
     
                    {
                        Car3 = 5;
     
                    }
     
                    else if (Car3 < 1)
     
                        {Car3 = 1;}
     
     
     
                    if (Car4 > 40)
     
                    {
                        Car4 = 40;
                    }
     
                    else if (Car4 < 1)
    {
                        Car4 = 1;
    }
    }
    }
    );
     
     
     
     
    ;
    public void paint(Graphics g)
    {
    	super.paint(g);
     
     
    	g.setColor(Color.BLACK);
    	g.drawLine(0, 100, 500, 100);
    	g.drawLine(0, 200, 500, 200);
    	g.drawLine(0, 300, 500, 300);
    	g.drawLine(0, 400, 500, 400);
    	g.drawLine(0, 500, 500, 500);
     
    	g.drawImage(exp, 100, 100, 500, 500, this);
    	g.drawImage(iron, 0, 100, 100, 100, this);
    	g.drawImage(hulk, 0, 200, 100, 100, this);
    	g.drawImage(thor, 0, 300, 100,100, this);
    	g.drawImage(cap, 0,400,100,100, this);
     
     
    	}
     
     
    }
    }
    private class TimerHandler implements ActionListener
    {
    	public void actionPerformed(ActionEvent event)
    {
    	repaint();
    }
     
    }
    and this is the four errors im getting
    superclass.java:188: illegal start of expression
    public void paint(Graphics g)
     
    superclass.java:188: illegal start of expression
    public void paint(Graphics g)
     
    superclass.java:188: ';' expected
    public void paint(Graphics g)
     
    superclass.java:188: ';' expected
    public void paint(Graphics g)
    im not a big expert on code, so could i get some help diagnosing this? i think i got everything ok, and also im looking for help with my timer, i have one that im trying to get runing but everytime i try to get it going it doesnt work. the code im trying to implement for my timer will be posted right under this, im trying to get it working the the public void start part of my code.
    if(timer =! null)
    {
    timer.start();
    }
    else
    {
    if(!timer.isRunning())
    timer.restart();
    }
     
    unclick.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {
    timer.stop();
    }
    }
    i havent added the timer things above because i just wanted to get that first code working first, so the errors for it wont show up

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    The formatting of the code needs to be fixed so it can be read and understood. The }s should not all be in the first column. They should be in line beneath the start of the statements with the { that they pair with.
    Its impossible to see where methods end.

    Also too many statements start in the first column and again that makes it hard to read and understand the code.
    Nested statements should be indented 3-4 spaces to show logic levels.


    The error may have something to do with the {}s
    It's hard to tell.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    superclass.java:84: addActionListener(java.awt.event.ActionListener) in java.swing.AbstractButton cannot be applied to (superclass)
    click.addActionListener(this);
    Ok I got that settled and for now I just took out the problem code. The problem I'm having now is trying to get my button to cause paint.start() to happen upon clicking. When I try to do a click.ActionListener() (click Is the buttons name) I get the above error. Also any ideas in how to change my images to be moving would be nice, I'm trying to google and read up on them but I'm getting blanks when it comes to implementing timer with it. Thanks.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    Please post the current version of the code.

    change my images to be moving
    Use a Timer or some other technique to change the x,y locations where the images are drawn.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    click.addActionListener(new ActionListener() { 
     
    public void actionPerformed(ActionEvent e)
    {
     
    timer1.start();
    }
     
    });
    I have an action listener connected to my button, so when it's clicked the timer should start. It's not starting. My code is above, and Im trying to think if a code to use with the x,y positions, since I know that the x must be move in order for my code to works. What line of code invokes the updating of a timer, so each time the timer goes off my x's will be updated as well? I saw a line of code called public void update(Graphics g), would that work?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    Please edit the posted code and properly format it. Nested statements within {} should be indented 3-4 spaces to make the code easier to read and understand. Statements should NOT all start in the first column.

    the timer should start. It's not starting
    Where is the current definition of the timer1 object?

    each time the timer goes off my x's will be updated
    Put the updating of the value of x in the timer's listener method that is called when the timer has an event. After updating the location, call the repaint() method for the component object where the images are being drawn.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    g.drawImage(iron, a, 100 100, 100, this);
    g.drawImage(exp, 100, 100, 500, 500, this);
    g.drawImage(hulk, b, 100, 100, 100 this);
    g.drawImage(thor, c, 100, 100, 100, this);
    g.drawImage(cap, d, 100, 100, 100, this);
    These are my images in paint, and my timer is made in the public void init part of my program. The int a, b, c, and d are the x's that I want to change so my pictures will move. I have some code for my Timer that's supposed to make the repaint go off and update the x's:
    //this is in init
    timer1 = new Timer(5, new TimerHandler());
    //this is made after my public void paint
    private class TimerHandler implements ActionListener
    {
     
    public void actionPerformed(ActionEvent event)
     
     
    {
     
    repaint();
    a++;
    b++;
    c++;
    d++;
     
    }
     
     
    }
    They seem to not be working. Any ideas why? Also, my button from before still isn't making the timer go off.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    They seem to not be working
    Is the method they are in called? Add a call to the println() method to print a message when the actionPerformed() method is executed.

    Nested statements within {} should be indented 3-4 spaces to make the code easier to read and understand. Statements should NOT all start in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    I've recently run into a new problem, am on break from school but have to have this done by next Tuesday. I've gotten a lot done, but a new problem has arrisen. this is my full code:
    import javax.swing.JApplet;
    import java.awt.Graphics;
    import javax.swing.JTextField;
    import javax.swing.Timer;
    import javax.swing.JPanel;
    import java.awt.*;
    import java.awt.Color;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.Dimension;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
    import java.awt.image.*;
    import javax.swing.*;
    import javax.swing.AbstractButton;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
     
    public class superclass extends JApplet
    {
    private Image iron;
    private Image hulk;
    private Image thor;
    private Image cap;
    private Image exp;
     
     
    private int Car1, Car2, Car3, Car4;
     
     
    private JTextField one;
    private JTextField two;
    private JTextField three;
    private JTextField four;
    private JPanel big;
    private JButton click, unclick;
    private Timer timer1;
     
     
    private int a, b, c, d;
     
     
    private JLabel onelabel;
     
    private JLabel twolabel;
     
    private JLabel threelabel;
     
    private JLabel fourlabel; 
     
     
    public void init()
    {
     
     
    JPanel big = new JPanel();
     
    int APPLET_WIDTH = 500;
    int APPLET_HEIGHT = 500;
    big.setPreferredSize(new Dimension(APPLET_WIDTH,APPLET_HEIGHT));
    big.setOpaque(true);
     
    Container background = getContentPane();
    background.setBackground(Color.blue);
    setLayout(new FlowLayout());
     
    JTextField finalone = new JTextField("", 3);
    JTextField finaltwo = new JTextField("", 3);
    JTextField finalthree = new JTextField("", 3);
    JTextField finalfour = new JTextField("", 3);
     
     
    JLabel onelabel = new JLabel("IronMan:");
    JLabel twolabel = new JLabel("Thor:");
    JLabel threelabel = new JLabel("Hulk:");
    JLabel fourlabel = new JLabel("Captain America:");
    click = new JButton("Avengers, MOVE OUT!!");
    click.setActionCommand("enable");
    unclick = new JButton("Avengers, Halt!");
    timer1 = new Timer(0, new TimerHandler());
     
    big.add(onelabel);
    big.add(one);
     
    big.add(twolabel);
    big.add(two);
     
    big.add(threelabel);
    big.add(three);
     
    big.add(fourlabel);
    big.add(four);
    big.add(click);
    big.add(unclick);
     
    iron = getImage(getDocumentBase(),"ironman.jpg");
    hulk = getImage(getDocumentBase(), "hulk.jpg");
    thor = getImage(getDocumentBase(), "thor.jpg");
    cap = getImage(getDocumentBase(), "america.jpg");
    exp = getImage(getDocumentBase(), "explosion.jpg");
     
     
    add(big);
     
    JOptionPane.showMessageDialog(null, "AVENGERS MOVE OUT!! \n" + "To make your SuperHeros accelerate move please enter a\n" + 
    "number between 1 and 40 in each text box. \n" + "\n" + 
    "1 = Slowest Speed 40 = Fastest Speed\n" + "\n" + 
    "If you enter a number lower than 1,\n" +
    "your Speed will round up to 1.\n" + "\n" +
    "If your number entered is higher than 40,\n" +
    "The Speed will be converted to 40. ");
     
     
    a = 0;
    b = 0;
    c = 0;
    d = 0;
     
     
    }
     
     
     
    public void start(){
     
     
     
    click.addActionListener(new ActionListener() {
     
    	public void actionPerformed(ActionEvent event)
    	{
     
    	Car1 = Integer.parseInt(one.getText());
    	Car2 = Integer.parseInt(two.getText());
    	Car3 = Integer.parseInt(three.getText());
    	Car4 = Integer.parseInt(four.getText());
     
    	if(Car1 > 40)
    	{
    		Car1 = 40;
    	}
    	else if(Car1 < 1)
    		{
     
                    Car1 = 1;
                    }
     
     
    	if(Car2 > 40)
    	{
    		Car2 = 40;
    	}
    	else if(Car2 < 1)
    		{
     
                     Car2 = 1;
     
     
                    }
     
     
    	if(Car3 > 40)
    	{
    		Car3 = 40;
    	}
    	else if(Car3 < 1)
    		{
     
                    Car3 = 1;
     
                    }
     
     
    	if(Car4 > 40)
    	{
     
     
     
     
    		Car4 = 40;
     
     
     
    	}
    	else if(Car4 < 1)
    		{
     
     
                       Car4 = 1;
     
     
                      }
     
     
     
     
     
    	timer1.start();
    	}
     
    });
     
    unclick.addActionListener(new ActionListener() {
     
    	public void actionPerformed(ActionEvent b)
    	{
     
    		Car1 = 0;
    		Car2 = 0;
    		Car3 = 0;
    		Car4 = 0;
    		timer1.stop();
    	}
    });
     
     
    }
     
     
     
     
    public void paint(Graphics g)
    {
    	super.paint(g);
     
     
    	g.setColor(Color.BLACK);
     
    	g.drawLine(0, 100, 500, 100);
    	g.drawLine(0, 200, 500, 200);
    	g.drawLine(0, 300, 500, 300);
    	g.drawLine(0, 400, 500, 400);
    	g.drawLine(0, 500, 500, 500);
     
     
    	g.drawImage(iron, Car1, 100 ,100, 100, this);
    	g.drawImage(exp, 100, 100, 500, 500, this);
    	g.drawImage(hulk, Car2, 200, 100, 100, this);
    	g.drawImage(thor, Car3, 300, 100,100, this);
    	g.drawImage(cap, Car4, 400, 100,100, this);
     
     
    }
     
     
     
     
     
     
     
     
    private class TimerHandler implements ActionListener
    {
     
    	public void actionPerformed(ActionEvent actionEvent)
     
    {
     
    	repaint();
    	Car1++;
    	Car2++;
    	Car3++;
    	Car4++;
     
     
     
     
    }
     
     
    }
     
    }
    im getting some weird error saying that something about a exception and the applet not being initialized. it says:
    java.lang.NullPointerException
            at java.awt.Container.addImpl(Container.java:1045)
            at java.awt.Container.add(Container.java:365)
            at superclass.init(superclass.java:86)
            at sun.applet.AppletPanel.run(AppletPanel.java:424)
            at java.lang.Thread.run(Thread.java:619)
    i cannot make heads or tails of it can you guys help me? thanks.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1045)
    at java.awt.Container.add(Container.java:365)
    at superclass.init(superclass.java:86)
    There is a variable with a null value on line 86. Look at line 86 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 86 and print out the values of all the variables on that line.



    Nested statements within {} should be indented 3-4 spaces to make the code easier to read and understand. Statements should NOT all start in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    How do i add a println? i did a System.out.println() over the whole line but then it skipped to line 92 and so on and so on lol. it's really frustrating as i've never seen this error before.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    Code a System.out.println() for each variable on line 86.
    it skipped to line 92 and so on and so on
    Please post the code to explain how the code skipped to line 92.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    big.add(one);
    java.lang.NullPointerException
            at java.awt.Container.addImpl(Container.java:1045)
            at java.awt.Container.add(Container.java:365)
            at superclass.init(superclass.java:89)
            at sun.applet.AppletPanel.run(AppletPanel.java:424)
            at java.lang.Thread.run(Thread.java:619)
    this happens when i do the :
    System.out.println("big.add(one)");
    and when i went to line 89 and did the system.out.println, i got this:
    big.add(one);
    big.add(two);
    java.lang.NullPointerException
            at java.awt.Container.addImpl(Container.java:1045)
            at java.awt.Container.add(Container.java:365)
            at superclass.init(superclass.java:92)
            at sun.applet.AppletPanel.run(AppletPanel.java:424)
            at java.lang.Thread.run(Thread.java:619)

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    System.out.println("big.add(one)");
    That prints a String (the stuff between the "s). It does NOT print the values of any variables.
    There are two variables in that source statement: big and one
    The println needs to print their values:
    System.out.println("big="+big + ", one="+one);
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    Ok i did it like you posted, and got this now
    big=javax.swing.JPanel[,0,0,0x0,invalid,layout=java.awt.FlowLayout,alignmentX=0.
    0,alignmentY=0.0,border=,flags=16777225,maximumSize=,minimumSize=,preferredSize=
    java.awt.Dimension[width=500,height=500]], one=null
    java.lang.NullPointerException
            at java.awt.Container.addImpl(Container.java:1045)
            at java.awt.Container.add(Container.java:365)
            at superclass.init(superclass.java:91)
            at sun.applet.AppletPanel.run(AppletPanel.java:424)
            at java.lang.Thread.run(Thread.java:619)
    i got a feeling that something is nulling all my JTextFields in my applcation, and i have no idea why. i had to set them like so in init():
    JTextField finalone = new JTextField("", 3);
    JTextField finaltwo = new JTextField("", 3);
    JTextField finalthree = new JTextField("", 3);
    JTextField finalfour = new JTextField("", 3);
    so i could use them in a Integer.parseInt:
            Car1 = Integer.parseInt(one.getText());
    	Car2 = Integer.parseInt(two.getText());
    	Car3 = Integer.parseInt(three.getText());
    	Car4 = Integer.parseInt(four.getText());
    could that be the problem? im really trying to get this problem fixed asap because this thing is due in 2 days and haven't even started moving yet.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    Did you see this part of what was printed:
    , one=null
    That shows that the one variable has a null value.
    Look back through the code to see why that variable: one does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    i got problem handled now. the only error im running into is when i run my program, i get this error whenever i press the button linked to the start timer:
    [CODE]
    Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at superclass$1.actionPerformed(superclass.java:129)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:19
    95)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.jav
    a:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel
    .java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242
    )
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonL
    istener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6267)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:603 2)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577
    )
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)

    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThre
    ad.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.
    java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
    ad.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)

    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)

    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

    [\CODE]
    can you help me diagnose this?

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at superclass$1.actionPerformed(superclass.java:129)
    There is a variable with a null value on line 129. Look at line 129 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 129 and print out the values of all the variables on that line.


    This is the same kind of problem as was posted in post#13
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    i took out some program:
    [CODE]
    Car1 = Integer.parseInt(one.getText());
    Car2 = Integer.parseInt(two.getText());
    Car3 = Integer.parseInt(three.getText());
    Car4 = Integer.parseInt(four.getText());

    [\CODE]
    and found that it corrects the program and allows the timer buttons to work. the only problem im running into now is trying to get the text from the speeds to affect the x's of my code. i want to speeds to become the rate that the x's change, so if i were to enter 40 into the JTextField one, it would move at 40 x units per timer ding. the only way i've though about turning the "40" entered into the textbox into an int was the Integer.parseInt(), which is the cause of my error. is there a different way of doing that?

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with racecar applet?

    get the text from the speeds
    Are the "speeds" textfields? What are the variable names for those text fields? Where does the program get the contents of the textfields so they can be converted to int values and used to control the movement of the cars?

    which is the cause of my error.
    What error are you talking about? The NullPointerException?
    That is caused by a variable with a null value. You MUST find that variable and give it a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help with racecar applet?

    the variable was basically all the JTextFields, one, two, three, and four. something about turning the text inside them into the int Car1 made my program quite angry.
    This is the code for my JTextFields:
    JTextField one = new JTextField("", 3);
    JTextField two = new JTextField("", 3);
    JTextField three = new JTextField("", 3);
    JTextField four = new JTextField("", 3);
    im not sure how any of this makes them null, or gives them a value not akin to a JTextField. would it be because the user pressed a button and didnt enter a digit? would i have to swtich some code around to make it change? and yes by speed i mean the text entered into the JTextField. when i run my program using the timer and the TimerHandler i created for the ActionListener, which is:
    [CODE]
    private class TimerHandler implements ActionListener
    {

    public void actionPerformed(ActionEvent event)

    {

    a++;;\
    b++;
    c++;
    d++;
    repaint();



    }


    }
    [\CODE]
    they all slide across the screen at the same speed.

Page 1 of 2 12 LastLast

Similar Threads

  1. Applet help
    By Javan00bz in forum Paid Java Projects
    Replies: 4
    Last Post: August 20th, 2013, 02:23 PM
  2. Applet help
    By Javan00bz in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 13th, 2013, 07:46 AM
  3. Replies: 29
    Last Post: May 18th, 2012, 02:16 PM
  4. Applet
    By needhelp101 in forum AWT / Java Swing
    Replies: 16
    Last Post: November 7th, 2011, 06:22 PM
  5. Replies: 0
    Last Post: October 13th, 2011, 07:42 PM