Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-07-2008, 03:34 AM
Member
 
Join Date: Feb 2008
Posts: 10
anotsu is on a distinguished road
how can i turn this java program to an applet
Hello everybody ,
i would like to know how can i turn this java program( that bounce a ball) into an java applet that can be launch form a html doc
thank you in advance




import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;


public class AnimeBall {
Motivator fullMover;
Motivator fadeMover;

private void start() {
fullMover.start();
fadeMover.start();
}

private JTabbedPane getContent() {
FullTrail fullTrail = new FullTrail();
fullMover = new Motivator(fullTrail);
FadingTrail fader = new FadingTrail();
fadeMover = new Motivator(fader);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("full trail", fullTrail);
tabbedPane.addTab("fading trail", fader);
return tabbedPane;
}

public static void main(String[] args) {
AnimeBall trails = new AnimeBall();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(trails.getContent());
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
trails.start();
}
}
class FullTrail extends JPanel implements Movable {
BufferedImage offscreen;
Point loc;
int dx = 3;
int dy = 2;
final int D = 30;

protected void paintComponent(Graphics g) {
if(offscreen == null) {
initOffscreen();
}
g.drawImage(offscreen, 0, 0, this);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.red);
g2.fillOval(loc.x-D/2, loc.y-D/2, D, D);
}

public void moveBall() {
updateOffscreen();
checkBoundries();
loc.x += dx;
loc.y += dy;
repaint();
}

private void checkBoundries() {
if(loc.x - D/2 + dx < 0 || loc.x + D/2 + dx > getWidth())
dx *= -1;
if(loc.y - D/2 + dy < 0 || loc.y + D/2 + dy > getHeight())
dy *= -1;
}

private void initOffscreen() {
int w = getWidth();
int h = getHeight();
loc = new Point(w/2, h/2);
offscreen = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = offscreen.createGraphics();
g2.setColor(UIManager.getColor("Panel.background") );
g2.fillRect(0,0,w,h);
g2.dispose();
}

private void updateOffscreen() {
Graphics g = offscreen.getGraphics();
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(new Color(240,200,240));
g2.fillOval(loc.x-2, loc.y-2, 4, 4);
g2.dispose();
}
}

class FadingTrail extends JPanel implements Movable {
Point[] trail;
Point loc;
int dx = 2;
int dy = 3;
final int D = 30;

public FadingTrail() {
int trailLength = 20;
trail = new Point[trailLength];
for(int j = 0; j < trail.length; j++) {
trail[j] = new Point();
}
loc = new Point(200,100);
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);
Graphics2D copy = (Graphics2D)g.create();
copy.setPaint(new Color(240,200,240));
float alpha = 1f;
float alphaInc = 1f/20.5f;
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER , alpha);
for(int j = 0; j < trail.length; j++) {
copy.setComposite(ac);
copy.fillOval(trail[j].x-2, trail[j].y-2, 4, 4);
alpha -= alphaInc;
ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER , alpha);
}
copy.dispose();
g2.setPaint(Color.red);
g2.fillOval(loc.x-D/2, loc.y-D/2, D, D);
}

public void moveBall() {
updateTrail();
checkBoundries();
loc.x += dx;
loc.y += dy;
repaint();
}

private void checkBoundries() {
if(loc.x - D/2 + dx < 0 || loc.x + D/2 + dx > getWidth())
dx *= -1;
if(loc.y - D/2 + dy < 0 || loc.y + D/2 + dy > getHeight())
dy *= -1;
}

private void updateTrail() {
for(int j = trail.length-1; j > 0; j--) {
trail[j].setLocation(trail[j-1].getLocation());
}
double theta = Math.atan2(dy,dx) + Math.PI;
int x = loc.x + (int)((D/2)*Math.cos(theta));
int y = loc.y + (int)((D/2)*Math.sin(theta));
trail[0].setLocation(x, y);
}
}

