Drawing on a JPanel or frame? - java

I am currently coding a Java program that involves loading an image of a map on a JLabel. I would like to draw on this map image. Is this possible, and if so, how?

Related

BufferedStrategie on JPanel?

I am programming a game and at the moment I use canvas to render the graphic stuff on it I am using a game loop to render ever 16 ms the player,the Map and so on new.
I wanted to insert buttons on it for some functions like opening a questlog but there is no way to use normal buttons on canvas as far as I got it.
So I asked my programming teacher and he told me to use JPanel instead of canvas so I can use buttons and render my graphics on it.
But when I try to render on a JPanel in my gameloop the game is flickering.
I think this happens because a JPanel don't have bufferedstrategie so I wanted to ask if it's possible to do buffering on JPanel like on a canvas?

Cannot Get grid style map to be drawn in JFrame

I am working on a simple game and do not have much JFrame background. How should i go about drawing a grid based map (20x20) with dynamic changes to the tiles (ie set pictures in them)?

Positioning a label using swing in java

I have been using jFrame to build a GUI. I had to insert images in the GUI, for which i inserted a label and put the image as an icon for the label. Now, i have to find out the position of the image in terms of the x and y co-ordinates and i am unable to do that. I have used
setLocaction(x,y);
but it still doesn't seem to work. I even disabled the layout manager by using
setLayout(null);
What is the possible solution for this problem?
Edit
Basically i am creating a Solar system GUI using Swing, so the positions of the planets are to be set by me. I being new to java, there is being some difficulty in implementing the layouts.
This isn't a layout issue at all, but a drawing and possibly an animation issue. If this were my project, I'd
First and foremost, separate out the program logic from its display, a la the MVC or Model-View-Control pattern, or one of its many variants.
Create one JPanel for the GUI's graphics and do all my drawing in this
I would not display my planet images using ImageIcons inside of JLabels.
Rather, I'd create a background image and draw my planet sprites inside of the drawing JPanel's paintComponent method.
I'd create non-GUI Planet classes that a non-GUI model would hold.
In the GUI portion of my program, I would associate a BufferedImage with each Planet, probably using a HashMap<Plant, Image>.
I'd then draw each Planet's associated image in the drawing JPanel's paintComponent(...) method, and place it depending on the Planet's position field values.
If I wanted to animate this, I'd use a Swing Timer to drive my simple animation.
With null layout you should use setSize and setLocation methods on you label to get your image visible correctly inside your frame.

Can I create a PNG image from a Java panel that has been drawn on?

I have a program that acts somewhat like a light MS Paint. Currently it reads in an XML SVG description (a very limited subset of SVG) and paints the image onto a JPanel, and then the user can update the image.
I was wondering if it was possible to save this image as a PNG (saving it back to SVG is easy). I have researched this, and seen that the examples create a BufferedImage and use Graphics to paint onto the BufferedImage. I was wondering if it was possible to take everything that has already been painted/edited and immediately transfer it to a BufferedImage.
Have you tried this?
Export JPanel Graphics to .png or .gif or .jpg
I think that there is something that you don't understand about how JPanel and graphics work in Java. Asking the JPanel to draw itself in a buffer is the same as what you want to do. A JPanel isn't drawn only once. The JPanel is repainted many many times even when you only drag a window over it.

how add different shapes partially on JPanel+image which is on JPanel

My Project is in Java Swing.
I have a JPanel on which I am adding some images with .png extension (which are on JLabels) at center.
Now I want to add a line which will be partially on the JPanel & partially on that image.
Currently when I am adding a line, JPanel shows the line but when I resize the image & drag it to the image, the image hides the line.
What can be done so that the image doesn't hide my line & shows it on image?
You're probably better off drawing the image yourself and drawing the line over the top in the same control. Create a class that extends Canvas and in the paint method write your own code to paint the image and then draw the line.
Another option is to use JLayeredPane instead of JPanel as your main container and place a non-opaque (setOpaque(false)) JPanel on a higher layer
Use JLayeredPane.setLayer(yourPanel, highNumber) and fill your JLayeredPane using something like GridBagLayout or a simple custom LayoutManager.
You can then implement the custom painting on that panel.
You could try using JXLayer and defining a custom LayerUI for it that would draw the lines. These would then appear above the components you need to draw over.
This is a little more advanced and involves using a 3rd party (open source) custom component but will allow you to change you mind about what Swing component you use to render your images later.
I think this article best describes how to achieve what you want.
I've solved many similar issues to this in the past in a variety of ways and none have had the flexibility and maintainability of JXLayer.

Categories

Resources