🔒 Locked
My java code
Advertisement
Original Post
Hey!Im pretty new to java and ive made writtin a code for a applet thats surpost to countdown from 10 to 1,it has the 1 following eror: nothing.java:11: ';' expected the code is here: import java.applet.*; import java.awt.*; public class nothing extends applet { int count = 10; int zero = zero; public void init() { if (count > 0) count-- } public void paint(Graphics g); { if (count == 0) system.out.println("Blast Off!!!"); else system.out.println("countdown: " + count); repaint(); } }
Should be "count--;"
Also, get the ';' out after "paint(Graphics g)".
So, the source should look like this:
Also, get the ';' out after "paint(Graphics g)".
So, the source should look like this:
import java.applet.*;import java.awt.*;public class nothing extends applet {int count = 10;int zero = zero;public void init() { if (count > 0) count--;}public void paint(Graphics g) { if (count == 0) System.out.println("Blast Off!!!"); else System.out.println("countdown: " + count); repaint();}}
EDIT: This response is to the question you just edited out about it not working
Your init code should be in some sort of "updating" section. So you want to start a new thread (extend Runnable), and use the "run" method. This is all a bit above your head as a new java programmer, but it is a pretty well established base for code.
As well, your logic for your code is wrong. You want "while(count > 0) count--; " Your init code makes count only go to 9.
As well, you are using "System.out.println" in an applet to be viewed in a browser. System.out.println goes to the console. You want to use "Graphics::drawString(char *, int, int)"
So for example, in your paint method, you want something like this:
So here is how I would write your code:
Also, "int zero = zero" in your code doesn't mean anything...
import java.awt.*;import java.applet.Applet;import java.awt.event.*;import java.util.*;public class ExampleApplet extends Applet implements Runnable{ private final int APPLET_WIDTH = 800; private final int APPLET_HEIGHT = 600; public void init() { setBackground(Color.white); setSize(APPLET_WIDTH, APPLET_HEIGHT); } public void start () { Thread th = new Thread (this); th.start(); } public void stop() { if(Thread.currentThread() != null) Thread.currentThread().stop(); } public void destroy() { } public void run () { //logic here (countdown) repaint(); } public void update (Graphics g) { //drawing routine here } public void paint (Graphics g) { }}Your init code should be in some sort of "updating" section. So you want to start a new thread (extend Runnable), and use the "run" method. This is all a bit above your head as a new java programmer, but it is a pretty well established base for code.
As well, your logic for your code is wrong. You want "while(count > 0) count--; " Your init code makes count only go to 9.
As well, you are using "System.out.println" in an applet to be viewed in a browser. System.out.println goes to the console. You want to use "Graphics::drawString(char *, int, int)"
So for example, in your paint method, you want something like this:
public void paint(Graphics g) { g.setColor(Color.black); if(count == 0) { g.drawString("BLAST OFF!", 0, 0); done = true; } else { g.drawString("Countdown: " + count, 0, 110 - count*10); }}So here is how I would write your code:
import java.awt.*;import java.applet.Applet;import java.awt.event.*;import java.util.*;public class nothing extends Applet implements Runnable{ private int count = 10; private boolean done = false; private final int APPLET_WIDTH = 800; private final int APPLET_HEIGHT = 600; public void init() { setBackground(Color.white); setSize(APPLET_WIDTH, APPLET_HEIGHT); } public void start () { Thread th = new Thread (this); th.start(); } public void stop() { if(Thread.currentThread() != null) Thread.currentThread().stop(); } public void destroy() { } public void run () { while(count > 0) { count--; repaint(); try { Thread.sleep(1000); //sleeps for a second } catch(InterruptedException e){} } } public void update (Graphics g) { g.setColor(Color.black); if(count == 0) { g.drawString("BLAST OFF!", 0, 0); } else { g.drawString("Countdown: " + count, 0, 110 - count*10); } } public void paint (Graphics g) { }}Also, "int zero = zero" in your code doesn't mean anything...
Yeah, my bad. Forgot to set the color to black. This is probably what you want:
Remember, drawString() uses the bottom left point of rectangle to draw the string. "12" is a magic number for the height of the font we are using.
import java.awt.*;import java.applet.Applet;import java.awt.event.*;import java.util.*;public class nothing extends Applet implements Runnable{ private int count; private final int APPLET_WIDTH = 800; private final int APPLET_HEIGHT = 600; public void init() { setBackground(Color.white); setSize(APPLET_WIDTH, APPLET_HEIGHT); } public void start () { Thread th = new Thread (this); th.start(); } public void stop() { if(Thread.currentThread() != null) Thread.currentThread().stop(); } public void destroy() { } public void run () { for(count = 10; count > 0; count--) { repaint(); try { Thread.sleep(1000); //sleeps for a second } catch(InterruptedException e){} } repaint(); } public void update (Graphics g) { g.setColor(Color.black); if(count == 0) { g.drawString("BLAST OFF!", 0, 12); } else { g.drawString("Countdown: " + count, 0, 12 + count*12); } } public void paint (Graphics g) { repaint(); }}Remember, drawString() uses the bottom left point of rectangle to draw the string. "12" is a magic number for the height of the font we are using.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement