• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mark Lockery
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you should override the update(Graphics) method...
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic