Required null layout, for drawing + labels - java

I am making a small JFrame in which I draw a large triangle. On all the sides of the triangle there has to be a label. I have to position these labels myself, because of the irregular shape of the triangle (yes, there will also be a non-right-angled triangle). (I have specified setResizable(false) on my JFrame, so there is no need for multiple sizes.)
Would there be any way to manually set the location of all my labels, WITH a layout manager?

If you want to have a canvas in the middle and then labels on any of the 4 sides, you could use a BorderLayout, like so:
JPanel framePanel = new JPanel(new BorderLayout());
JPanel triangleCanvas = ...
framePanel.add(triangleCanvas, BorderLayout.CENTER);
JPanel northLabels = ...
framePanel.add(northLabels, BorderLayout.PAGE_START);
JPanel southLabels = ...
framePanel.add(southLabels, BorderLayout.PAGE_END);
JPanel eastLabels = ...
framePanel.add(eastLabels, BorderLayout.LINE_END);
JPanel westLabels = ...
framePanel.add(westLabels, BorderLayout.LINE_START);
frame.getContentPane().add(framePanel);
The tricky part will be if you want to somehow align where your labels are depending on the currently drawn triangle (or whatever is in the container), but hopefully you don't want to do that.
How to Use BorderLayout

You say that you want to draw the labels at irregular places e.g. at corners and at the sides the dhapes. Then I would recommend to use drawString(String s, int x, int y) in the Graphics2D API. So place the labels and draw then when you draw the figure.
See Lesson: Working with Text APIs for more advanced options like e.g. Fonts and Font metrics.
When you want to specify the exact position of labels, is the only case when you shouldn't use an Layout Manager.

Related

Can I add a rectangle to a Jpanel in java swing and specify constraints for it using GridBagConstraints?

