I think this is more than just beginner forum stuff (I may be wrong).
First question, i have a paint command within a canvas class that doesn't seem to be recognised on PC's, but it is on macs, is there some different way you have to set it up on PC's? The code is exactly the same, i don't understand this problem.
Second question, are the commands for getting keyboard presses different between the two platforms (mac and pc), they've never worked on pc's.
It would be really helpful if someone could post some code showing how to get keyboard presses to move a picture around the screen on a pc (this would solve all my problems most likely).
This really has me confused
Any suggestions or answers would be very much appreciated.
These are probably not beginner questions, but they're also not intermedite questions: they're Swing/AWT questions -- so I'm moving this to the Swing/AWT forum. You should probably head over to the new copy of this thread and post some code, so people can see what you're doing wrong.
I don't think you should override the update(Graphics) method...
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Ilja Preuss: I don't think you should override the update(Graphics) method...
No, in AWT it's a fine thing to do -- otherwise the screen is erased to gray before your paint method gets called.
Your code is too simplified, I think, for us to see any problems. The problems are likely to be in the event handlers or in the paint method, so let's see those.
One way to move an image is with the help of a Rectangle declared in class scope. Make it the width and height of the image, move it with your key code and render the image at r.y, r.y.
Well, two separate problems deserve two separate answers.
First, the escape key handler. Components won't receive key events unless they've got the keyboard focus. You can put the keyboard focus on a component using the requestFocusInWindow() method. You'll find that if you add anf.requestFocusInWindow() line at the end of main(), your escape key handler will work.
Second, the canvas painter. The BorderLayout will ask the Canvas for its preferred size, and try to make the height onscreen equal to that value. The default getPreferredSize() method returns Dimension(0, 0). Therefore, the canvas will be zero pixels high, and the AWT is smart enough to never try to paint it. If you override getPreferredSize() in the Canvas subclass and return a java.awt.Dimension with non-zero dimensions, you'll find your paint method is then called.
If this works on a Mac, it's due to undefined behavior, not any official difference between platforms.