Results 1 to 2 of 2
  1. #1
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Question Applet running funny

    Good afternoon all,
    So I have built a quick Applet, but when I run it, it is not smooth and fluid. It is just a text box with a yellow ball moving around the screen, and when I run it in netbeans by executing Shift + f6 It runs, the window pops up and when I try to resize it etc.... I can see that the yellow ball is moving on the screen and the coordinates are chaning. Just can't figure out why it isn't fluid when I run it. I compared it to someone else's that when run is smooth. At first I thought it was my machine, but have run it on multiple machines. Not sure if I am just missing something that makes it refresh properly? I have increased and decreased the ANIMATIONDELAY as well but no change.

    Any assistance would be appreciated

    Java Code:
    package steadman.applet;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JApplet;
    import javax.swing.Timer;
    
    public class SteadmanApplet extends JApplet {
        //private double sum;
    
        private int xLoc = 0;
        private int yLoc = 0;
        private final int ANIMATIONDELAY = 100;
        private Timer myTimer;
    
        @Override
        public void init() {
    //        String n1 = JOptionPane.showInputDialog("xLoc ");
    //        String n2 = JOptionPane.showInputDialog("Enter second number");
    //        
    //        double nD1 = Double.parseDouble(n1);
    //        double nD2 = Double.parseDouble(n2);
    //        
    //        sum = nD1 + nD2;
            myTimer = new Timer(ANIMATIONDELAY, new TimerHandler());
            myTimer.start();
        }
    
        @Override
        public void paint(Graphics g) {
            super.paint(g);
    
            g.drawRect(15, 10, 270, 20);
            g.drawString("xLoc is " + xLoc + "yLoc is " + yLoc, 25, 25);
            g.setColor(Color.yellow);
            g.fillOval(xLoc, yLoc, 130, 130);
    
        }
    
        private class TimerHandler implements ActionListener {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                xLoc += 10;
                yLoc += 5;
                if (xLoc > 200) {
                    xLoc = 0;
                }
                if (yLoc > 300) {
                    yLoc = 0;
                }
                repaint();
            }
        }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Applet running funny

    For starters, you should be overriding paintComponent, not paint().
    And the first line shuold be super.paintComponent(g).
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Stop applet from running
    By lemanou in forum Java Applets
    Replies: 0
    Last Post: 03-03-2013, 05:16 PM
  2. Replies: 7
    Last Post: 12-18-2011, 04:13 AM
  3. Applet not running in
    By sgtmac1995 in forum Java Applets
    Replies: 0
    Last Post: 10-18-2010, 04:36 PM
  4. running an applet
    By ksatty in forum Java Applets
    Replies: 4
    Last Post: 01-08-2010, 10:12 AM
  5. Java Applet Not Running
    By kahaj in forum Java Applets
    Replies: 4
    Last Post: 10-08-2009, 12:11 AM

Posting Permissions

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