• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can't figure it out

 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I this code out of a Java book and edited it some to suite what I needed. When compiled I get a error on the part of the code that came from the book. Please help I have no idea. I am just wanting it to compile and run right now.

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

When you run into problems compiling your code, it helps to post the error messages you receive; makes it easier for people to help you.

In any case, I see a number of problems, most quite minor spelling issues. For example, "addMouseMotionListener()" is not spelled quite right, and you've got variables named things like RMouseX that you later refer to as RmouseX, for example. Java is case sensitive!

A slightly more complex issue is that you haven't implemented all the methods of the interfaces you're implementing. You should be getting errors about missing mouseEntered(), mouseMoved(), and mouseExited() methods, for example; you'll need to add those.
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im getting a mouseExited() error. Saying that I dont have one implemented. I don't think that I plan on using it. I still have to add it? Thats okay U guess. Ill look back over the spelling. thanks
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a class declares that it implements an interface, then yes, it's a promise to implement all the methods of that interface. MouseMotionListener has about half a dozen methods and you must include them all (even if some of them are just empty!)
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay I did that and it compiles but it does not run correctly. I wanted to copy and paste it into NetBeans so I can step through it and see what it does. I have been spoiled by MS Visual Studio. I have to make it a program to put it into NetBeans(i guess) so I copied and pasted it over and I get errors. I'm wanting an applet that allows you to draw lines by clicking and dragging your mouse. I also want the lines to stay on the screen and I will add a button so the user can clear the screen. But that clear screen comes after I can get it to draw the lines correctly.

The errors are at
addMouseListener(this);
addMouseMotionListener(this);

stating that:
non-static variable this cannot referenced from a static context.




[ July 07, 2007: Message edited by: James Hambrick ]
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The main() method is a static method -- it is called without using an object reference. Hence, there is no such thing as a "this" object, in a static method. This is what is meant by "non-static variable this cannot referenced from a static context".

I'm wanting an applet that allows you to draw lines by clicking and dragging your mouse. I also want the lines to stay on the screen and I will add a button so the user can clear the screen. But that clear screen comes after I can get it to draw the lines correctly.


The other issue is that an "applet" inherits from the java.applet.Applet (or one of its subclass) class -- which you did not do in your second example.

Henry
[ July 07, 2007: Message edited by: Henry Wong ]
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Netbeans I'm going to make it a program so thats why it doesn't extend Applet. Where do you suggest I place the addMouseListener and addMouseMotionListner then? I've tried a few places and still get errors. Im just wanting to be able to step through the code that's why Im makign it program in NetBeans, I will then make it back into an applet after I get it working right. Also I added


Which is supposed to override update. This is supposed to keep it from clearing when updating, but it still clears, or im just moving around the same line instead of drawing new ones.
[ July 07, 2007: Message edited by: James Hambrick ]
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i move it around I get
Cannot find Symbol. Also all the repaint(); 's are errored with cannot find symbol.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Netbeans I'm going to make it a program so thats why it doesn't extend Applet. Where do you suggest I place the addMouseListener and addMouseMotionListner then? I've tried a few places and still get errors.


The difference between an applet, and a plain application (using main), is more than just inheriting from the Applet class. The applet class not only deals with the HTML interface, it also deals with the panels and drawing canvas for the applet itself.

A plain application is a command line application. You don't just move the addMouseListener methods and by magic, it works. If you want a GUI, you will need to instantiate a frame, a canvas, probably a panel so that you can place buttons, and then register your mouse listener to it. Your paint() method should be part of the drawing area, hence, the canvas that you create.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, here is an article on the Sun website that discusses how to convert an applet into a stand-alone application.

Henry
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How should I step through an applet to see whats going wrong? I think that if click the mouse then the mouseClicked(MouseEvent me) would be called and I would get the value of x and y and if I dont put repaint() inside this method then it would not draw the line, but it does, it calls paint(Graphics g). Ill make a windows and all that stuff to get it to work, then from what I learned I can make an applet. I wish there was a way to be able to step through an applet without having to re-write it into a program.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wish there was a way to be able to step through an applet without having to re-write it into a program.


Well, I am not a Netbeans user, but I am surprised that Netbeans can't do this. Maybe you should post a question about this in the IDE (and other tools) forum.

Henry
 
Chad Clites
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Netbeans has a good debugger. All you need to do is set a breakpoint by clicking the grey area (where the line numbers are) to set a breakpoint. Then in the File Pane on the left side, right click on the applet, and select debug. When it hits your breakpoint, execution will pause. Then you use f7 to step through the code.

Alternatively, if you are running the applet in the debugger, you can add System.out.println statements wherever you want to check for values. Typically, something like: System.out.println("Am I in the loop");
can help you identify to what point you are making it in the program.

Here is a tutorial that may help you with the debugging process. Here is another tutorial that may help you.
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the issue is getitng the applet into NetBeans not learning how to use the debugger. I cannot just open it up in NetBeans, and I cannot just start a new Applet and copy and paste the code over.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic