[hotjoe.com] HotJoe Java Help Forums
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Visit java.com
Please send email if you are having login problems - see the posts below for more info.
Hotmail and Yahoo! users - please see the Hotmail post or the Yahoo! post for information on lost emails.
BackgroundPanel.java cannot be placed in BorderLayout.NORTH  XML
Forum Index » New to Java
Author Message
chopficaro

Newbie

Joined: 05/02/2010 08:35:09
Messages: 13
Offline

i am trying to put a picture on the top of my swing GUI that stretches horizontally to fit the width of the window as it resizes

http://www.camick.com/java/source/BackgroundPanel.java
this is the code for a commonly used function that scales images to their components size. it works by overloading the paintcontainer class, which is called every time the window resizes, which then calls the paint class. they simply grab the size of the container and draw the image to those dimensions

the image that needs to be resized must be placed in BorderLayout.NORTH, because the rest of my GUI is in BorderLayout.CENTER, but BackgroundPanel cant be placed in NORTH, the image disappears, even with a minimum size set. it cant be placed inside a panel inside a panel inside NORTH either. ive been reading over the code for BackgroundPanel and its driving me crazy i cant figure out why

here is some short example code, not my program but it simplifies the problem:
[CODE]import java.awt.*;
import javax.swing.*;
import java.awt.event.WindowEvent;

public class JPanel1 extends JFrame
{
public static void main(String[] args)
{
new JPanel1();
}

public JPanel1()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
add(mainJPanel());
pack();
setVisible(true);
}


//debug space center to streatch
private JPanel mainJPanel()
{
JPanel JPanel = new JPanel();
BorderLayout layout = new BorderLayout();
this.setMinimumSize( new Dimension( 224, 224 ) );
JPanel.setLayout(layout);
JPanel.add(mainJPanel2(),BorderLayout.NORTH);
return(JPanel);
}


//debug space center to streatch
private JPanel mainJPanel2()
{
JPanel JPanel = new JPanel();
BorderLayout layout = new BorderLayout();
JPanel.setLayout(layout);
JPanel.add(JPanel2(),BorderLayout.CENTER);
return(JPanel);
}


private JPanel JPanel2()
{
BorderLayout layout = new BorderLayout();
this.setLayout(layout);
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image duke = toolkit.getImage("sun.jpg");
BackgroundPanel panel = new BackgroundPanel(duke, BackgroundPanel.SCALED);
//panel.setMinimumSize( new Dimension( 224, 224 ) );
this.setMinimumSize( new Dimension( 224, 224 ) );
GradientPaint paint = new GradientPaint(0, 0, Color.BLUE, 600, 0, Color.RED);
panel.setPaint(paint);
return panel;
}

//exit when closed
public void processWindowEvent(WindowEvent event)
{
if(event.getID() == WindowEvent.WINDOW_CLOSING)
System.exit(0);
}

}[/CODE]
 
Forum Index » New to Java
Go to:   
Powered by JForum 2.1.9 © JForum Team
This site run by Scott Dunbar of Xigole Systems. © 2005-2011 - Scott Dunbar
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners
hotjoe.com, xigole.com, and Scott Dunbar have no affiliation with Oracle