Creating a small images from JPanel - java

I'm writing simple slide program in Java. In that program, I draw lines, ellipses, rectangles and etc in each slide. What is more, like a powerpoint I want to show all of my slides' symbolic small pictures on the JList.
How should I create small images from all elements in JPanel?
Thanks.

You might look at capturing a panel's image using Screen Image, discussed here.
Addendum: See also ComponentImageCapture.

If I understood your task correct, you must inherit from JPanel and overload method paintComponent(Graphics g). Inside you can write something like g.drawLine(0,0, 10, 10)

Related

How to put paint component pieces on top of a background in Java?

We have a group project where we must create a turn based, 2 player, grid game for class, i.e. Chutes and Ladders. The whole project is done except for the fact that when we bring the background image up, the players pieces get painted behind the background, and are therefore not visible. Is there a way to get the pieces to show up on top of the background image? We are using imageIO for the image, GridLayout for the Grid, and then using PaintComponent to place the pieces on the grid. When we do not have the background image, the pieces show up on the Grid flawlessly.
using PaintComponent to place the pieces on the grid
Make sure to call the super's paintComponent(...) method first thing in your paintComponent override.
Make sure that your paintComponent method has no program logic inside of it.
Sometimes it's better to place your pieces in ImageIcons and the Icons in JLabels, and then place your JLabels on a Grid of JPanels. For example.
For more help, post code and give more details. To be honest, I'm a little surprised that you haven't even posted your paintComponent method code.

Using a paintComponent with layout

How do I convert a paintComponent to something that I can manipulate with a layout in a JFrame?
So, I'm running into an issue. I haven't really been taught (and don't have access to a book) how to use layouts/GUI stuff in my courses yet.
My issue is this: I have a program that the user inputs a number. Based on this number, the program calculates a circle and draws it out with a paintComponent method that has a for loop inside of it. The "pixels" that the circle is drawn with are actually fillRect methods. The current method of getting a user-input that I am using is a JOptionPane showInputDialog. This is MOSTLY fine, but I want the user to be able to select from a set of pre-defined numbers. Somebody suggested that I use a JComboBox, but I don't know how I would convert the paintComponent to something that would be usable by a layout manager (which a JComboBox must use, as far as I've learned). I know the dimensions of the paintComponent (805px by 805px) and there is no situation where it will change. If I could get some help with this bit, I am confident that I can figure out using a layout manager myself.
Another way to paint (besides custom painting) is to paint to a BufferedImage. The image can then be displayed in a JLabel.
Examples:
Painting in a BufferedImage inside Swing A fairly complicated one.
Dynamic Graphics Object Painting Another one.
Yet another one.
You don't know the dimensions of paintComponent because it's a method, and methods don't have dimensions. You probably know the dimensions of a JPanel or a JFrame or whatever your component is.
You should separate the panel where you do the painting, and a different panel which would contain any comboboxes or other inputs you decide to put in. That way you can keep your drawing panel as is, and they won't interfere with each other. You'll want to search for the tutorial on LayoutManagers.

Drawing lines on BufferedImage in Java using threads

I have a BufferedImage, which is set to a JLabel, which in turn is part of a JPanel. On the click of a button, I am drawing some lines on the BufferedImage, which is subsequently updated on the panel. Earlier, I was drawing each line one-by-one, which was OK, but was time-inefficient for my purposes. To increase the efficiency, I divided the coordinates of the BufferedImage in 4 parts and drew the lines on each part using threads. Now, after the execution is completed, some random part of the image does not have the lines drawn. Why is that? Is drawing using Graphics2D not thread-safe??
Is drawing using Graphics2D not thread-safe??
Yes, Swing (and drawing in it) is definitely not thread-safe and rendering errors which you are experiencing are definitely because of that.
Please refer to following tutorial for more details:
http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html
As per mKorbel comment, please add SSCCE to receive more specific advice.

Making screenshot of drawing application + drawing outlines of 2D shapes

I am making a drawing program, using the Graphics 2D objects (lines, rectangles and ovals, namely) by placing them on a panel. With that in mind, I have 2 questions:
1) How can I store the images currently portrayed on the panel as PNG, JPG or similar file onto disk?
2) I have added a drag function. How can I implement a function so that one can see the "outline" of the rectangle, line or oval, before it is actually put onto the canvas (but not placing the outline on the canvas after the mouse button has been released)? I can't see that any of the MouseListener methods can do such a thing.
1) How can I store the images
currently portrayed on the panel as
PNG, JPG or similar file onto disk?
You can create a BufferedImage and paint any component onto it. The Screen Image class does this for you.
2) How can I implement a function so that one can see the "outline" of the rectangle, line or oval?
In this example, the shape itself may be dragged, rather than its outline, but the draw() method of class Node may be modified as desired. A rectangular outline is used for selection, as on a desktop.
1) ImageIO
http://www.java-tips.org/java-se-tips/javax.imageio/how-to-save-a-bufferedimage-to-a-png-file.html
2) Can't think of an answer for 2.

Is this possible to do with images in java?

Is there any way I can print/show images on top of each other. The picture on top will always be positioned a little lower so that the one under will show partially. How can I decide which image is on top of what image? What layout lets me do this kind of positioning?
Is there any way that I can make a border appear on the image when I click it, and then move to (doesnt have to be animated, can be a "jump") where I click next inside the JFrame.
I've been trying to do this whole day now (I'm pretty new to swing), before I carry on I'd like to know if I'm trying something impossible.
So far I've been printing the images right on to the JFrame as JPanels... Inside the JPanel I add in the paintComponent(Graphics g) method: g.drawImage
Sounds like a Swing tutorial is in order.
What you're describing shouldn't be very hard. Instead of painting the images directly, load them up in ImageIcons, and pass those to JLabels. That way you can manipulate your images as JComponents, using layout managers, or direct coordinates by setting the layout to null. You can set the Z-Order with setComponentZOrder regardless of the layout you choose. You can draw borders by adding swing borders (see BorderFactory) to the JLabels. You can handle the manipulation with MouseListeners.
Look into Root Panes. You may be able to do something with the Layered Pane or the Glass Pane. I would try the Layered Pane first.

Categories