Results 1 to 12 of 12
  1. #1
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Red face Creating Java applet

    I have to create a Java applet, but I don't know how. It should display a simulation a running rabbit in 10 circles, explaining the For cycle in a programming language C. I tried to make it in NetBeans, but I couldn't. Can anubody please help me? Any hint, useful site or line of code... :( I would be very, very grateful.

  2. #2
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default Code

    This code is all I have:

    package org.me.hello;
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.Font;
    import java.awt.event.*;
    public class NewApplet extends Applet implements Runnable {
    int frameNumber = 0;
    int delay = 100;
    Thread animatorThread;
    boolean frozen = false;
    Color redColor;
    Color bgColor;
    Font f = new Font("TimesRoman", Font.BOLD, 20);
    public void init() {
    String str;
    addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
    if (frozen) {
    frozen = false;
    start();
    } else {
    frozen = true;
    stop();
    }
    }
    });
    }
    public void start() {
    if (!frozen) {
    if (animatorThread == null) {
    animatorThread = new Thread(this);
    }
    animatorThread.start();
    }
    }
    public void stop() {
    animatorThread = null;
    }
    public void run() {
    Thread.currentThread().setPriority(Thread.MIN_PRIO RITY);
    long startTime = System.currentTimeMillis();
    Thread currentThread = Thread.currentThread();
    while (currentThread == animatorThread) {
    frameNumber++;
    repaint();
    try {
    startTime += delay;
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    break;
    }
    }
    }
    public void paint(Graphics g) {
    g.setFont(f);
    g.setColor(Color.red);
    g.drawString("FOR petlja", 120, 20);
    g.setColor(Color.black);
    setBackground( Color.white );
    g.drawOval(60, 60, 220, 140 );
    g.drawOval( 80, 80, 180, 100);
    g.drawString("Krug broj " + frameNumber, 20, 50);
    setBackground (Color.white);
    g.setColor (Color.blue);
    Dimension d = getSize();
    if (frameNumber % 2 == 1) {
    // On odd frames, draw the ball at the top.
    g.fillOval (70, 0, 20, 20);
    }
    else {
    g.fillOval (70, 90, 20, 20);
    }
    }
    }

    It should be made into a rabbit (instead of ball) which will ran on the drawn path and counter will count his circuits to simulate For loop. Can anybody help, please...?

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by mneskovic View Post
    ...
    It should be made into a rabbit (instead of ball) ..
    In which case finding the rabbit could be a plausible first step, no?
    An image should be acceptable.

  4. #4
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Red face

    Here is one rabbit. It is JPG format, I think it is acceptable. Is it good?


  5. #5
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    This is maybe better :rolleyes:


  6. #6
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Thumbs up

    Quote Originally Posted by mneskovic View Post
    This is maybe better :rolleyes:

    gif rabbit is cool
    :D
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  7. #7
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    I have succeed to put the image. But the problem is to make it moving around the circle. Can you, please, help me? :)

  8. #8
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    It is better to use Circles instead Ellipses, because then you can calculate the positions easier.

    But the problem is to make it moving around the circle
    Change Ellipses to Circles first, and then with using mathematical calculation you can find
    new coordinates for new positions of rabbit.

  9. #9
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    Can you be little bit more precise? I really don't know how to write (I suppose) methods that will make my image move. Can you give me a simple example of that? It will be very helpful for me :o And can those calculations be used for ellipsis?

    Is it maybe better solution for me to draw a rabbit (the rabbit is on my imported image) and then make lines (that make it) move somehow simulating his moving? Can you help in any of these problems? I would be very grateful :)

  10. #10
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    well first of all you should get your image location... that is a point to start from.

    It is all depends of the layout managers you use for that.
    I recommend a JLayeredPane so you can set your image to JLabel and set it location as you wish ;)

    something like that (just to refresh my fingers :D )

    Java Code:
    JLayeredPane layeredPane=new JLayeredPane();
    
    --method A body (start)...
    ImageIcon icon=new ImageIcon(image);
    JLabel imagel=new JLabel();
    imagel.setIcon(icon);
    imagel.setBounds(x,y,width,height);
    layeredPane.add(imagel);
    --method A body (end)...
    and action...

    Java Code:
    --method B body(start)...
    imagel.setLocation(x,y);
    --method B body (end)...
    I provide a pseudocode here... I dearly hope you find out how to put in params and loops here ;)
    Last edited by Webuser; 05-10-2010 at 05:17 AM.
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  11. #11
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    I really don't know how to put loops and to continue code :(

  12. #12
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    Quote Originally Posted by mneskovic View Post
    I really don't know how to put loops and to continue code :(
    It is simply... If you need the rabbit to move some limited number of jumps you can write a for loop right on init() applet or actionPerformed methods...

    but if you don't know what loop and params are I do recommend you to read "Java fundamental" studying book first.
    Last edited by Webuser; 05-11-2010 at 12:55 AM.
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

Similar Threads

  1. Help with creating an applet
    By josephdcoleman in forum New To Java
    Replies: 2
    Last Post: 02-23-2009, 11:50 PM
  2. Looking for team members for creating the applet game.
    By M77 in forum Reviews / Advertising
    Replies: 10
    Last Post: 05-28-2008, 08:25 AM
  3. creating image using java
    By mmahesh_mca in forum New To Java
    Replies: 1
    Last Post: 04-26-2008, 11:23 PM
  4. Creating Calendar in Applet
    By wco5002 in forum New To Java
    Replies: 2
    Last Post: 04-09-2008, 04:21 AM
  5. Creating a Downloader using JAVA
    By shinojkk in forum New To Java
    Replies: 0
    Last Post: 01-08-2008, 05:08 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
  •