I have a JInternal Frame and I want to draw a circle(using 2Dgraphics) in it and make it flexible. I mean when I change the size of frame the circle become smaller or in making frame larger circle also become larger. Can somebody help?
You would draw in the paintComponent method of a JPanel or JComponent that is held in the JInternalFrame's contentPane, same as you would draw in any other JPanel. I'd get the dimensions of the JPanel at the start of the paintComponent method and use those values to tell how big to draw the circle.
Also, if you add the JPanel directly to the JInternalFrame's contentPane, it will be added by default BorderLayout.CENTER, and so when the JInternalFrame changes size, the JPanel also changes size, it's paintComponent will be called by the JVM, and the new drawing will be resized automatically.
add a WindowListener to the jInteralFrame and redraw whenever the size changes
Related
I've been using Java Swing for quite some time now and I never found a solution to this problem. When I create a JFrame the window surrounding it is actually smaller than the frame. In the included picture below my JFrame size is 800x600. The 2 white lines crosses at the center of the frame, 400,300. As you can see they are not at the center of the window. If I stretch the window right and down I can see some of the black background of the frame was hidden. When the black background is revealed you can see the the lines do indeed cross at the center (2nd picture).
Why is it working like that? Anything I can do to solve this problem? Im making a game where the playable character is in the center of the screen so this causes me a lot of problem. The 1st image is larger because i've left the code in the background. As we can see it's a standard JFrame creation.
Not centered because part of the frame is hidden:
centered when frame is fully revealed:
my JFrame size is 800x600
You are doing things backward.
The frame has decorations (ie. the title bar and borders). The panel where you do the painting is added to the frame, so therefore it will be less than the size of the frame.
The proper approach is to override the getPreferredSize() method of the JPanel where you do the custom painting to return the desired size of the panel.
Then you add the panel to the frame you invoke the pack() method on the frame. Now the frame will be sized slightly larger (to fit the complete panel and the frame decorations) and your painting will be accurate.
I have a main panel with a border layout containing another panel in the center.
Because I want to paint inside the centered panel I have to get the bounds of it.
panel_main.add(panel, BorderLayout.CENTER);
I wanted to save the bounds in a rectangle like this:
Rectangle bounding = new Rectangle
(panel.getX(), panel.getY(), panel.getWidth(), panel.getHeight());
But every argument seems to be 0. So how do I get the bounds of the panel?
Could I get the bounds of the BorderLayout instead?
But every argument seems to be 0.
All Swing components have a size of (0, 0) when they are created. Components are only given a size when the frame is packed and made visible.
Because I want to paint inside the centered panel I have to get the bounds of it.
You override the paintComponent() method of the panel. Then you can use the getSize() method to control where the painting is done.
You will also need to override the getPreferredSize() method of the panel so the layout manager can pack() the frame properly.
Read the section from the Swing tutorial on Custom Painting for more information and working examples to get you started.
Currently, I have a full screen JFrame. Within that JFrame, is a panel called MainPanel which is 1280 by 640, a portion of the full screen game. The MainPanel is where my game is rendered to. Is there any way to scale this MainPanel to fit JFrame without having to adjust all the component sizes of sprites and such? I'm thinking of rendering MainPanel to an image and drawing it over JFrame, but I do not know how to go about this.
Thanks.
By default, the layout manager of a JFrame is BorderLayout, and adding a JPanel to it without any arguments will place it in CENTER, which will automatically scale to the size of the JFrame. It will ignore any size you have given the JPanel.
I have a rectangle which I move along the JPanel using repaint(). When the position of the rectangle reaches a position outside the JPanel it is not visible anymore. How can I make it visible outside the JPanel?
This my paint method:
public void paintComponent (Graphics g) {
g.setColor(Color.red);
g.fillRect(dist, 0, 10, 10);
dist++;
}
Update:
I have multiple JPanels in the JFrame which I positioned using the GridBagLayout. The JPanels represent Lanes in a Street and the rectangles cars. The reason to make the rectangles visible outside their JPanel is to have the cars change lanes. The JPanel seemed suitable to me, to set the first position of a car.
Is there a better solution for this problem?
You state:
I have a rectangle which I move along the JPanel using repaint(). When the position of the rectangle reaches a position outside the JPanel it is not visible anymore. How can I make it visible outside the JPanel?
If the JPanel is drawing it, the short answer is: "you can't".
The longer answer will depend on just where you're trying to draw the JPanel and how the rest of your GUI is set up.
Edit
You now state:
I have multiple JPanels in the JFrame which I positioned using the GridBagLayout. The JPanels represent Lanes in a Street and the rectangles cars. The reason to make the rectangles visible outside their JPanel is to have the cars change lanes. The JPanel seemed suitable to me, to set the first position of a car.
If I coded the way you were doing it, I wouldn't have these local JPanels draw the car but rather would have the car be its own sprite that exists on a different layer from the streets, perhaps using a JLayeredPane. It could exist in its own JPanel that encompasses your entire map, as long as this JPanel is not opaque. Then you could move the car any which way you'd like.
As said before you can't but if you want it to occupy and larger area you should either make the JPanel bigger or put the paintComponent in the parent component.
I have a JPanel where I draw shapes. this panel is added to a scrollPane. but the scrolling doesn't work when a shape is out of the scene. is there an easy way to autoscroll or I have to do it programmatically.
Thank you
I'm assuming you're using the JPanel as a canvas for custom drawing(i.e. you're not adding any JComponents to it). If that's case, the JScrollPane has no way of knowing how large the JPanel is and will just size it to exactly fill the scrollPane's viewport(which means you'll never actually get scroll bars).
To fix this issue you should override getPreferredSize on your JPanel.
#Override
public Dimension getPreferredSize() {
int height = calculateHeight();
int width = calculateWidth();
return new Dimension(width, height);
}
Edit:
Also, since you're doing custom drawing make sure to call revalidate whenever the shapes you want to draw change. This tells swing that it needs to re-think the layout/size of the JPanel.