Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Java don't work

  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Java don't work

    I got this Java, but it don't work like i want it too

    Script:
    Code:
    import java.applet.*;
    import java.awt.*;
    
    
    
    public class MovingBall extends Applet implements Runnable
    {
    
        int x_pos = 30;
        int y_pos = 100;
        int x_speed = 1;
        int radius = 20;
        int appletsize_x = 300;
        int appletsize_y = 300;
    
    private Image dbImage;
    private Graphics dbg;
    
        public void init()
    {
    		setBackground (Color.blue);
    }
        public void start()
        {
            Thread th = new Thread (this);
    
        	th.start();
        }
        public void stop(){}
        public void destroy(){}
        public void run()
        {
            Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
            while (true)
            {
                if (x_pos > appletsize_x - radius)
                {
                    x_speed = -1;
                }
                 else if (x_pos < radius)
                {
         		x_speed = +1;
                }
                x_pos += x_speed;
    
                repaint();
    
                try
                {
                    Thread.sleep (20);
                }
                catch (InterruptedException ex)
                {
    		// do nothing
                }
                Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
            }
        }
        public void paint(Graphics g)
        {
            g.setColor(Color.red);
            g.fillOval(x_pos - radius, y_pos - radius, 2*radius, 2*radius);
    
        }
        public void update (Graphics g)
        {
            if (dbImage == null)
            {
                dbImage = createImage (this.getSize().width, this.getSize().height);
                dbg = dbImage.getGraphics ();
            }
    
        dbg.setColor(getBackground ());
        dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
    
        dbg.setColor(getForeground());
        paint (dbg);
    
        g.drawImage (dbImage, 0, 0, this);
    
    }
    
    
    
    }
    the ball needs to go back when it hits the side but it goes out of the screen, anyone know why?

  • #2
    Regular Coder
    Join Date
    Dec 2007
    Posts
    137
    Thanks
    1
    Thanked 21 Times in 21 Posts
    Works fine for me.

    I copy and pasted your code and then compiled it which gave me the MovingBall.class applet.

    The applet worked fine in my web browser. The ball bounces back each time it hits a wall.

    I just coded this simple html to embed the applet and display in the browser:

    Code:
    <HTML>
    <BODY>
    <APPLET CODE=MovingBall.class WIDTH=300 HEIGHT=300>
    </APPLET>
    </BODY>
    </HTML>
    Don't know. Try different browsers? Update your Java to the most recent?

  • Users who have thanked pigpen for this post:

    Son_Of_Diablo (01-04-2011)

  • #3
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    okay, it works now, but i didn't change anything :S


    but thanks anyway pigpen


  •  

    Tags for this Thread

    Posting Permissions

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