Changing the border of rectangle in jPanel - java

I ama drawing a rectangle in a panel and i added a + button to increase my shape's size.how can i do it?

Read the section from the Swing tutorial on Custom Painting for the basics.
Now, in your class that does the custom painting you need to keep two variables:
rectangleWidth
rectangleHeight
You will also need to add a method to the class like "increaseRectangleSize()". Then when you click your button you invoke that method. That method will increase the values of those two variables and then invoke repaint() on itself.
If you need more help post your SSCCE that demonstrates the problem since your description of the problem is too vague.

If your question is how to draw a bigger rectangle. In the paintComponent method that you are drawing a the rectangle increase the size. If you are trying to make the JPanel bigger, I would highly suggest you looking using Layout Managers and possibly set the PreferredSize() of the panel.

Related

Why does JCompnent.getBounds() always return 0 values?

When I try to get the bounds of a JPanel it always returns (
I use this.getBounds() in the constructor of the JPanel class):
java.awt.Rectangle[x=0,y=0,width=0,height=0]
At what point in the rendering of a Swing component does this actually get set? Is there an component event that happens after sizing of the bounds is made?
In the constructor of a GUI, the components have not yet been rendered, and so their bounds will be [0, 0]. The components only get rendered after calling pack() or setVisible(true) on the top level window such as a containing JFrame. But your question begs the question of why do you need this information here? What will you do plan to do with this data? There are other locations where the information can be obtained and can be useful, such as a JComponent's paintComponent method or in a ComponentListener.
Okay, thanks for the response, but I figured it out. Basically I need to override the paint() method from JComponent. At this point the rectangle bounds is properly produced. It really wasn't that complicated at all.
UPDATE:
Based on http://www.oracle.com/technetwork/java/painting-140037.html, it recommends using paintComponent() over paint() even though the API does allow it. This deals specifically with the component itself rather than all the related elements like borders and such.

java double buffer doesn't erase

I am doing a project with double buffering. When I paint, it simply paints on top of the old layers, but I need to erase them. Repaint() didn't work, but I'm guessing something equally as simple is the answer.
Any ideas?
Added code, and now it disappears, but it erases the background color.
public void paint(Graphics g)
{
super.paint(buffer);
for(Projectile p: projectiles)
drawRectImage(buffer, p.image, p.getRectangle());
}
Suggestions:
If this is a Swing GUI, then don't override the paint method, but instead override the paintComponent method. This won't help your current problem, but will help prevent future problems including problems with painting of borders and child components.
If Swing (again you don't say), then make sure that your painting component extends JPanel, not JComponent, since JPanel is opaque and fills its background rectangle in its super method.
If it's not Swing, then you should strongly consider changing from AWT to Swing.
If you're still stuck, then yep, you'll want to create and post a minimal example program. Please check out the link.

How to paint graphics from multiple classes to a single panel

At the moment, I have two JPanel classes that draw images and shapes to a JFrame (I will have more in the future), I'm doing this to make things organised.
At first I tried to add each JPanel to the JFrame, but one JPanel would paint over the other.
Each class should be able to call other classes that may draw images to screen as well.
The problem I have is that I cannot get them to draw to the screen.
Should I use paintComponent or paintAll? And how should they be used?
Thank you for any help :)
It sounds as though you are adding both panels to the same location in the JFrame probably in the BorderLayout.CENTER position. One solution is to use a GridLayout with 2 columns for the JFrame and add the 2 panels.
paintComponent is the correct method to override in your panels.
Follow the custom painting trail to see how it should be used.

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.

How to make a Java Canvas look raised from its container

I have a simple GUI component written in Java. The class draws an analog clock in a java.awt.canvas.
This canvas is then contained in a JFrame - What I want to do is give the canvas a 3d "raised" effect - almost like adding a drop shadow to a photo.
Is there a simple way to do this?
If you are using a JFrame, then you have two options:
Add your own component to a JPanel first and then add this to the JFrame.
Instead of inheriting from java.awt.Canvas, you can inherit from JComponent. Then you would have to do all your painting in the paintComponent() Method instead of just paint() (you can just rename your current paint method).
In both cases you can now set a border with the setBorder() Method (on the JPanel or your component) you can get from BorderFactory.
See also: How to Use Borders
If you were using a Swing element, you'd use the createRaisedBevelBorder() method of the BorderFactory and set the canvas' border to the resulting border. Canvas is an AWT component, so you'll need to wrap it in a Swing component to which you can set the border.

Categories

Resources