Need to convert JApplet to JFrame

I need to write code for where I have 60 balls bouncing around inside a window. The client will then be able to select a button and it will pull out a ball with the number 1-60 written on it. The user will be able to do this up to 7 times. Each time it is done the past numbers that have already appeared can not reappear. What I am stuck on right now is geting my balls into a JFrame. Can anyone give advice or show how to. I currently have my 60 balls running in a JApplet. Here is the JAVA code and the HTML code. Thanks!
Here is the JAVA code
import java.awt.*;
import java.applet.*;
import java.util.*;
import javax.swing.*;
class CollideBall{
int width, height;
public static final int diameter=20;
//coordinates and value of increment
double x, y, xinc, yinc, coll_x, coll_y;
boolean collide;
Color color;
Graphics g;
Rectangle r;
//the constructor
public CollideBall(int w, int h, int x, int y, double xinc, double yinc, Color c){
width=w;
height=h;
this.x=x;
this.y=y;
this.xinc=xinc;
this.yinc=yinc;
color=c;
r=new Rectangle(150,80,130,90);
public double getCenterX() {return x+diameter/2;}
public double getCenterY() {return y+diameter/2;}
public void alterRect(int x, int y, int w, int h){
r.setLocation(x,y);
r.setSize(w,h);
public void move(){
if (collide){  
double xvect=coll_x-getCenterX();
double yvect=coll_y-getCenterY();
if((xinc>0 && xvect>0) || (xinc<0 && xvect<0))
xinc=-xinc;
if((yinc>0 && yvect>0) || (yinc<0 && yvect<0))
yinc=-yinc;
collide=false;
x+=xinc;
y+=yinc;
//when the ball bumps against a boundary, it bounces off
if(x<6 || x>width-diameter){
xinc=-xinc;
x+=xinc;
if(y<6 || y>height-diameter){
yinc=-yinc;
y+=yinc;
//cast ball coordinates to integers
int x=(int)this.x;
int y=(int)this.y;
//bounce off the obstacle
//left border
if(x>r.x-diameter&&x<r.x-diameter+7&&xinc>0&&y>r.y-diameter&&y<r.y+r.height){
xinc=-xinc;
x+=xinc;
//right border
if(x<r.x+r.width&&x>r.x+r.width-7&&xinc<0&&y>r.y-diameter&&y<r.y+r.height){
xinc=-xinc;
x+=xinc;
//upper border
if(y>r.y-diameter&&y<r.y-diameter+7&&yinc>0&&x>r.x-diameter&&x<r.x+r.width){
yinc=-yinc;
y+=yinc;
//bottom border
if(y<r.y+r.height&&y>r.y+r.height-7&&yinc<0&&x>r.x-diameter&&x<r.x+r.width){
yinc=-yinc;
y+=yinc;
public void hit(CollideBall b){
if(!collide){
coll_x=b.getCenterX();
coll_y=b.getCenterY();
collide=true;
public void paint(Graphics gr){
g=gr;
g.setColor(color);
//the coordinates in fillOval have to be int, so we cast
//explicitly from double to int
g.fillOval((int)x,(int)y,diameter,diameter);
g.setColor(Color.white);
g.drawArc((int)x,(int)y,diameter,diameter,45,180);
g.setColor(Color.darkGray);
g.drawArc((int)x,(int)y,diameter,diameter,225,180);
public class BouncingBalls extends Applet implements Runnable {
Thread runner;
Image Buffer;
Graphics gBuffer;
CollideBall ball[];
//Obstacle o;
//how many balls?
static final int MAX=60;
boolean intro=true,drag,shiftW,shiftN,shiftE,shiftS;
boolean shiftNW,shiftSW,shiftNE,shiftSE;
int xtemp,ytemp,startx,starty;
int west, north, east, south;
public void init() {  
Buffer=createImage(getSize().width,getSize().height);
gBuffer=Buffer.getGraphics();
ball=new CollideBall[MAX];
int w=getSize().width-5;
int h=getSize().height-5;
//our balls have different start coordinates, increment values
//(speed, direction) and colors
for (int i = 0;i<60;i++){
ball=new CollideBall(w,h,50+i,20+i,1.5,2.0,Color.white);
/* ball[1]=new CollideBall(w,h,60,210,2.0,-3.0,Color.red);
ball[2]=new CollideBall(w,h,15,70,-2.0,-2.5,Color.pink);
ball[3]=new CollideBall(w,h,150,30,-2.7,-2.0,Color.cyan);
ball[4]=new CollideBall(w,h,210,30,2.2,-3.5,Color.magenta);
ball[5]=new CollideBall(w,h,360,170,2.2,-1.5,Color.yellow);
ball[6]=new CollideBall(w,h,210,180,-1.2,-2.5,Color.blue);
ball[7]=new CollideBall(w,h,330,30,-2.2,-1.8,Color.green);
ball[8]=new CollideBall(w,h,180,220,-2.2,-1.8,Color.black);
ball[9]=new CollideBall(w,h,330,130,-2.2,-1.8,Color.gray);
ball[10]=new CollideBall(w,h,330,10,-2.1,-2.0,Color.gray);
ball[11]=new CollideBall(w,h,220,230,-1.2,-1.8,Color.gray);
ball[12]=new CollideBall(w,h,230,60,-2.3,-2.5,Color.gray);
ball[13]=new CollideBall(w,h,320,230,-2.2,-1.8,Color.gray);
ball[14]=new CollideBall(w,h,130,300,-2.7,-3.0,Color.gray);
ball[15]=new CollideBall(w,h,210,90,-2.0,-1.8,Color.gray);*/
public void start(){
if (runner == null) {
runner = new Thread (this);
runner.start();
/* public void stop(){
if (runner != null) {
runner.stop();
runner = null;
public void run(){
while(true) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
try {runner.sleep(15);}
catch (Exception e) { }
//move our balls around
for(int i=0;i<MAX;i++)
ball[i].move();
handleCollision();
repaint();
boolean collide(CollideBall b1, CollideBall b2){
double wx=b1.getCenterX()-b2.getCenterX();
double wy=b1.getCenterY()-b2.getCenterY();
//we calculate the distance between the centers two
//colliding balls (theorem of Pythagoras)
double distance=Math.sqrt(wx*wx+wy*wy);
if(distance<b1.diameter)
return true;
return false;
private void handleCollision()
//we iterate through all the balls, checking for collision
for(int i=0;i<MAX;i++)
for(int j=0;j<MAX;j++)
if(i!=j)
if(collide(ball[i], ball[j]))
ball[i].hit(ball[j]);
ball[j].hit(ball[i]);
public void update(Graphics g)
paint(g);
public void paint(Graphics g)
gBuffer.setColor(Color.lightGray);
gBuffer.fillRect(0,0,getSize().width,getSize().height);
gBuffer.draw3DRect(5,5,getSize().width-10,getSize().height-10,false);
//paint our balls
for(int i=0;i<MAX;i++)
ball[i].paint(gBuffer);
g.drawImage (Buffer,0,0, this);
Here is the HTML code
<html>
<body bgcolor="gray">
<br><br>
<div align="center">
<applet code="BouncingBalls.class" width="1000" height="650"></applet>
</div>
</body>
</html>

In the future, Swing related questions should be posted in the Swing forum.
First you need to convert your custom painting. This is done by overriding the paintComponent() method of JComponent or JPanel. Read the Swing tutorial on [Custom Painting|http://java.sun.com/docs/books/tutorial/uiswing/TOC.html].
If you need further help then you need to create a [Short, Self Contained, Compilable and Executable, Example Program (SSCCE)|http://homepage1.nifty.com/algafield/sscce.html], that demonstrates the incorrect behaviour.
Don't forget to use the [Code Formatting Tags|http://forum.java.sun.com/help.jspa?sec=formatting], so the posted code retains its original formatting.

Similar Messages

  • Please let me know what steps need to take to convert JApplet to JFrame

    I have an application which needs to convert from JApplet to JFrame....Please let me know what are the steps need to take for this

    I have an application which needs to convert from
    JApplet to JFrame....The topic of this forum is web-start, and it
    can be used to launch both applets and
    applications. An applet deployed using
    web-start avoids a lot of the browser related
    problems that affect 'embedded' applets.
    This leads me to..
    Please let me know what are the steps need to take for this You do not need to convert it, to launch it
    using web-start, but if you are determined to
    make it into a frame, it would be best to ask
    how to do that on a forum where the subject
    being talked about, is closer to what you are
    doing.
    For more closely related forums, try here
    http://forum.java.sun.com/category.jspa?categoryID=5
    (More the first two of the Available Forums:)

  • I need to convert PDF file to Word Document, so it can be edited. But the recognizing text options do not have the language that I need. How I can convert the file in the desired of me language?

    I need to convert PDF file to Word Document, so it can be edited. But the recognizing text options do not have the language that I need. How I can convert the file in the desired of me language?

    The application Acrobat provides no language translation capability.
    If you localize the language for OS, MS Office applications, Acrobat, etc to the desired language try again.
    Alternative: transfer a copy of content into a web based translation service (Bing or Google provides a free service).
    Transfer the output into a word processing program that is localized to the appropriate language.
    Do cleanup.
    Be well...

  • Need to convert to .img file

    I have an image, currently in .bmp format, but can be changed to .jpg or .tiff. I need to convert it to .img. I'm running CS6 on a Mac.

    Are you sure you don't mean img in the file name?
    Some cameras create file name like: img001.jpg, img002.jpeg, etc.
    Googling .img points to some type of image in AutoCAD?
    I find it hard to believe that AutoCAD cannot work with .jpg or tiff.
    .JPG is pretty universally accepted by all applications that deal with images.
    Who is asking you for an image with the .img file extension and what are they planning to do with it, (use in what application)?

  • Need to convert language from English to Thailand

    Hi Gurus,
    Can any one let me know the procedure of language conversion,
    like i need to convert the available statement in report from English to Thailand.
    Is their any function module to convert, please let me know.
    thanks
    balu.

    Hi,
    Do you want text in Thailand , when your logon language is English that to dynamic text ? is it so.
    If not  I think  you can do this using Translation  .
    Mintain text-elements and then GOTO -> Translation.
    When you logon in that particular language then it will be displayed in that language only.
    Regards,
    Rajitha.

  • Need to convert byte[] to image

    hi frnds,
    i have a byte[](which i got it from an image) i need to convert back to image.Could u pls suggest me how to??
    i tried with Pixelgrabber but i exaclty did get wht shld be the input for it(tried giving image file name). :(
    So pls let me know how and wht to use..ASAP.
    Regards
    subinjava

    Subinjava wrote:
    ..i have a byte[](which i got it from an image) i need to convert back to image.Could u pls suggest me how to??
    Image image = Toolkit.getDefaultToolkit().createImage(byteArray)See http://java.sun.com/javase/6/docs/api/java/awt/Toolkit.html#createImage(byte[]) for more details.

  • Need to convert .wmv to .mov. Any software suggestions?

    Anyone using software to convert files to incorp. into their iMovie projects? I need to convert some .wmv files to .mov or .mpeg. Any suggestions?
    Thanks
    Ed

    Not easy or cheap:
    http://www.danslagle.com/mac/iMovie/qt_plugins/3002.shtml

  • Need to convert 720p video from 240 to 480

    I have some video files that were recorded at 720x240, that I need to convert to 720x480. Any suggestions for a free Mac-friendly converter?
    Thanks!

    Try enolsoft video converter which supports to specify the video parameters and quality. You can set the resolution as 720*480.

  • Need to convert labview files from version 4.0 to 8.6

    I have a few labview .iv files that I need to convert to version 8.6.  They were created in version 4.0.  Can I download an intermediate verison of labview to process the conversion?  Or can i order a cd?

    Typically, you would need access to several version of LabVIEW to do the conversion.
    Open the 4.0 VI in 5.1, save.
    Open the 5.1 VI in 7.1, save.
    Open the 7.1 VI in 8.6, save.
    This example uses the most popular versions - ones you are most likely to encounter. 6.i was also popular, and might work somewhere in there.
    Richard

  • Need to convert original language to EN in SAPscript

    Hi all
    I have copied a standard form and need to convert the language to EN but when I go to Utilities the option of convert original language is disable.
    How can I go for it.
    Regrds
    Mona

    Hi,
    Goto SE71 of your script and identify the original language in header details.
    Now goto SE71, form name with original language and click change, usually if you check the radio button trnslate to all languages it is available in all languages... now if you want the form to be avialable in only few languages, click radio button to individual languages and click arror language selelction and select ES... and EN now form will be available in EN and Spanish...save and activate.
    Or
    Go to se71->changeform->form menu->copy from.
    u will find the option to change langauage.
    Regards,
    Satish
    Edited by: Satish Panakala on Apr 17, 2008 12:51 PM

  • Need to convert DTS packages to SQL Server 2012 environment.

    Hi Friends,
    As Part of the Project Requirement,we need to convert DTS packages to SQL Server 2012 environment.
    Please sugest me best approach.
    Thanks

    Best approach is to re-design the packages in SSIS 2012 directy if it is feasible. Because most of the DTS feaures are discontinued and also you will have to clean up all errors that you are going to get after migration.
    Discontinued Integration Services Functionality in SQL Server 2012
    Otherwise you need to migrate it first to 2008R2 and then 2012 version.
    Below ones would be helpful for you:
    Migrate DTS packages to SSIS (SSIS2008R2)
    Support of DTS packages in 2008 R2
    Known DTS Package Migration Issues
    Cheers,
    Vaibhav Chaudhari

  • Need to convert VI from 8.0 - 7.1 - 7.0

    I have an 8.0 VI that I need to convert to version 7.0.  Looks like 8.0 can only convert to 7.1. Does anyone know where I can download the evaluation for 7.1 so I can do the 7.1 -> 7.0 conversion?
    Thanks.

    There is no evaluation version for 7.1.  Typically you only get eval versions for the ".0" releases...6.0, 7.0, 8.0, etc.  If it's only a few VIs, you can post your 7.1 VI(s) on these forums and someone will surely convert them for you.
    -D
    Darren Nattinger, CLA
    LabVIEW Artisan and Nugget Penman

  • Need to convert an image to .jpeg using Java !

    Hello,
    i am working in images now. i need to convert any image in the form of .jpeg using Java. in other words, i need to store an image in the form of .jpeg format. can any of u help me ! thanks in advance.
    - Krishna

    There's also jimi, at http://java.sun.com/products/jimi/
    You can do something like:
    image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);
    // create your image
    String path = "/path/image.jpg";
    Jimi.putImage(image, path);
    [\code]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Need to convert over video 100 files to iPod quality files

    i have over 100 .mov files that i need to convert to mp4 in order to play on my Palm Treo 700p...
    each original file is approx 78 to 81 megs... about 39 to 40 minutes of video. i am converting ONE file now and it told me it would take about 1 hour 55 minutes to convert it to the "convert to iPod" preset
    at that rate, it will take forever to convert all these files... is there a faster way? or better settings to use in quicktime?
    they don't have to be extremely high quality, they are lessons for bible classes i am taking, but i'd like the audio to be decent and for the video to be clear and not fuzzy...
    thanks for any help you can provide.

    I have both VisualHub and MPEG Streamclip.
    At the moment, I only use VisualHub to handle a few quirky .mpg files which MPEG Streamclip had problems with (data breaks that aren't fixed) and FLV file conversion (because I don't have the new beta version of S'clip, which supports FLV).
    But as Rick said, MPEG Streamclip works wonders for pretty much everything else. I still would recommend that first before VisualHub.
    Steve

  • Need to convert to vector format.

    I have a document with pictures.  I need to convert it to vector format before sending to printer.  Not sure how to do it.  Using CS5.  Is converting to outlines the same? 

    It must be difficult to convert from illustrator into a vector format since no one has a specific answer.
    Soshagayle7,
    There is no automagic "conversion" from a raster image to a vector graphic. The artwork represented by a raster image can be redrawn as vector artwork.
    You can re-draw the artwork using the vector drawing tools in Illustrator or any similar program. You can even import the raster image and use it as a guide while "tracing" it with vector paths.
    Or (and this is where the all-too-common misconception about "conversion" comes in)...
    You can use the autotrace feature which exists in Illustrator or any similar program to try to automate the process of "tracing" the raster image with vector paths.
    Either way, you end up with what would more accurately be called a "reinterpretation" of the subject, not a "conversion" of the raster image in the sense of "converting" one file format to another.
    And that's the key. All that most current autotrace features (including the one in Illustrator) do is detect color differences between pixels of a raster image based upon a user-specified sensitivity setting, and then try to draw vector paths which follow along those detected differences. It's very much a garbage-in-garbage-out process.
    And even when the "in" is not garbage, there is no real intelligence involved. The kind of autotrace algorithms in Illustrator and programs like it have no shape-recognition intelligence. For example, in the case of a human face, the autotrace feature doesn't know that the eyes' pupils are round; it just detects a region of similarly-colored pixels and tries to draw a path around them. Similarly, in the case of a geometric logo that obvously (to a human) is supposed to contain a perfect circle, the autotrace feature doesn't "see a circle"; it doesn't go and get the Ellipse tool and draw a circle that fits, as any human would do. Again, it just tries to follow around the regions of similarly-colored pixels.
    But that sounds okay, right? Well think about it. Imagine turning the sensitivity of such an algorithm way up to the max. What's going to be the most accurate autotracing of a raster image? The mathematically "most accurate" result would be a perfect vector square for each and every pixel in the image. And the "vector advantage" of that would be absolutely nill. The resolution-independence reason for preferring vector paths would be rendered moot. Such a graphic would be entirely vector, but to absolutely no advantage regarding scaleability. (Thus my comment about the line drawn with a 1-pixel raster image.)
    So the reason you haven't received an answer to the "how do I convert" question is because the same thing has been explained in this forum countless times, and to answer it correctly really requires a lengthy explanation (like this one) that tries to clear up the too-common misconception that autotracing is some kind of magic bullet for "converting" a raster image into a vector graphic in some kind of mathematically accurate way that then yields all the advantages of properly drawn vector graphics.
    Autotracing is useful to those who understand when it's appropriate and why. But without seeing or at least knowing more about the actual raster image you are dealing with, advising whether it's appropriate in your case is nothing but a guess. Generally speaking, if the image in question is not already of high enough resolution at the size at which it will be printed, then it's probably also not of high enough resolution for good auto-tracing results. And if it is of sufficient resolution for the size at which it will be printed, then there's probably no reason it needs to be vector.
    Bottom line: There's a good reason why vector graphics are preferred. But that means properly-drawn vector graphics. The best way to "convert" your raster image to a vector graphic is to re-draw it using the vector tools. The sometimes-acceptable but often sub-standard "cheat" for doing it is to import the graphic and apply the autotrace feature (called LiveTrace in Illustrator). It's use is explained in the online help.
    Autotracing--the "conversoin" that such questions are almost always talking about--is not a lossless translation, like converting quarts to gallons or binary to hexidecimal. Entropy always rules. Anytime something is automatically "reprocessed" something is lost, not gained. You see that in everything from repainting a room over and over without removing the old paint, to taking photographs of photographs of photographs. Degradation occurs. You swap one kind of ugliness for another. That's autotracing when used inappropriately.
    The drawing tools exist for a reason. If you really need vector paths, you should probably draw them.
    JET

Maybe you are looking for