Hi,
I'm trying to make a paint editor with Java in which I have a toolbar with the objects that I would like to paste in the canvas. I'm using swing components to make the GUI, but when I looked for the way of making the canvas, I only found the class canvas from awt.
Is there any way to make something similar to canvas with swing? (for example, JPanel?) I have read that using the class canvas from awt with a GUI made with swing won't work corectly, is that true?
Thank you, and sorry if my english is not very good.
-
You'll probably want to make a subclass of
JPaneland implement your own way of painting components you want to draw onto the panel.The basic approach will probably be along the line of assigning a
MouseListenerto the subclass ofJPanel, then implement painting functionality.The basic idea may be something along the line of:
class MyCanvas extends JPanel implements MouseListener { Image img; // Contains the image to draw on MyCanvas public MyCanvas() { // Initialize img here. this.addMouseListener(this); } public void paintComponent(Graphics g) { // Draws the image to the canvas g.drawImage(img, 0, 0, null); } public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); Graphics g = img.getGraphics(); g.fillOval(x, y, 3, 3); g.dispose(); } // ... other MouseListener methods ... // }The above example is incomplete (and not tested -- it definitely won't compile), but it gives an idea about how to implement a
MyCanvasclass in which a user can click on and draw circles.The
imgobject is used to hold the image of the canvas. ThepaintComponentmethod is used to paint theimgobject to the canvas. In themouseClickedmethod, theGraphicsobject associated withimgis retrieved in order tofillOvalonto the image.Since one the requirements is to paste images onto the canvas, it may be a good idea to hold some
Images that you want to paste into the canvas. Perhaps something along the line of:Image[] myImages; // Used to store images to paint to screen.Then, in the routine to paint the image onto
imgstored inMyCanvas:g.drawImage(myImage[INDEX_OF_DESIRED_IMAGE], 0, 0, null);By using the
drawImagemethod of theGraphicsobject, otherImages can be drawn ontoImages.
As for the question on AWT and Swing, yes, it is true that you do not want to mix components from the AWT and Swing, as they differ in the way they render GUI components. AWT is based on heavyweight components, meaning they native windowing for painting the GUI, while Swing is based on lightweight components, meaning the GUI is drawn by Java itself without using native components.
A good guide on the difference of AWT and Swing is provided in Painting in AWT and Swing article from Sun.
-
In order to make a custom 'Canvas' in swing you usually write a subclass of a
JPanel. Then, you must overwrite theprotected paintComponent(Graphicsg)method ofJPanel.In the paint method, you can call methods on the
Graphicsobject to actually draw on theJPanel.As always, the Java Tutorials have a great reference on this to get you started.
jjnguy : You are welcome. It is nice to hear positive feedback from an answer.Kenny M. : Very useful! I didn't know that a JPanel could be so multiverse. :-)jjnguy : @Kenny, glad to help! (Over a year later!) -
Simply subclass
JComponent.JPanelis an inappropriate class. It is often suggested as it appears to havesetOpaque(true)invoked on it automatically. It's actually the PL&F which does that, and whether or not it actually happens is implementation and vendor dependent.Canvasis a heavyweight component. That is to say that it is controlled by the underlying windowing system. The result is that it will typically be drawn over the top of Swing components, without respect to z-order or clipping (putting it in a scroll pane will give odd behaviour). -
You might want to look at the Minueto API. It is a very simple to use graphics api, and you can combine the Java event listening with it to provide your drawing capability.
0 comments:
Post a Comment