-1

For my current project I have the situation that I need an interactive element that will be drawn on a custom graphics context and allows user interaction (e.g. mouse clicks).

Since this elements will be quite complex I decided the best solution would be to use a JPanel for layouting and manually paint it and dispatch mouse and keyboard events.

Currently my plan is to:

  1. call setSize and setLocation on the Jpanel
  2. call paint with the graphic context
  3. Catch desired events (in this example clicks) create a new MouseEvent and pass it on with JPanel.dispatchEvent().

I suspect there will be many traps and edge cases along the way so

a.) Is this the correct approach or is it missing something fundamental? b.) Is there any existing library which could be uses?

Vespasian
  • 605
  • 1
  • 5
  • 14
  • Do you have a single problem you want help with? Besides your first point (which is a bad idea), the other two seem like functions already provided by swing behind the scenes. – Paul Samsotha Mar 17 '14 at 11:26
  • Thanks for the feedback. My problem is that I need to render this UI elements directly onto a map that allows panning and moving. (see comment below Tim B answer) – Vespasian Mar 17 '14 at 17:25

1 Answers1

0

This is a bad idea, you are throwing away half of what Swing does for you and then trying to re-implement it yourself.

If you really must do this then something like this: How to make canvas with Swing? is probably your best way forwards. Really I would look to see if you can instead do this just by building the screen from custom controls though.

Community
  • 1
  • 1
Tim B
  • 40,716
  • 16
  • 83
  • 128
  • My situation is that I have a map system on which overlays are drawn. For historical reasons (aka "massive change of scope") the system is written in Java in the first place instead of a more "appropriate language". Now the feature request "make the elements on the map controlable" forces me to somehow squeeze UI elements in while maintaining map properties like panning and zooming. I will look into your link and see whether it can be applied to my problem – Vespasian Mar 17 '14 at 17:24