Java JPanel Paint Scaling - java

Following code work for visualizing a molecule on a JPanel except it does not rescale when I change the size of JPanel at runtime. The Chemistry Development Kit is used for generating rendered Image. A molecule is passed to MoleculeViewer for visualizing. What am I doing wrong??

What am I doing wrong??
why you needed to setSize(new java.awt.Dimension(400, 400));
put your image = new BufferedImage(this.WIDTH, this.HEIGHT, BufferedImage.TYPE_INT_RGB); as Icon to the JLabel, then you can remove anything about paintComponent()
then you can return JLabel instead of JPanel, but JLabel is translucent by default, or put JLabel to the JPanel by using proper LayoutManager in this case BorderLayout.CENTER
you have to check how way you added MoleculeViewer, what LayoutManager is there used???, because only usage of LayoutManager can this job correctly to resize (or not resize) Container's childs with Top-Level Container,
MoleculeViewer must retuns PreferredSize for its parent

Adding following resolved the not redrawing upon scaling problem
renderer.paint(molecule_, new AWTDrawVisitor(g2), new Rectangle2D.Double(0, 0, w, h), false);
g2.dispose();
}
else

Related

How can can I develop a JFrame that has different parts of transparency

Basically I don't know how to make a JFrame which has a 100% transparent center and is filled gray outside. I looked at the whole Internet how to do that. You should see the window behind this application or even it is possible to practically reach through that window.
What I have done so far was creating a transparent jframe, but then I tried to create a BufferedImage but after that I don't know how to subtract the area of a rectangle from the BufferedImage. So the center of the application is transparent.
I dont know how to make a JFrame which has a 100% transparent center and is filled gray outside
Add a transparent panel with a Border to the transparent frame.
Border border = BorderFactory.createMatteBorder(10, 10, 10, 10, new Color(128, 128, 128, 64));
JPanel panel = new JPanel();
panel.setOpaque( false );
panel.setBorder( border );
frame.add( panel );

Why does my JPanel look gray with a certain size but not with another?

I have a JLayeredPane with 2 JPanels inside of it.
The one on top has a semi transparant background color:
this.setBackground(new Color(0, 0, 0, 150));
Now when i set the size of this JPanel to be equal to the size of the frame
`
this.ghostPanel.setSize(width, height);
My whole JPanel turns gray, but when i give it height - 1, It displays the correct transparant view.
this.ghostPanel.setSize(width, height - 1);
I don't really understand why this happens. I would love to find a valid explanation!
Thanks in advance!
Swing components are either opaque or transparent, Swing doesn't know how to paint components which have a alpha based color.
By using a alpha based color, Swing doesn't know that it should paint the components below it when the component is updated, so you be up with some weird paint artefacts
Instead, make the component transparent (setOpaque(false)), then override it's paintComponent method and use a AlphaComposite to fill the background

Adding JPanel to JFrame with FlowLayout

I am trying to display a filled oval on the screen using a subclass of JPanel
when I try to add an object of this subclass to a JFrame with FlowLayout the oval is not displayed correctly I don't know what the problem is. Could you help me please?
here's my code
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillOval(0, 0, 50, 50);
}
in main
JFrame frame = new JFrame("Ball");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setLayout(new FlowLayout());
BallPanel ball = new BallPane();
frame.add(ball);
See the Javadoc of the FlowLayout:
A flow layout lets each component assume its natural (preferred) size
Since you do some custom painting, the BallPanel cannot compute its preferred size (which is based on the components you add the panel). So override in the BallPanel class the getPreferredSize method to return correct dimensions (correct means matching your custom painting). Consider doing the same for the getMinimumSize and getMaximumSize.
What I always use when I have to debug Swing layout problems is to add borders to my components in specific colors. That way you clearly see the size your components take in their parent container, which might be a good starting point for debugging. In your case, it would show a very tiny panel ;-)
When you create any custom component and do custom painting you need to override the getPreferredSize() method to return the size of the custom component. Ideally you would also overrid the minimum/maximum sizes as well.
Then the layout manager can do its job.

AutoScroll when Shape is outside a JPanel

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.

How to create border around an image and NOT on around the JLabel?

The size of the JLabel is larger than the size of the image.
With the code below, the border is created around the JLabel and not around the image.
How can I created border on the image and Not on the JLabel?
ImageIcon icon;
Border border = BorderFactory.createLineBorder(Color.RED,5);
Image image = icon.getImage().getScaledInstance(widthX,heightY, Image.SCALE_SMOOTH);
icon.setImage(image);
JLabel label = new JLabel(icon);
label.setBorder(border);
Create a BorderedBufferedImage that accepts an int for borderThickness, a Color for the borderColor, and a BufferedImage. Create a new BufferedImage based on the new size (with size increased by 2 x borderThickness), draw the border, then draw the image inside.
Use the BorderedBufferedImage for the JLabel.
The size of the JLabel is larger than the size of the image.
Why? Is it a problem with your layout manager? Or is this a wierd requirement.
You don't appear to be using any text, just an image, so I would just add the label to another panel that uses a FlowLayout. That way if the layout manager resizes the component only the panel will be resized, not the entire label.
If you need more help then post your SSCCE that demonstrates the problem so we can better understand your requirement.
You could create an implementation of the Icon interface which accepts a border thickness and a ImageIcon to which image drawing is delegated.
Use labelname.setBorder(BorderFactory.createLineBorder(color.BLACK)).

Categories