interface Movable {
public void moveBall();
}

class Motivator implements Runnable {
Movable movable;
Thread thread;
boolean keepMoving;

public Motivator(Movable m) {
movable = m;
}

public void run() {
while(keepMoving) {
try {
Thread.sleep(50);
} catch(InterruptedException e) {
keepMoving = false;
}
movable.moveBall();
}
}

public void start() {
if(!keepMoving) {
keepMoving = true;
thread = new Thread(this);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
}

public void stop() {
keepMoving = false;
thread.interrupt();
thread = null;
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-07-2008, 05:56 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
public class AnimeBallApplet extends JApplet { Motivator fullMover; Motivator fadeMover; public void init() { getContentPane().add(getContent()); } public void start() { fullMover.start(); fadeMover.start(); } public void stop() { fullMover.stop(); fadeMover.stop(); } private JTabbedPane getContent() { FullTrail fullTrail = new FullTrail(); fullMover = new Motivator(fullTrail); FadingTrail fader = new FadingTrail(); fadeMover = new Motivator(fader); JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.addTab("full trail", fullTrail); tabbedPane.addTab("fading trail", fader); return tabbedPane; } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-09-2008, 03:03 AM
Member
 
Join Date: Feb 2008
Posts: 10
anotsu is on a distinguished road
thank you for your answer
however do you know how can this applet be executed in an html document ?

thank you very much
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-09-2008, 04:00 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Deploying Applets
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-11-2008, 12:28 AM
Member
 
Join Date: Feb 2008
Posts: 10
anotsu is on a distinguished road
i dont know what is wrong in my applet but it seems to not work, can you help me please?

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.JApplet;

public class AnimeBall extends JApplet {
Motivator fullMover;
Motivator fadeMover;

public void init() {
getContentPane().add(getContent());
}

public void start() {
fullMover.start();
fadeMover.start();
}

public void stop() {
fullMover.stop();
fadeMover.stop();
}

private JTabbedPane getContent() {
FullTrail fullTrail = new FullTrail();
fullMover = new Motivator(fullTrail);
FadingTrail fader = new FadingTrail();
fadeMover = new Motivator(fader);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("full trail", fullTrail);
tabbedPane.addTab("fading trail", fader);
return tabbedPane;
}
}


public class AnimeBall {
Motivator fullMover;
Motivator fadeMover;

private void start() {
fullMover.start();
fadeMover.start();
}

private JTabbedPane getContent() {
FullTrail fullTrail = new FullTrail();
fullMover = new Motivator(fullTrail);
FadingTrail fader = new FadingTrail();
fadeMover = new Motivator(fader);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("full trail", fullTrail);
tabbedPane.addTab("fading trail", fader);
return tabbedPane;
}

public static void main(String[] args) {
AnimeBall trails = new AnimeBall();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(trails.getContent());
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
trails.start();
}
}
class FullTrail extends JPanel implements Movable {
BufferedImage offscreen;
Point loc;
int dx = 3;
int dy = 2;
final int D = 30;

protected void paintComponent(Graphics g) {
if(offscreen == null) {
initOffscreen();
}
g.drawImage(offscreen, 0, 0, this);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.red);
g2.fillOval(loc.x-D/2, loc.y-D/2, D, D);
}

public void moveBall() {
updateOffscreen();
checkBoundries();
loc.x += dx;
loc.y += dy;
repaint();
}

private void checkBoundries() {
if(loc.x - D/2 + dx < 0 || loc.x + D/2 + dx > getWidth())
dx *= -1;
if(loc.y - D/2 + dy < 0 || loc.y + D/2 + dy > getHeight())
dy *= -1;
}

private void initOffscreen() {
int w = getWidth();
int h = getHeight();
loc = new Point(w/2, h/2);
offscreen = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = offscreen.createGraphics();
g2.setColor(UIManager.getColor("Panel.background") );
g2.fillRect(0,0,w,h);
g2.dispose();
}

private void updateOffscreen() {
Graphics g = offscreen.getGraphics();
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(new Color(240,200,240));
g2.fillOval(loc.x-2, loc.y-2, 4, 4);
g2.dispose();
}
}

class FadingTrail extends JPanel implements Movable {
Point[] trail;
Point loc;
int dx = 2;
int dy = 3;
final int D = 30;

public FadingTrail() {
int trailLength = 20;
trail = new Point[trailLength];
for(int j = 0; j < trail.length; j++) {
trail[j] = new Point();
}
loc = new Point(200,100);
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);
Graphics2D copy = (Graphics2D)g.create();
copy.setPaint(new Color(240,200,240));
float alpha = 1f;
float alphaInc = 1f/20.5f;
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER , alpha);
for(int j = 0; j < trail.length; j++) {
copy.setComposite(ac);
copy.fillOval(trail[j].x-2, trail[j].y-2, 4, 4);
alpha -= alphaInc;
ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER , alpha);
}
copy.dispose();
g2.setPaint(Color.red);
g2.fillOval(loc.x-D/2, loc.y-D/2, D, D);
}

public void moveBall() {
updateTrail();
checkBoundries();
loc.x += dx;
loc.y += dy;
repaint();
}

private void checkBoundries() {
if(loc.x - D/2 + dx < 0 || loc.x + D/2 + dx > getWidth())
dx *= -1;
if(loc.y - D/2 + dy < 0 || loc.y + D/2 + dy > getHeight())
dy *= -1;
}

private void updateTrail() {
for(int j = trail.length-1; j > 0; j--) {
trail[j].setLocation(trail[j-1].getLocation());
}
double theta = Math.atan2(dy,dx) + Math.PI;
int x = loc.x + (int)((D/2)*Math.cos(theta));
int y = loc.y + (int)((D/2)*Math.sin(theta));
trail[0].setLocation(x, y);
}
}

