简体   繁体   中英

how to implement java awt/swing application

I want to implement a java awt/swing application but i am new to awt/swing. I need to draw a grid like panel. Than drag and drop some objects on it. Later than objects can be clickable and resizable. My application should look like this:

期望的输出

I am able to draw object with :

public void paint(Graphics g){}

but its too far away from what i want to do.

How can i implement this kind of application? What should i read and know to do this?

First, I'd be sure to follow the Swing tutorials:

Then, I'd revisit your approach a little: I would use Swing components.

  • Start with a JPanel on a JFrame.
  • Add a JComponent to the panel for your grid. Override the paintComponent method there to draw your grid.
  • Add other JComponents on top of that for your objects. Again, override the paintComponent method for those to draw the appearance of your objects.
  • You can then use the built-in functionalities provided by JComponent to allow for clicking, resizing, etc. With each event, you can then redraw the component to account for any changes you need to display in the next call to paintComponent - calling repaint if necessary to force a redraw.

请不要使用public void paint(Graphics g){} void paintComponent(Graphics g){}此方法适用于AWT ComponentsBasicXxxUISwing JComponents是否可以使用Graphics教程中的方法public void paintComponent(Graphics g){}

Do you need to develop everything from scratch?

For painting, clicking mechanics etc you can use PlayN only the java part...

But if you want to use the swing capabilities to... I am not sure if you can mix playN with swing...

But if you only relly on the GraphicsAPI awt.... than you will need to look up couple of things.

small graphics enigne for drawing with the pinpoint capability (simple collision detection) so you can check each object on click. Movement and repainting are easely managed with simple engine...

Here is really good page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM