File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes can't figure it out Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can Watch "can New topic
Author

can't figure it out

James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

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.



Visit my blog! http://jameshambrick.com
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Sep 04, 2004
Posts: 282

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
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40



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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

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

Joined: Sep 04, 2004
Posts: 282

If i move it around I get
Cannot find Symbol. Also all the repaint(); 's are errored with cannot find symbol.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

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

Joined: Sep 04, 2004
Posts: 282

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
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

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

Joined: Aug 16, 2005
Posts: 134
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

Joined: Sep 04, 2004
Posts: 282

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
 
subject: can't figure it out