I'm pretty new to GUI and Java as a whole so I hope that I can explain this well enough and understand people's answers.
For a school project, I need to put a bunch of stuff on some rectangles but I'm having issues even adding one rectangle properly.
From researching online, this is what I have (the JPanel and GridBagConstraints are just there to show what I'd like to use):
public class GUI extends JPanel
{
public static void main (String [] args)
{
GUI g = new GUI();
JFrame window = new JFrame("Java Window");
window.setSize(1280, 960);
window.add(g);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel layout = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
}
#Override
public void paintComponent(Graphics g)
{
Color boxColour = new Color(194, 190, 190);
super.paintComponent(g);
g.setColor(boxColour);
g.fillRect(10, 10, 100, 100);
}
}
So right now, the rectangle appears in the window. But how can I add constraints to it? Is that possible? I would think that I should use JPanel to keep everything more organized since there will be many components so I tried adding this:
layout.add(g);
window.add(layout);
window.setVisible(true);
However, the rectangle no longer appeared. What am I doing wrong and how can I fix it and add constraints to my shapes? Thanks!
In your first scenario, the default layout manager of the frame is the BorderLayout and you are adding your "g" panel to the BorderLayout.CENTER. So based on the rules of the BorderLayout your "g" panel will take up all the space available in the frame. So you have plenty of space to paint your rectangle.
However, in your second scenario, your "layout" panel is using the default layout manager of a JPanel which is a FlowLayout which respects the preferred size of any component added to it.
The preferred size of the "g" panel is 10 x 10. So, when you add the "g" panel to the "layout" panel there is nothing to see because all you custom painting is done outside the bounds of the panel.
You need to override the getPreferredSize() method of your GUI panel to return a preferred size of (120, 120) so you can see your rectangle painted with a 10 pixel border around all the edges.
Read the section from the Swing tutorial on Custom Painting for more information and working examples demonstrating how to override the `getPreferredSize() method.
You will also need to read the Swing tutorial on How to Use GridBagLayout for example of using the constraints to add multiple components.

Center Panel's Component Within a java swing BoxLayout

I'm trying to use a BoxLayout to display 2 panels vertically and I searched how to center the components in those panels. At the moment, my components are placed on the top center of each panel, and I want to get them at the center X and Y.
I added the components I want in the 2 panels, then I added the panels in my BoxLayout. This way they're displayed vertically as I want them to be, but as I said I don't want them to be on top center.
I tried to use methods such as setAlignementY and setLocation but any of them actually moves the components. I also saw that a BoxLayout will try to set the components as wide as the widest component, but as I have only 2 panels which have the same size I don't really understand it.
This is basically how I've added my components (without trying to center) :
private void initPanels ()
{
this.titlePanel.add(this.title);
this.bookInputPanel.add(bookTitle);
this.bookInputPanel.add(bookInput);
this.authorInputPanel.add(by);
this.authorInputPanel.add(authorInput);
this.authorInputPanel.add(this.authorsTable);
this.buttonsPanel.add(confirm);
this.contentPanel.setLayout(new BoxLayout(this.contentPanel, BoxLayout.Y_AXIS));
this.contentPanel.add(bookInputPanel);
this.contentPanel.add(authorInputPanel);
this.add(this.titlePanel, BorderLayout.NORTH);
this.add(this.contentPanel, BorderLayout.CENTER);
this.add(this.buttonsPanel, BorderLayout.SOUTH);
}
I made a picture to show you exactly what I want but it seems that I need 10 rep to do it, sorry about that.
This way they're displayed vertically as I want them to be, but as I said I don't want them to be on top center.
One way is to add "glue" to the top/bottom of the panel. This "glue" will expand to fill the extra space available to the panel:
this.contentPane.add(Box.createVerticalGlue());
this.contentPanel.add(bookInputPanel);
this.contentPanel.add(authorInputPanel);
this.contentPane.add(Box.createVerticalGlue());
Read the section from the Swing tutorial on How to Use BoxLayout for more information about the feature of a BoxLayout.
Another option might be to use a "wrapper" panel that uses a different layout manager. For example the GridBagLayout with the default constraints will automatically center the component horizontally/vertically:
//this.add(this.contentPanel, BorderLayout.CENTER);
JPanel wrapper = new JPanel( new GridBagLayout() );
wrapper.add( contentPanel );
this.add(wrapper, BorderLayout.CENTER);

Java setOpaque method not working to add panels over the top of others

I am attempting to place two panels over the top of another two panels, so the positions match exactly. This will allow me to then paint over the top of the previous(bottom) panels. The reason for doing this, is that I have implemented a "drag a rectangle" zoom function, so on the panel the user will drag the mouse to draw a rectangle, and then the panel will become filled with the contents of that rectangle. However, I have now decided I would like include a visual representation of the rectangle as the user drags it.
Since I (assume) I cannot draw the rectangle on the bottom panel, since it already has a complicated paint on it anyway, I figured I would be able to draw it onto another panel that I would place on top. (If there is any easier way please shout!)
I am happy I have been able to get the panels into the correct position, using a BorderLayout/GridLayout combo, without the need for absolute positing, however the setOpaque method is not working on any of the three panels I have added. Does anyone know why this is?
Many thanks in advance, code below:
public void initiate() {
JPanel panel1 = new JPanel();
mandPanel mandDisplay = new mandPanel();
juliaPanel juliaDisplay = new juliaPanel();
JPanel detailsPanel = new JPanel();
JPanel bottomDetails = new JPanel();
JPanel panelForDisplays = new JPanel();
JPanel listenerPanelHolder = new JPanel();
JPanel listenerPanel = new JPanel();
JPanel blankPanel = new JPanel();
panelForDisplays.setLayout(new GridLayout(1,2));
detailsPanel.setLayout(new FlowLayout());
bottomDetails.setLayout(new FlowLayout());
panel1.setLayout(new BorderLayout());
panelForDisplays.add(mandDisplay);
panelForDisplays.add(juliaDisplay);
listenerPanelHolder.setOpaque(false);
listenerPanel.setOpaque(false);
blankPanel.setOpaque(false);
listenerPanelHolder.setLayout(new GridLayout(1,2));
listenerPanelHolder.add(listenerPanel);
listenerPanelHolder.add(blankPanel);
panel1.add(detailsPanel,BorderLayout.NORTH);
panel1.add(panelForDisplays,BorderLayout.CENTER);
panel1.add(listenerPanel,BorderLayout.CENTER);
panel1.add(bottomDetails, BorderLayout.SOUTH);
setContentPane(panel1);
setSize(width*2, height);
setResizable(false);
this.addWindowListener(new onClose(savedJulias));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
PS: A lot of code is omitted to avoid mess, but if anyone thinks they could solve with more code please say! Thanks :)
First off, adding two components at the BorderLayout.CENTER position replaces the first component with the 2nd, and so the 2nd doesn't cover the 1st, it displaces it. As per the BorderLayout API:
Each region may contain no more than one component,...
But having said that, as I understand your problem in greater detail, I think that your best solution is not to use JLayeredPane or to overlay two JPanels. The best solution is in fact to draw directly on the image-displaying JPanel. This is not hard to do, and shouldn't over complicate your code. For example.
You can use JLayeredPane to place one component to the top of the other. You can check this

Fill JPanel only to the height of inner elements

I'm trying to achieve a design whereby one of my JPanel's heights is only set to the height of it's inner components. Currently it's laid out like so:
JPanel (BoxLayout)
JPanel (CardLayout)
JPanel (BoxLayout)
JPanel (BoxLayout)
JPanel (FlowLayout)
It currently displays as so, I want the top bit to expand the both on the x and y axis pushing the "Your Name" to above the status bar.
I need to use BoxLayouts it seems so that it flows from top to bottom.
Please help!
I want the top bit to expand the both on the x and y axis pushing the "Your Name" to above the status bar.
Then is sounds like you can use a BorderLayout as the top level panel, instead of the BoxLayout.
Since the default layout manager for the content pane of the frame is a BorderLayout you can just add the panels directly to the content pane of the frame:
frame.add(cardLayoutPanel, BorderLayout.CENTER);
frame.add(flowLayoutPanel, BorderLayout.PAGE_END);
When you add a panel to the "PAGE_END", its preferred height is respected.
When you add a panel to the "CENTER" it gets all the extra spaces available in the frame.
Read the Swing tutorial on Layout Managers for more information and working examples.
I need to use BoxLayouts it seems so that it flows from top to bottom.
You don't need to use a BoxLayout (as demonstrated above), but you can still use it if you want. BoxLayout respects the maximum size of a panel. So you could override the getPreferredSize() method of your panel using the flow layout to return the preferred height of the panel.
#Override
public Dimension getMaximumSize()
{
Dimension preferred = super.getPreferredSize();
Dimension maximum = super.getMaximumSize();
maximum.height = preferred.height;
return maximum;
}
Now the height of the flow panel will be respected and all the extra space will go to the other panel.

Java Swing Panel Size

Hi I have been learning Java Swing for creating a chess game to practice my Java programming skills.
I've added a JPanel to the east of the JFrame with BorderLayout and I've used the setPrefferedSize(new Dimension(x,y)) method to set the width and height.
After that I have created 4 JPanel and added them with BoxLayout on the previously created panel.
I have tried to set the size of the 4 panels with the setSize(x,y) and setPreferredSize(new Dimension(x,y)) but it dosent work the 4 panels automaticly changed there size to fit the main JPanel and after adding a JLabel on one of them the size of it increased automaticly .
This is my code:
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
JPanel a = new JPanel();
a.setPreferredSize(new Dimension(50, 50)); //this dosent work
a.add(min);
a.setBackground(Color.red);
this.add;
JPanel b = new JPanel();
b.setBackground(Color.blue);
this.add(b);
JPanel c = new JPanel();
this.add(c);
JPanel d = new JPanel();
d.setBackground(Color.black);
this.add(d);
How can I change the size of each of these panels?
BoxLayout is best for laying out components with varying sizes along a single axis. From the Javadocs:
"BoxLayout attempts to arrange components at their preferred widths (for horizontal layout) or heights (for vertical layout)."
The idea is that they may have different heights (for a horizontal layout) and it will take the maximum height. And, they definitely may have different widths. Also, BoxLayout works with some, er, "interesting" filler pieces like Box.createHorizontalGlue(). These are actually quite useful for flexible, resizeable layouts once you get the hang of it. But, all in all, BoxLayout is for flexible, resizable layout of items with differing sizes.
For simpler cases, especially if you want both preferred width and preferred height to be "respected", use GridLayout as everybody else has suggested.

Categories

Resources