-
Nov 10th, 2000, 04:10 PM
#1
Thread Starter
Dazed Member
Im having i little of a problem compiling this code which
i scapped from suns Java tutoral site any suggestions or coments on what im doing wrong would really be cool.
I understand the code for the most part but evertime i compile i keep getting 3 errors.
1) illegal start of expression
public void init(){
^
2) ClickMe should be declared abstract; it does not define
MouseClicked(java.atw.event.MouseEvent)
3) cannot resolve symbol
Spot spot = null;
^
It seems to be that they are importing the entire
package ie "import java.atw.*;" so then is
java.atw.event.MouseEvent a package memeber of
"java.awt.event.*" ?
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class ClickMe extends Applet implements MouseListener {
private Spot spot = null;
private static final int RADIUS = 7;
public void init() {
addMouseListener(this);
}
public void paint(Graphics g) {
// draw a black border and a white background
g.setColor(Color.white);
g.fillRect(0, 0, getSize().width - 1, getSize().height - 1);
g.setColor(Color.black);
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
// draw the spot
g.setColor(Color.red);
if (spot != null) {
g.fillOval(spot.x - RADIUS,
spot.y - RADIUS,
RADIUS * 2, RADIUS * 2);
}
}
public void mousePressed(MouseEvent event) {
if (spot == null) {
spot = new Spot(RADIUS);
}
spot.x = event.getX();
spot.y = event.getY();
repaint();
}
public void mouseClicked(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
}
---------------------------------------------------------
// Spot class in another file
public class Spot {
//instance variables
public int size;
public int x, y;
//constructor
public Spot(int intSize) {
size = intSize;
x = -1;
y = -1;
}
}
-
Nov 10th, 2000, 05:40 PM
#2
Your code worked for me. I just changed "public class Spot {" to "class Spot {" and kept it in one file (just to test it).
Maybe your problem is that you typed "atw" instead of "awt".
Check out Adapters like "MouseAdapter" for Listeners with more than one method. That way you can be sure that you can implement only the method that you want and not have to provide every "null" implementation for methods you do not need to implement. That way the compiler won't say your class needs to be declared "Abstract".
-
Nov 11th, 2000, 09:32 PM
#3
Thread Starter
Dazed Member
This seems to work kind of good but i keep getting
illegal start of expression.
public void init () {
^
which i dont understand.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.MouseListener;
abstract class ClickMe extends Applet implements MouseListener{
public static void main(String[] args){
int RADIUS = 7;
public void init () {
addMouseListener(this);
}
public void paint(Graphics g) {
// draw a black border and a white background
g.setColor(Color.white);
g.fillRect(0, 0, getSize().width - 1, getSize().height - 1);
g.setColor(Color.black);
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
// draw the spot
g.setColor(Color.red);
if (spot != null) {
g.fillOval(spot.x - RADIUS,
spot.y - RADIUS,
RADIUS * 2, RADIUS * 2);
}
}
public void mousePressed(MouseEvent event) {
if (spot == null) {
spot = new Spot(RADIUS);
}
spot.x = event.getX();
spot.y = event.getY();
repaint();
public void mouseClicked(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
};
}
}
-
Nov 14th, 2000, 06:07 PM
#4
Watch where you put your braces {, and }
First of all, why don't you revert back to the code in the first post. That one works (watch where I made it one file) or keep it two files and make an html file to view your applet.
Second, the normal reason you declare a class as abstract is to require a class that inherits (extends from) this class to implement its abstract methods. I don't think you are dealing with instances of classes yet (well, you are, when you instantiate objects of the Spot.class).
You are trying to define the "init()" method from within the "main(String[] args)" method. I believe you can do that, but I think you need an inner class within main, then a method of that inner class would techinically be defined within the outer method "main". But I doubt you are dabbling in inner classes yet.
I recommend that you go back to the original code; the two files ClickMe.java, Spot.java and you need an html file to view the applet. Then you can use "appletviewer" or a real browser.
Here is a sample html file "ThisWorks.html"
Code:
<applet code="ClickMe.class" width=400 height=400>
</applet>
Type appletviewer ThisWorks.html at the dos prompt then click in the applet and you will see red spots. Or open this html file in a browser and click in the applet area.
Of course you will begin and end all your html files with the <html></html> tags when you are done testing. But this works with this minimal info.
-
Nov 15th, 2000, 03:07 PM
#5
Thread Starter
Dazed Member
Hey thanks for your help VirtuallyVB...!
I went back to the code i had and add the spot class to the ClickMe class and when i compile the ClickMe class i end up getting to class files so do i code the html part like this? <applet code= "C:\Java\ClickMe.class" "C:\Java\Spot.class">
</applet>
I tried <applet code="C:\Java\ClickMe.class">
</applet> and the applet shows up but nothing happens
when i click on it.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
class Spot{
public int x; // represents the horizontal axis
public int y; // repersents the vertical axis
public int size;
public Spot(int intsize){
size = intsize;
x = -1;
y = -1;
}
}
public class ClickMe extends Applet implements MouseListener {
private Spot spot = null;
private static final int RADIUS = 7;
public void init() {
addMouseListener(this);
}
public void paint(Graphics g) {
// draw a black border and a white background
g.setColor(Color.white);
g.fillRect(0, 0, getSize().width - 1, getSize().height - 1);
g.setColor(Color.black);
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
// draw the spot
g.setColor(Color.red);
if (spot != null) {
g.fillOval(spot.x - RADIUS,
spot.y - RADIUS,
RADIUS * 2, RADIUS * 2);
}
}
public void mousePressed(MouseEvent event) {
if (spot == null) {
spot = new Spot(RADIUS);
}
spot.x = event.getX();
spot.y = event.getY();
repaint();
}
public void mouseClicked(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|