File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Arrow key commands and paint method in canvas (mac vs pc) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Arrow key commands and paint method in canvas (mac vs pc)" Watch "Arrow key commands and paint method in canvas (mac vs pc)" New topic
Author

Arrow key commands and paint method in canvas (mac vs pc)

Mark Lockery
Greenhorn

Joined: Jun 04, 2004
Posts: 19
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Hi Mark,

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.


[Jess in Action][AskingGoodQuestions]
Mark Lockery
Greenhorn

Joined: Jun 04, 2004
Posts: 19
Sorry about the wrong forum area.

This is my code, with unneccessary bits weeded out.

The problems are that the paint method doesnt get called for some reason, and that the keycodes dont work.

Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535

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.
Mark Lockery
Greenhorn

Joined: Jun 04, 2004
Posts: 19
Here is my complete code at the moment.

It does not recognise if i press the key escape, and it does not start the paint method (i can tell this by the exit command contained in it)

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrow key commands and paint method in canvas (mac vs pc)
 
Similar Threads
Mac Mini, Apple Stuffs
Performance question
DriverManager.getConnection hangs from actionPerformed
do i need mac os for iphone application development ?
Mac Noobie needs help