interface Movable {
public void moveBall();
}

class Motivator implements Runnable {
Movable movable;
Thread thread;
boolean keepMoving;

public Motivator(Movable m) {
movable = m;
}

public void run() {
while(keepMoving) {
try {
Thread.sleep(50);
} catch(InterruptedException e) {
keepMoving = false;
}
movable.moveBall();
}
}

public void start() {
if(!keepMoving) {
keepMoving = true;
thread = new Thread(this);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
}

public void stop() {
keepMoving = false;
thread.interrupt();
thread = null;
}
}
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 03-11-2008, 01:48 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
C:\jexp\AB>javac animeball.java animeball.java:38: duplicate class: AnimeBall public class AnimeBall { ^ 1 error
You have an extra AnimeBall class below the AnimeBall applet:
Code:
public class AnimeBall { Motivator fullMover; Motivator fadeMover; private void start() { fullMover.start(); fadeMover.start(); } private JTabbedPane getContent() { FullTrail fullTrail = new FullTrail(); fullMover = new Motivator(fullTrail); FadingTrail fader = new FadingTrail(); fadeMover = new Motivator(fader); JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.addTab("full trail", fullTrail); tabbedPane.addTab("fading trail", fader); return tabbedPane; } public static void main(String[] args) { AnimeBall trails = new AnimeBall(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(trails.getContent()); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); trails.start(); } }
You should remove this extra class.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute an External Program through Java program Java Tip java.io 0 04-04-2008 04:40 PM
turn off auto compile bugger Eclipse 3 01-04-2008 10:12 PM
Convert a program to Applet carl Java Applets 2 08-09-2007 11:33 PM
Export to Java program, applet or c/c++ fernando New To Java 1 08-07-2007 08:55 AM
To turn a view jsp to a pdf file Daniel JavaServer Pages (JSP) and JSTL 1 07-05-2007 08:23 PM


All times are GMT +3. The time now is 12:39 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org