473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Converting JFrame to JApplet

hello im quite newbies in Java Programing so any one can help me. i will be plz....

what am i trying here is try convert The JFrame to JApplet, but when i change the JFrame to JApplet 4 Error come out

+++++++++++++++++++++++++error++++++++++++++++++++ ++++++++

CarRace.java:36: cannot find symbol
symbol : method setIconImage(java.awt.Image)
location: class CarRace
this.setIconImage(carImage);
^
CarRace.java:39: cannot find symbol
symbol : method setResizable(boolean)
location: class CarRace
setResizable(false);
^
CarRace.java:47: cannot find symbol
symbol : method addWindowListener(<anonymous java.awt.event.WindowAdapter>)
location: class CarRace
addWindowListener(new java.awt.event.WindowAdapter() {
^
CarRace.java:56: cannot find symbol
symbol : method pack()
location: class CarRace
pack();
^
4 errors

++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++

really need help he re plz


Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.util.*;
  4.  
  5.  
  6. public class CarRace extends JFrame {
  7.  
  8.     class MyTimer extends TimerTask {
  9.  
  10.         public MyTimer(CarRace listener) {
  11.             this.listener = listener;
  12.         }
  13.  
  14.         public void run() {
  15.             listener.onTime();
  16.         }
  17.  
  18.         private CarRace listener;
  19.     }
  20.  
  21.  
  22.     /** Creates new form CarRace */
  23.     public CarRace() {
  24.         initComponents();
  25.     }
  26.  
  27.     public void onTime() {
  28.         timer1OnTime();
  29.     }
  30.  
  31.  
  32.     private void initComponents() {
  33.         timer1 = new java.util.Timer();
  34.         timer1.schedule(new MyTimer(this), 1000, 50);
  35.         this.setIconImage(carImage);
  36.         jPanel1 = new javax.swing.JPanel();
  37.  
  38.         setResizable(false);
  39.         addKeyListener(new java.awt.event.KeyAdapter() {
  40.  
  41.             public void keyPressed(java.awt.event.KeyEvent evt) {
  42.                 formKeyPressed(evt);
  43.             }
  44.         });
  45.  
  46.        addWindowListener(new java.awt.event.WindowAdapter() {
  47.  
  48.             public void windowClosing(java.awt.event.WindowEvent evt) {
  49.                 exitForm(evt);
  50.             }
  51.         });
  52.  
  53.         getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
  54.  
  55.        pack();
  56.     }
  57.  
  58.     public void timer1OnTime() {
  59.         //get graphics
  60.         g = jPanel1.getGraphics();
  61.         //draw road
  62.         drawRoad(roadX, roadY, roadWidth, roadHeight);
  63.         //draw enemy car
  64.         for (int i = 0; i <= 4; i++) {
  65.             //move cars vertically
  66.             enemyCarsY[i] -= enemyCarsSpeed[i] - carSpeed;
  67.             if (enemyCarsY[i] < formDimension[1] - enemyCarHeight) {
  68.                 enemyCarsY[i] = formDimension[3] + enemyCarHeight;
  69.             }
  70.             if (enemyCarsY[i] > formDimension[3] + enemyCarHeight) {
  71.                 enemyCarsY[i] = formDimension[1] - enemyCarHeight;
  72.             }
  73.             if (enemyCarsX[i] < -enemyCarHeight) {
  74.                 enemyCarsX[i] = formDimension[2] - enemyCarHeight;
  75.             }
  76.             if (enemyCarsX[i] > formDimension[2] + enemyCarHeight) {
  77.                 enemyCarsX[i] = formDimension[0] - enemyCarHeight;
  78.             }
  79.             //move cars horizontally
  80.             if (i == 0 || i == 2 || i == 4) {
  81.                 enemyCarsX[i]++;
  82.             } else
  83.                 enemyCarsX[i]--;
  84.             drawEnemyCar(enemyCarsX[i], enemyCarsY[i], enemyCarWidth, enemyCarHeight, i);
  85.         }
  86.         //draw drivers car
  87.         drawCar(carX, carY, carWidth, carHeight);
  88.         //watch for car crashes
  89.         carCrash();
  90.         //draw fuel speed and road information on the form
  91.         drawPanel();
  92.     }
  93.  
  94.     private void formKeyPressed(java.awt.event.KeyEvent evt) {
  95.         //37 - left arrow; 38 - up arrow; 39 - right arrow; 40 - down arrow;
  96.         // 32 - space bar
  97.         keyCode = evt.getKeyCode();
  98.  
  99.         if (keyCode == 37 && carX > formDimension[0] && carSpeed != 0) {
  100.             carX -= 50;
  101.         } else if (keyCode == 38 && carY > formDimension[1]) {
  102.             if (carSpeed < maxCarSpeed) {
  103.                 carSpeed += minCarSpeed;
  104.             }
  105.         } else if (keyCode == 39 && carX < formDimension[2] + formDimension[0] - carWidth && carSpeed != 0) {
  106.             carX += 50;
  107.         } else if (keyCode == 40 && carY < formDimension[3] + formDimension[1] - carHeight) {
  108.             if (carSpeed > minCarSpeed) {
  109.                 carSpeed -= minCarSpeed;
  110.             }
  111.         } else if (keyCode == 32) {
  112.             newGame();
  113.         }
  114.     }
  115.  
  116.     /** Exit the Application */
  117.     private void exitForm(java.awt.event.WindowEvent evt) {
  118.         System.exit(0);
  119.     }
  120.  
  121.     public static void main(String args[]) {
  122.         new CarRace().setVisible(true);
  123.     }
  124.  
  125.     public Dimension getPreferredSize() {
  126.         return frameSize;
  127.     }
  128.  
  129.     private void drawCar(int a, int b, int c, int d) {
  130.         roadY += carSpeed;
  131.         if (roadY > formDimension[3]) {
  132.             roadY = 10;
  133.         }
  134.         //draw sikilmish image on the car blyad!!!
  135.         g.drawImage(carImage, a, b, c, d, null);
  136.     }
  137.  
  138.     private void drawRoad(int a, int b, int c, int d) {
  139.         //draw the road
  140.         g.setColor(Color.black);
  141.         g.fillRect(formDimension[0], formDimension[1], formDimension[2], formDimension[3]);
  142.         //draw the stripes on the road
  143.         g.setColor(roadColor);
  144.         for (int i = -6; i <= 6; i++) {
  145.             g.fillRect(a, b + i * stripesDelay, c, d);
  146.         }
  147.     }
  148.  
  149.     private void drawEnemyCar(int a, int b, int c, int d, int e) {
  150.         g.drawImage(enemyCarImage, a, b, c, d, null);
  151.     }
  152.  
  153.     private void carCrash() {
  154.         //oni stolknulis sikim blya
  155.         for (int i = 0; i <= 4; i++) {
  156.             if (enemyCarsX[i] > carX - enemyCarWidth && enemyCarsX[i] < carX + carWidth && enemyCarsY[i] > carY - enemyCarHeight && enemyCarsY[i] < carY + carWidth) {
  157.                 carSpeed = minCarSpeed;
  158.             }
  159.         }
  160.         //oni stolknulis no teper uje mejdu soboy sikim blya
  161.         for (int i = 0; i <= 4; i++) {
  162.             for (int z = 0; z <= 4; z++) {
  163.                 if (z != i && enemyCarsX[z] > enemyCarsX[i] - enemyCarWidth && enemyCarsX[z] < enemyCarsX[i] + enemyCarWidth && enemyCarsY[z] > enemyCarsY[i] - enemyCarHeight && enemyCarsY[z] < enemyCarsY[i] + enemyCarWidth) {
  164.                     enemyCarsX[i] = enemyCarsX[z] + enemyCarWidth;
  165.                 }
  166.             }
  167.         }
  168.     }
  169.  
  170.     private void drawPanel() {
  171.         g.setColor(panelColor);
  172.         if (carTimer >= 0) {
  173.             carTimer--;
  174.         } else {
  175.             g.drawString("               MAAF ANDA KEHABISAN MASA ",170,300);
  176.             g.drawString("TEKAN SPACE BAR UNTUK MEMULAKAN GAME", 170, 320);
  177.             carSpeed = 0;
  178.  
  179.         }
  180.         if (roadLength > 0) {
  181.             roadLength -= carSpeed;
  182.         } else {
  183.             g.drawString("       ANDA BERJAYA SAMPAI KE DESTINASI", 170, 300);
  184.             g.drawString("TEKAN SPACE BAR UNTUK MEMULAKAN GAME", 170, 320);
  185.             carSpeed = 0;
  186.             carTimer =  0;
  187.             roadLength = 0;
  188.         }
  189.         g.drawString("=========================", panelX, panelY);
  190.         g.drawString(" Kelajuan: " + String.valueOf(carSpeed) + " km/j", panelX, panelY + 20 );
  191.         g.drawString(" Masa: " + String.valueOf(carTimer) + " milisaat", panelX, panelY + 40);
  192.         g.drawString(" Jarak destinasi: " + String.valueOf(roadLength) + " km", panelX, panelY + 60);
  193.         g.drawString("=========================", panelX, panelY + 80);
  194.  
  195.     }
  196.  
  197.     private void newGame() {
  198.         //drivers car parameters
  199.         carX = formDimension[2] / 2;
  200.         carY = formDimension[3] - carHeight - 50;
  201.         carSpeed = 0;
  202.         //enemy car parameters
  203.         carTimer = 150;
  204.         //road parameters
  205.         roadX = 300;
  206.         roadY = -roadHeight;
  207.         roadLength = 10000;
  208.         keyCode = 0;
  209.  
  210.     }
  211.  
  212.     private javax.swing.JPanel jPanel1;
  213.  
  214.     private java.util.Timer timer1;
  215.  
  216.     private final int[] formDimension = { 0, 0, 600, 700};
  217.  
  218.     private Graphics g = null;
  219.  
  220.     //drivers car parameters
  221.     private final int carWidth = 65;
  222.  
  223.     private final int carHeight = 100;
  224.  
  225.     private int carX = Math.round(formDimension[2] / 2);
  226.  
  227.     private int carY = formDimension[3] - carHeight - 50;
  228.  
  229.     private int carSpeed = 0;
  230.  
  231.     private final int maxCarSpeed = 100;
  232.  
  233.     private final int minCarSpeed = 5;
  234.  
  235.     private int carTimer = 150;
  236.  
  237.     private final Image carImage = Toolkit.getDefaultToolkit().getImage("carImage.jpg");
  238.  
  239.     //enemy car parameters
  240.     private int enemyCarWidth = 55;
  241.  
  242.     private int enemyCarHeight = 100;
  243.  
  244.     private final int defaultEnemyCarWidth = 40;
  245.  
  246.     private final int defaultEnemyCarHeight = 80;
  247.  
  248.     private int[] defaultEnemyCarsX = { 400, 200, 250, 300, 350};
  249.  
  250.     private int[] enemyCarsX = { 400, 200, 250, 300, 350};
  251.  
  252.     private final int defaultEnemyCarY = formDimension[1] - enemyCarHeight - 50;
  253.  
  254.     private int[] enemyCarsY = { defaultEnemyCarY, defaultEnemyCarY, defaultEnemyCarY, defaultEnemyCarY, defaultEnemyCarY};
  255.  
  256.     private final int[] enemyCarsSpeed = { 18, 10, 25, 15, 30};
  257.  
  258.     private final Image enemyCarImage = Toolkit.getDefaultToolkit().getImage("-carImage.jpg");
  259.  
  260.     //road parameters
  261.     private final int roadWidth = 10;
  262.  
  263.     private final int roadHeight = 40;
  264.  
  265.     private int roadX = 300;
  266.  
  267.     private int roadY = -roadHeight;
  268.  
  269.     private int roadLength = 10000;
  270.  
  271.     private final int stripesDelay = 200;
  272.  
  273.     private final Color roadColor = new Color(255, 255, 255);
  274.  
  275.     //panel parameters
  276.     private final Color panelColor = new Color(255, 255, 255);
  277.  
  278.     private final int panelX = 15;
  279.  
  280.     private final int panelY = 25;
  281.  
  282.     private int keyCode = 0;
  283.  
  284.     private Dimension frameSize = new Dimension(formDimension[2], formDimension[3] + 20);
  285.  
  286.  
  287. }
  288.  
  289.  
Mar 17 '08 #1
3 4374
i really need some1 to guide me plz...
Mar 18 '08 #2
r035198x
13,262 8TB
i really need some1 to guide me plz...
Have you read Sun's swing tutorial to find out how to create JFrames?
I think you are better off reading the tutorials first.
Mar 18 '08 #3
might b good idea ... i'll try it out 1st
Mar 20 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: David Eynon | last post by:
I know that the basic methods that can be overwritten for class JApplet are init, and paint. However, the API does not specifically state this, and JApplet does not describe this right away.. How...
0
by: Kookymon1 | last post by:
This is an attempt to respond to an older question (several months). Date: 2002-03-07 13:10:23 PST Subject: On the Common DOM API and Applets. The original message was: >LiveConnect and the...
2
by: Aurel | last post by:
Hi, I developped an applet (JApplet class) and now I'd like to integrate it in another application which is a JFrame one... Does anyone can help ? Thanks in advance... AureL
1
by: - | last post by:
I have googled and learned that there are limitations with JApplet writing to files on remote host. However, I am unsure as to whether it is possible to do so on the same computer. Please advice....
7
by: sreenulanka | last post by:
please help me in my forum i have labels and textareas .iwant to add textarea components dyanamically when i am clicking the button.please help me import java.awt.*; import javax.swing.*;...
1
by: dishal | last post by:
Can anyone help me please? How do I convert these codes to launch from a JFrame instead of a Java Applet? A simple program where the user can sketch curves and shapes in a variety of...
5
by: James Barrett | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi all, I am experimenting with JApplet and JPanel. My JApplet contains a JPanel called jpanel1 size 210x210. I created a class myPanel which...
4
by: JNeko | last post by:
hello all, I have tried my hand at this for a couple hours but no luck. I am using JCreator. I used these two links for reference: http://www.tech-recipes.com/java_programming_tips1265.html...
3
by: shivapadma | last post by:
1.JApplet application can be runned through either browser or appletviewer 2.i want to know whether JFrame application can be runned through browser or not. please,someone help me....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.