How to Change JFrame Size dynamically - java

I have a simple drawing program, I set my Jframe's size with the following code:
frame.setSize(900, 700);
However, when user change the size of the window, white area to draw, still remain same size therefore, user is not able to draw eventhough he enlarge the window.
http://forum.codecall.net/java-tutorials/31180-java-mini-paint-program.html this is where I start from. I extended class "PadDraw" and wrote most of my code to there, in my other java file, I only create a frame, then I create "PadDraw" object, I crated a container and then added object to the frame's container: content.add(drawPad, BorderLayout.CENTER);
I changed my code:
public class PadDraw extends JComponent implements ActionListener, ItemListener, ComponentListener{
public synchronized void addComponentListener(ComponentListener l) {};
.
.
.
.
And I added unimplemented methods, and the "componentResized" is:
public void componentResized(ComponentEvent arg0) {
System.out.println("Changed ????");
}
But when I changed the window's size, nothing happens.
One thougt: When I added componentlistener to my other file instead of drawPad, componentResized method flags, but because I have created drawPad object before this event occured, I can't change the size =/
Thanks

Hopefully you're not drawing directly on the JFrame but rather in a JPanel or JComponent that's been added to the JFrame's contentPane. If it has been added BorderLayout.CENTER, then it should expand and contract as the JFrame expands and contracts. Things get a little trickier if you're drawing on a BufferedImage as you'll have to use a ComponentListener to see when the component that holds the BufferedImage changes size, and when it does, change the size of the BufferedImage without losing the drawing that's already there.
If this answer is not helpful enough, consider giving us more specific information about your current problem and your current program.
In the program that you link to, you're drawing on an Image object whose size is set the first time paint is called. What you'll want to do is to add a ComponentListener to the PadDraw object in its own constructer, and in the ComponentListener's componentResized method, resize your image using the PadDraw's new height and width. I would only do this however if the new size is larger than the old size. You would also then want to redraw the old image on the new before setting the image variable = to the new image. Then call repaint.

Related

Java Swing component not repainting

I have an LWJGL OpenGL Display showing up inside an AWT Canvas, which in turn is inside a Swing JPanel that is used as content pane for a Swing JFrame. At some point in the program, I want to switch the AWT Canvas containing the Display for a JComponent, so that instead of having something like that:
JFrame > JPanel > Canvas > Display
I have something like so:
JFrame > JPanel > JComponent
However, even though I remove the Canvas from the JPanel and add the JComponent, then revalidate the JPanel and repaint it, the Display still shows until I CTRL-ALT-SUPPR to task manager (my JFrame is set Undecorated and ExtendedState is JFrame.EXTENDED_BOTH, so it is full screen). At which point, the JComponent shows up like nothing ever happened..
I'll share the part of my code that does the transition so you can maybe help me point out what I have done wrong:
public static void switchTo(Container container){
pan.removeAll();
container.setBounds(0, 0, Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);
pan.add(container);
frame.getContentPane().validate();
pan.revalidate();
pan.repaint();
}
where pan is my JPanel and frame is my JFrame.
I have also tried directly setting my JComponent as my JFrame's content pane, but that gives the exact same result.
The only way I managed to make it function correctly was by calling destroy() on the Display beforehand; however, I need to keep the OpenGL context running so that I don't have to re-initialize the Display and reload every texture when switching back to the Display, which would be quite a long process given the number of textures I have.
Thank you very much for any answer, I hope I made myself clear!

Paint hidden Java swing component

I've tried to paint component to PDF. I've got itextpdf 4.2 and everything works perfectly.
But this works only if I make visible the frame that I've tried to render.
The similar question that I've found is How to paint an invisible JFrame elsewhere? that has the same issue, but the solution wasn't provided in answer.
A little of code.
I've created a JFrame and insert main view that I want to render.
JFrame jframe = new ShowingFrame();
jframe.setPreferredSize(new Dimension(PDFHelper.getOriginalWidth().intValue(), PDFHelper.getOriginalHeight().intValue()));
jframe.setMinimumSize(new Dimension(PDFHelper.getOriginalWidth().intValue(), PDFHelper.getOriginalHeight().intValue()));
jframe.add(view);
jframe.setUndecorated(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setState(Frame.ICONIFIED);
jframe.setState(Frame.NORMAL);
//jframe.setVisible(true);
If I uncomment jframe.setVisible(true) than everything works.
But user will see this frame that I want to avoid.
So the question is: How to paint hidden control?
Inside Swing Component.java class all paint methods first check if the component is visible:
public void paint(Graphics g) {
if (isShowing()) {
// rest of the code...
}
}
I've tried to create inherit class ShowingFrame extends JFrame, that override isShowing and always return true. But this not helps to.
Swing (and the Java Graphics API) is optimized to stop rendering as soon as possible.
So the solution is to create a BufferedImage, get the Graphics instance from it and then call component.paint(g); with it.
Now you have a tab component. Try to get the content of the tab instead of rendering the tab itself. If that doesn't work, you can try to clone the tree of children, create a new JPanel, attach the children and render the result. But cloning can become tedious if the models don't behave well.
See this question for some code: Swing: Obtain Image of JFrame
Why do you want to paint something that is not visible? Your computer does not want to waste CPU cycles rendering graphics that can't be seen. In fact, there is a lot of computations done to see what parts of each window are visible and only paint the visible parts (the so called clip window).
If you want to paint something so you can use it later or save it you can always create a BufferedImage of the size you want and paint to that.
If I uncomment jframe.setVisible(true) than everything works. But user will see this frame that I want to avoid.
You can set the frame location so that it is not visible on the screen. Maybe something like:
frame.pack();
Dimension d = frame.getSize();
frame.setLocation(-d.witdh, 0);

Component only fully shows when I maximize frame

I'm programming the game hangman and I have a Label in the center for the word to be guessed, a score panel on the right(east) and a panel of button characters on the bottom(south). As i was moving onto painting the hangman gallows, i tried to paint a simple circle at first and add it to the left(west) of the frame, but only a small portion of the circle appeared, the problem seemed to be fixed temporarily when i maximized the frame and the resized it to its original size, how do i fix this? I created a class that extends JPanel and overrided its pain component method. what should i do so that it fully shows the circle when the program is launched.
I created a class that extends JPanel and overrided its pain component method. what should i do so that it fully shows the circle when the program is launched.
You also need to override the getPreferredSize() method to return the size of the panel so that the layout managers can use this information.
overrided its pain component method
You should be overriding the paintComponent() method, not the paint() method.
Read the section from the Swing tutorial on Custom Painting for more information and working examples.

Resizing a JFrame to fit added components

I'm making a program for fun, it's basically a computer navigation GUI, details not required :)
Anyway, so far, I have a button called "new button" that, when clicked, it creates a new button named "test", to an infinite amount. Right now, i have my GUI set up like this:
Class Main extends JPanel (the main panel that holds everything in it, size set as)
Dimension size = new Dimension(300, 200);
setPreferredSize(size);
JFrame holding the Main JPanel, called like:
panel.frame = new JFrame();
panel.frame.setResizable(false);
panel.frame.setTitle(panel.title);
panel.frame.add(panel);
panel.frame.pack();
panel.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.frame.setLocationRelativeTo(null);
panel.frame.setVisible(true);
So, how would i have the JFrame/JPanel set their size based on the components inside it? I've tried to use panel.frame.pack(); but i get an error most of the time, and the other times it doesnt wrap, it is just a staight line. I want it to resize in the form of a square. Any ideas? Sorry if my question isnt clear/poorly phrased, i've always had issues articulating questions online, much better in person cause i can use my hands! :) Thanks in advance!
Class Main extends JPanel (the main panel that holds everything in it, size set as) Dimension size = new Dimension(300, 200); setPreferredSize(size);
Don't set the preferred size of the panel. The layout manager will determine the preferred size based on the components that you add to the panel.
and the other times it doesnt wrap,
The default layout manager for a JPanel is a FlowLayout. It is not designed to wrap automatically. Maybe use a different layout manager. Or you can try the Wrap Layout which extends FlowLayout to provide dynamic wrapping.
I've tried to use panel.frame.pack(); but i get an error most of the time
What error. I've never seen an error when using the pack() method.
Post a proper SSCCE if you need more help.

