473,288 Members | 1,693 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,288 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 4371
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....
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.