• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating Java applet

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code is all I have:

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...?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What ideas have you had? What do you think needs to be changed, and how?
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to put a picture of rabbit instead of this ball, and the rabbit should be running around the track simulating running in circle and making loop. The counter should count those circles. Do you have any idea how to do it? As you can see, I'm new in Java programming and I really don't know how to do it, but I have to make this... I would be very grateful
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have the rabbit as an image, then you can use Graphics.drawImage to draw it. You can obtain an Image object through various means, the easiest probably being AppletContext.getImage.

You'll have to find a way to keep track of where the rabbit should be (maybe divide its path around the circle in 100 steps), and then figure out how to get from numbers 0..100 to (X,Y) positions around the circle.

Once you've counted up to 100 (meaning the rabbit has been around the circle), increment the counter and display it using Graphics.drawString.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found an image. Do you think it's ok? Can you please explain me in more detail how to do the rest?

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I found an image. Do you think it's ok?


Um, if you don't know that, how can we? It's your project, after all... It won't be animated once it's drawn on a Graphics object, though.

Can you please explain me in more detail how to do the rest?


This sounds like an assignment that you should be working on in order to learn something, so we don't want to rob you of the opportunity to do so by giving away too much. I'd break this down in smaller steps, maybe start with writing an applet that loads an image and display it at arbitrary screen locations. The CodeBarn has an example applet that does something like that. Then you could write math functions that provide you with (X,Y) coordinates along a circle.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My professor gave me this assignment because I am a good student, but he knows that I'm not familiar with Java. The simulation doesn't have to be complicated. He needs this for showing students in simulating For loop in programming language C and he actually made me to do this. That's why I need help with this

I found the displayed image on the Internet, I have only it's URL. Can it be imported in applet that is created in NetBeans? I have looked at this site, but I don't understand this:

1. Create an instance of Image
2. Create an instance of MediaTracker
3. Add the Image to the MediaTracker instance
4. Call the MediaTracker object's waitForAll() method. The method will block until the image is fully downloaded.
5. Override the applet's paint() method, and add a method call to drawImage().

It's so complicated... I hope I didn't confused you too and THANK YOU very much

Best regards!
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The steps 1 through 5 are what that applet does; it's very short, so it shouldn't be hard to correlate those steps with the lines of code that carry out those steps. Reading the javadocs of the involved methods and classes will also help.

It makes no difference to the working of the applet if you create it using some IDE or in a text editor. In order for an applet to load images, those images will need to be located in your server, though, along with the applet class files.

