import java.applet.Applet;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

import javax.swing.JFrame;

public class StartingPoint extends Applet implements Runnable, KeyListener{

                     public static void main(String[] args) {
                            StartingPoint sp = new StartingPoint();
                            JFrame frame = new JFrame("My applet, as application");
                            frame.getContentPane().add(sp);
                            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                            sp.init();
                            sp.start();
                            frame.pack();
                            frame.show();
                        }


                //=========================
                //Varible section
                //=========================
                private Image i;            //Varible saying whats on the screen
                private Graphics doubleG;   //double buffer photocopy graphic
                                            //These are made private to make sure
                                            //Its overiding correctly.
                ball b;                //A ball named b.
                platform p[] = new platform[7];     // a platform array named p.
                Item item[] = new Item[3];
                Color skyblue = new Color(135,206,250);


                //================================
                //Runs first time program starts.
                //================================
                @Override
                public void init() {
                    //=====================
                    //presets applet size
                    //=====================
                    setSize(800, 600);
                    //========================
                    //Set backround to blue
                    //========================
                    setBackground(skyblue);

                    //===================================
                    //adds the implimented key listener
                    //===================================
                    addKeyListener(this);


                }

                //================================
                //Runs first time program starts
                //then again when called
                //================================

                @Override
                public void start() {
                    b = new ball();

                    //============================
                    //Create all platforms.
                    //============================
                    for (int i = 0; i < p.length; i++){

                         Random r = new Random();
                         p[i] = new platform(getWidth() + 200 * i, getHeight() - 40 - r.nextInt(400));
                    }

                    //===================================
                    //Create items
                    for (int i = 0; i < item.length; i++){

                         Random r = new Random();
                         switch (r.nextInt(9)){
                            case 0:
                                item[i] = new GravUp(getWidth() + r.nextInt(2000) * (2+i));
                                break;
                            case 1:
                                item[i] = new GravDown(getWidth() + r.nextInt(2000) * (2+i));
                                break;
                            case 2:
                                item[i] = new AgilUp(getWidth() + r.nextInt(2000) * (2+i));
                                break;
                            case 3:
                                item[i] = new AgilDown(getWidth() + r.nextInt(2000) * (2+i));
                                break;
                            case 4:
                                item[i] = new GravUp(getWidth() + r.nextInt(2000) * (2+i));
                                break;
                            case 5:
                                item[i] = new GravDown(getWidth() + r.nextInt(2000) * (2+i));
                                break;
                            case 6:
                                item[i] = new AgilUp(getWidth() + r.nextInt(2000) * (2+i));
                                break;
                            case 7:
                                item[i] = new AgilDown(getWidth() + r.nextInt(2000) * (2+i));
                                break;
                            case 8:
                                if(b.getBrake() == 0){
                                    item[i] = new BrakeOn(getWidth() + r.nextInt(2000) * (2+i));
                                }
                                else{
                                    item[i] = new BrakeOff(getWidth() + r.nextInt(2000) * (2+i));

                                }

                            }
                    }

                    Thread thread = new Thread(this);
                    thread.start();


                }

                @Override
                public void run() {
                    //Thread information


                    while (true){
                        Random r = new Random();

                        for (int i = 0; i< item.length; i++){
                            if (item[i].getY() == this.getHeight() + 100)
                                switch (r.nextInt(9)){
                                case 0:
                                    item[i] = new GravUp(getWidth() + r.nextInt(6000));
                                    break;
                                case 1:
                                    item[i] = new GravDown(getWidth() + r.nextInt(6000));
                                    break;
                                case 2:
                                    item[i] = new AgilUp(getWidth() + r.nextInt(6000));
                                    break;
                                case 3:
                                    item[i] = new AgilUp(getWidth() + r.nextInt(6000));
                                    break;
                                case 4:
                                    item[i] = new GravUp(getWidth() + r.nextInt(6000));
                                    break;
                                case 5:
                                    item[i] = new GravDown(getWidth() + r.nextInt(6000));
                                    break;
                                case 6:
                                    item[i] = new AgilUp(getWidth() + r.nextInt(6000));
                                    break;
                                case 7:
                                    item[i] = new AgilUp(getWidth() + r.nextInt(6000));
                                    break;
                                case 8:
                                    if(b.getBrake() == 0){
                                        item[i] = new BrakeOn(getWidth() + r.nextInt(6000));
                                    }
                                    else{
                                        item[i] = new BrakeOff(getWidth() + r.nextInt(6000));

                                    }                       

                                }

                        }
                        //=================
                        //Updates out ball
                        //================
                        b.update(this);
                        for (int i = 0; i < p.length; i++){
                            p[i].update(this, b);

                        }

                        //=================
                        //Updates out ball
                        //================
                        b.update(this);
                        for (int i = 0; i < item.length; i++){
                            item[i].update(this, b);

                        }





                        //=====================================
                        //Built in method calling update method
                        // which clears screen and calls paint method
                        //=====================================
                        repaint();

                        //================================
                        //Try to sleep catches if fail
                        //================================
                        try {
                            Thread.sleep(17);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }


                    }


                }


                //===========================
                //Stops the program
                //===========================
                @Override
                public void stop() {


                }

                //=====================
                //Terminates program.
                //=====================
                @Override
                public void destroy() {

                }

                //===============================
                //Allows access to update method
                //I am using this to double buffer
                //The screen to get rid of flickering
                //===============================


                //============================
                //Overides the update to 
                //Check for need for a 
                //double buffer
                //============================
                @Override
                public void update(Graphics g) {
                    //========================================================
                    //If screen is blank, create an image the size of the applet
                    //========================================================
                    if (i == null){
                        i = createImage(this.getSize().width, this.getSize().height);
                        doubleG = i.getGraphics();
                    }

                    //===============================
                    //Sets the grapic copy's backround
                    // color to 
                    //What ever is up.
                    //===============================
                    doubleG.setColor(getBackground());

                    //================================================
                    //Creats another rectangle of the same size
                    //and backround of app
                    //===================================================
                    doubleG.fillRect(0, 0, this.getSize().width, this.getSize().height);

                    //==================================
                    //Sets copy's foreground as the orignal's
                    //==================================
                    doubleG.setColor(getForeground());
                    //==============================
                    //Paints copy to screen
                    //=============================
                    paint(doubleG);

                    //======================================================
                    // draw image i at 0,0 (i is the varible on the screen)
                    //And its drawing it on this, refering to applet
                    //======================================================
                    g.drawImage(i, 0, 0, this);

                }



                @Override
                public void paint(Graphics g) {
                    b.paint(g);
                    for (int i = 0; i < p.length; i++){
                        p[i].paint(g);


                     }

                    for (int i = 0; i < item.length; i++){
                        item[i].paint(g);


                     }
                    }




            //==============================================================
            //Methods allowing keyboard input
            //=============================================================




                @Override
                public void keyPressed(KeyEvent e) {
                    //===================================
                    //Switch statement to moniter if 
                    //User moves arrows left or right
                    //And moves ball as a result
                    //====================================
                    switch(e.getKeyCode()){
                    case KeyEvent.VK_LEFT:
                        b.moveLeft();
                        break;
                    case KeyEvent.VK_RIGHT:
                        b.moveRight();
                        break;
                    case KeyEvent.VK_DOWN:
                        if(b.getBrake() == 1){
                            b.moveDown();
                        }

                        break;
                    default:
                        b.noMove();
                        break;

                    }

                }

                @Override
                public void keyReleased(KeyEvent e){



                }

                @Override
                public void keyTyped(KeyEvent e){
                    // TODO Auto-generated method stub

                }



}

What do I need to do to turn this applet into a application, and do I need to do anything to the other seperate class files?

Convert the code to use a JPanel for showing components and add it to a JFrame.
Rewrite the applet methods like init() and start() or call them from the class's constructor.
Replace all applet methods.

What happens if you change the extends to a JPanel and execute the code as an application?

If I try simply changing the extends from applet to JPanel it says JPanal can not be resolved to a type. and Ive tried several times to put the applet into a jframe then run applet.start(); and init, but I end up with a tiny window regardless of the set size, that when you resize it it just shows the backround color and you can see things moving at the top most edge of the screen. This program works fine as an applet not as an application, I wish to convert it to an applciation so I can make an exe file.

JPanal can not be resolved to a type

The class is named JPanel. Do you have the correct import statement for it?

yes and yes the typo was only in the post, auctually I got the extend Jpanel to go through, but now Im having issues with init and start. Saying they must over ride or impliment a super method. But they are already overrides.

Edited 4 Years Ago by cj122

This article has been dead for over six months. Start a new discussion instead.