Java add image from an event

So I'm making this program with a GUI and I haven't worked with Swing/SWT too much but a little bit to know what's going on.
Anyway, I add an actionlistener for a button so it'll add an image to the contentPane when I click on the button but it doesn't work unless I have it as a JComponent (as seen below) and add my other things (button, JLabel, etc) to it afterwards...AND set this JComponent to the content view (which doesn't make sense).... I've also tried making it extend JPanel and just clearing out original contents and re-adding them to the new JPanel. The thing is, when I do this it recreates the text for my JLabel in a weird way, and I just know there's gotta be a simpler, more efficient, way.
class ShowImage extends JComponent{
public ShowImage(){
super();
monkey = Toolkit.getDefaultToolkit().getImage(("D:/monkey.png"));
}
public void paintComponent(Graphics g){
g.drawImage(monkey, 20, 100, null);
repaint();
}
}
Do not invoke repaint inside paintComponent
Invoke super.paintComponent and then draw the image
Also, depending on the layout manager, this component will have a preferred size of (0, 0), and therefore will not be visible.
For more information, see 2D Graphics.
Edit -
Note that dynamically adding a component will force you to revalidate the container and issue a repaint request so the layout manager will layout its components again and remove any visual artifacts. Also, for more information regarding images, see Working with Images.
Anyway, the simplest approach would probably be to set the image as the icon of a JLabel instance and add that to the container. There's really no need to reinvent the wheel here.
g.drawImage(monkey, 20, 100, this);
..would most likely have fixed the problem in the original code. It was a combined problem of:
Loading the image in an asynchronous way. (Toolkit.getImage() as opposed to ImageIO.read().)
Painting it to a 'blinkered' ImageObserver. The JComponent implements ImageObserver. As soon as the image is totally loaded (as well as a few points before that), the observer will be informed, triggering a repaint().

Categories