Have you worked with applets before, so that you know their lifecycle, how to deploy them, and which methods do what? If not, start here: http://java.sun.com/docs/books/tutorial/deployment/applet/
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have read this tutorial, but I didn't understand all. My code doesn't have separately defined classes, everything is in the applet I put the picture in the folder with applet, but I get confused in writing code for image importing. It probably was a mistake...? I have no idea how to make something looking like wanted simulation. I haven't worked with applets nor with Java so far, I have only read in tutorials about applets. Thank you for caring about my problem
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you haven't worked with Java so far, I'd suggest to get an introductory book for getting started. You could also work through the Sun Java Tutorial (http://java.sun.com/docs/books/tutorial/), particularly the section Getting Started and Learning the Java Language (and possibly Essential Java Classes) before you start working on applets.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you I'm already reading about Java, but I have to make applet in parallel because it has to be made until Monday.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written this code:

package org.me.pack;
import java.applet.Applet;
import java.awt.*;
import java.net.*;
public class MyApplet extends Applet {
Font bigFont;
Color blueColor;
Color grayColor;
Color whiteColor;
Image rabbit;
URL base;
MediaTracker mt;
public void init() {
mt= new MediaTracker(this);
bigFont=new Font("Arial", Font.BOLD,20);
blueColor=Color.BLUE;
grayColor=Color.GRAY;
whiteColor=Color.WHITE;
try {
base=getDocumentBase();
}
catch (Exception e) {}
rabbit=getImage(base, "White_Rabbit1.gif");
mt.addImage(rabbit, 1);
try {
mt.waitForAll();
}
catch (InterruptedException e) {}
}
public void paint(Graphics g) {
g.setFont(bigFont);
g.setColor(blueColor);
g.drawString("FOR petlja",230,20);
g.setColor(grayColor);
g.drawOval(40, 100, 450, 300);
g.drawOval(70, 130, 390, 240);
g.fillOval(40, 100, 450, 300);
g.setColor(whiteColor);
g.fillOval(70, 130, 390, 240);
g.drawImage(rabbit, 40, 140, 60, 60, this);
}
}

It displays the image, but I don't know how to make image move. It should be rounding in a circle. Please, help me, I'm desperate
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please edit your post to UseCodeTags. It's unnecessarily hard to read the code as it is, making it less likely that people will bother to do so.

Earlier I wrote this:

You'll have to find a way to keep track of where the rabbit should be (maybe divide its path around the circle in 100 steps), and then figure out how to get from numbers 0..100 to (X,Y) positions around the circle.


That seems like a good next step, since currently the image is drawn at a fixed location.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, here is the code:



Do you have any idea how to do that circle divide and image moving in those little steps?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you really not indent your code?
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, no
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Do you have any idea how to do that circle divide and image moving in those little steps?


Let's say you have a variable "t" which runs from 0 to 100. Both 0 and 100 would represent the image being displayed at the top of the circle (at 12 o'clock), while 50 would mean to display the image at the bottom of the circle (at 6 o'clock), 25 means 3 o'clock, and 75 means 9 o'clock.

Now you need two functions that calculate x and y as a function of t, respectively. As a hint, both functions will have the diameter (or radius) of the circle in them, one of them will involve a cosine function, and the other will involve a sine function.

Does this help?
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand, but I how and where to implement these functions? Do you mean something like writing for loop that goes from 0 to 99 which will call functions?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you need to write a loop, and in each iteration the image will be drawn in a slightly different place from the previous iteration.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I'll try.

I have one more question: can I make simulation with buttons in simple Java applet or I need JApplet, or something else?

Greetings!
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use either. Applet is for applets that use the AWT toolkit, while JApplet is for applets that use the Swing toolkit.

The code above is AWT, so if you're basing your efforts on that, then you should stick with Applet.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what about JPanel and JApplet?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about them?
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between them? Should I use them?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should use JPanel and JApplet if you intend to use Swing; you should Panel and Applet of you intend to use AWT. If you're not sure what the difference between AWT and Swing is -and I suspect that you don't, since you're new to Java- you might as well stick with AWT, since the code you posted uses that. There's no sense in complicating the task at hand by switching to a different API.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for explanation. I have to make buttons also on my applet... Can it be done using only AWT?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure you can. I suggest to inspect the classes of the java.awt package to see what's available. You'll also find various AWT tutorials online.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much. I hope I can ask you again if I have problems... Greetings!
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello! I have written some code, but I did't know how to make image move, I mean I don't know how to write that For loop and how to include these defined threads... Here's the code:


Image is also flickering... I have written the update method and it doesn't stop flickering. Can you help please with all these problems?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I did't know how to make image move


The code passes fixed value to the drawImage method, so there's no way it can move, but I'm sure you know that. You need to program a loop that provides x and y coordinates in a circle, like I mentioned before; how far have you come with that?

If you want to avoid flickering, check out a technique called "double buffering"; searching for "awt double buffering" will find relevant pages. I'd leave that for last, once everything else is working.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is the thing I don't know - how to program coordinates in circle (or ellipsis) and how to make image moving, but not drawing on any point of that programmed circle...? Can you explain me that more detail, please?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a hint: If you want to draw a circle of radius RAD that is centered on the point (X0, Y0), then the following equation holds for all points (x, y) on the circle's circumference:

(x - X0) ^ 2 + (y - Y0) ^ 2 = RAD ^ 2

If you plug various values of x into this equation, then you can calculate the corresponding values of y. Maybe start with X0=100, Y0=100, RAD=50 and x values that run from 50 to 150.

If you are familiar with sine and cosine, then a better solution for calculating the coordinates can be found using those; a web search will find descriptions of that approach.
 
maja neskovic
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have succed to make something doing what I needed. Thank you for your help.

This is the code:



I have an assignment to make this applet connected with the database. I think to make connection with MySQL database. Can you explain me how to do it, please?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic