Results 1 to 2 of 2

Thread: java applet help

  1. #1

    java applet help

    Hey, Im never to java and thought Id have a go at making a few things to get used it it. Currently Ive been trying to convert an application to an applet.

    This is the original coding of the application:

    import java.lang.*;
    import java.awt.*;

    class BallGame extends Frame
    {
    Ball myBall, myBall2;

    BallGame()
    {
    super("Assignment 2: Ball Game");
    setSize(600,400);

    myBall=new Ball(100,100);
    myBall2=new Ball(100,130);

    setVisible(true);
    }

    public void paint(Graphics g)
    {
    myBall.move();
    myBall2.move();
    myBall2.paint(g);
    myBall.paint(g);
    repaint();
    }

    private class Ball
    {
    private int dx=1;
    private int x;
    private int y;
    Ball(int xin,int yin)
    {
    x=xin;
    y=yin;
    }

    void move()
    {
    if(x>=600-120)
    dx=-dx;
    if(x<=0)
    dx=-dx;

    x=x+dx;
    }

    public void paint(Graphics g)
    {
    g.drawOval(x,y,120,40);
    }

    }
    }

    ***********************************************

    import java.applet.Applet;
    import java.awt.*;
    public class J11 extends Applet {


    public static void main(String argv[])
    {
    BallGame f;
    f=new BallGame();
    }
    }

    ***********************************************

    This is what Ive got so far, after some help from a couple of other java newbies. The only error I currently get is on the public class Ball() line and it states { expected. Could someone help fix it please and point me in the right direction if I have made any other errors, thanks.

    import java.awt.*;
    import java.applet.*;
    import java.awt.Graphics.*;

    public class BallGame extends Applet
    {
    Ball myBall, myBall2;

    public void init()
    {
    super("Assignment 2: Ball Game");
    setSize(600,400);

    myBall=new Ball(100,100);
    myBall2=new Ball(100,130);

    setVisible(true);
    }

    public class Ball()
    {
    private int dx=1;
    private int x;
    private int y;
    Ball(int xin,int yin)
    {
    x=xin;
    y=yin;
    }

    public void move()
    {
    if(x>=600-120)
    dx=-dx;
    if(x<=0)
    dx=-dx;

    x=x+dx;
    }
    }

    void paint(Graphics g)
    {
    myBall.move();
    myBall2.move();
    myBall2.paint(g);
    myBall.paint(g);
    repaint();
    }

    void paint(Graphics g)
    {
    g.drawOval(x,y,120,40);
    repaint();
    }
    }
    }

    Think thats aboutit for now, Thanks for your time.

  2. #2
    Forum Techie A_old's Avatar
    Join Date
    Jan 2000
    Location
    Atlanta
    Posts
    10,663
    Wow. That looks like a school assignment, if I ever saw one. I'd get started by searching on how to write an applet to begin with. Once you've figured out how to write the basic bits of an applet, it'll be VERY easy for you to convert this to an applet. Start here: http://java.sun.com/javase/6/docs/api/index.html

Similar Threads

  1. Need java applet help
    By Firestorm ZERO in forum Programming Forum
    Replies: 6
    Last Post: 07-23-06, 03:06 PM
  2. About JAVA...
    By Bouncer in forum Software Forum
    Replies: 10
    Last Post: 05-11-06, 03:52 PM
  3. Symantec and Java Issues
    By mnosteele52 in forum Software Forum
    Replies: 1
    Last Post: 03-09-06, 01:59 AM
  4. Freeware Java Applications
    By A_old in forum General Discussion Board
    Replies: 3
    Last Post: 07-23-05, 01:45 AM
  5. java nice questions
    By aaa_aaa in forum Programming Forum
    Replies: 0
    Last Post: 03-25-05, 06:59 AM

Bookmarks

Posting Permissions

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