Frame, Panel, Componant Formatting.
My goal is to have a Frame that when it resizes the left and Right Panel take up 50% each and the Left panel should show a image that takes up the whole panel this is my Code:
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.Dimension;
publicclass MyFrame
{
private JPanel myLeftPanel =null;
private JPanel myRightPanel =null;
private JFrame frame =null;
public MyFrame(JPanel leftPanel, JPanel rightPanel)
{
myLeftPanel = leftPanel;
myRightPanel = rightPanel;
makeFrame();
addPanels();
}
publicvoid swapPanels(JPanel leftPanel, JPanel rightPanel)
{
myLeftPanel = leftPanel;
myRightPanel = rightPanel;
addPanels();
}
publicvoid makeFrame()
{
JFrame myFrame =new JFrame();
frame = myFrame;
frame.setSize(500, 500);
frame.setTitle("Nasa Program");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicvoid addPanels()
{
frame.getContentPane().setLayout(new FlowLayout());
myLeftPanel.setPreferredSize(new Dimension(frame.getWidth()/2, frame.getHeight()));
frame.getContentPane().add(myLeftPanel);
frame.getContentPane().add(myRightPanel);
}
}
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JButton;
import javax.swing.JComponent;
import java.awt.*;
import java.io.IOException;
import java.net.MalformedURLException;
publicclass LeftPanelEarthextends JPanel
{
public LeftPanelEarth()
{
Image map =null;
try{
map = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource("calgary_small.gif"),"calgary_small.gif"));}
catch (MalformedURLException mue){}
catch (IOException ioe){}
MapComponant mapPanel =new MapComponant(map);
mapPanel.setPreferredSize(this.getSize());/**REFERANCE POINT */
this.add(mapPanel);
}
publicclass MapComponantextends JComponent
{
Image map =null;
public MapComponant (Image mapGiven)
{
map = mapGiven;
}
publicvoid paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
if (map !=null)
{
g2.drawImage(map,
0,
0,
getWidth(),
getHeight(),
this);
}
}
}
}
And the Right Panel just has a Button that does appear.
So what I end up with is that I get a frame with the left side empty and the Right side with the button. If i replace the "REFERANCE POINT" above with values such asnew Dimension(200,200)the image will appear on the left side but It is not drawn in the (0,0) part of the Panel like it is soppose to.
So I can't get the Image to take the intire Panel up and When I give it a Dimension it dosent draw it at (0,0) anyone know whats going on?
Frame>2Panels (Left Panel is given Dimension (Frame/2, Frame))>Image is given(Dimension (Panel, Panel))
I dunno why it isn't woking.

