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.
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.
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!)
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 ]
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 ]
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 ]
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.
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.
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.