Subject: Background Image for GUI
Posted By: Floetic Post Date: 4/16/2008 7:52:47 AM
Im looking to put a background image on the gui using a photo from my pc, can anyone help me please?

Thanks.


package newpackage10;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class NewClass10 extends JFrame{
    
    public NewClass10() {
      initiateComponents(); 
    }
    
    private JPanel innerPanel;
    
    private JMenuBar MainMenuBar;
      
    private JMenu FileMenu;
    private JMenuItem NewScenarioMenuItem;
    private JMenuItem OpenScenarioMenuItem;
    private JMenuItem SaveScenarioMenuItem;
    private JMenuItem ClearOutputWindowMenuItem;
    private JMenuItem ExitMenuItem;
    
    private JMenu ScenarioMenu;
    private JMenuItem EditScenarioMenuItem;
    private JMenuItem CheckSyntaxScenarioMenuItem;
    private JMenuItem OpenScenarioGeneratorMenuItem;
    
    private JMenu RunMenu;
    private JMenuItem RunSimulationMenuItem;
    private JMenuItem MakeQueryMenuItem;
    
    private JMenu HelpMenu;
    private JMenuItem ReadMeMenuItem;
    private JMenuItem AboutMenuItem;
    
    private JLabel TitleLabel;
    
    private JLabel AlgorithmLabel;
    private JLabel ScenarioLabel;
    private JLabel GoalLabel;
    private JLabel TimeLabel;
    private JLabel PredicateLabel;
    
    private JTextField AlgorithmTextField;
    private JTextField ScenarioTextField;
    private JTextField GoalTextField;
    private JTextField TimeTextField;
    private JTextField PredicateTextField;
    
    private JButton SearchButton;
    private JButton HoldsButton;
    
    private JTextArea OutputWindow;
    
    private void initiateComponents() {
        getContentPane().setLayout(null);
        setTitle("Dynamic Environment Reasoning System");
        setSize(new Dimension(1025, 703));
        setLocation(0,0);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        getContentPane().setBackground(new Color(0,191,255));
   
        getContentPane().add(getInnerPanel());
        setJMenuBar(getMainMenuBar());
        getContentPane().add(getTitleLabel());
        populateInnerPanel();
        
    }

    private JMenuBar getMainMenuBar() {
        MainMenuBar = new JMenuBar();
                
        FileMenu = new JMenu();
        //FileMenu.setBorder(new TitledBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Times New Roman", 1, 12))));
        FileMenu.setText(" File");
        FileMenu.setFont(new Font("Bookman Old Style",1,14));
        FileMenu.setPreferredSize(new Dimension(49,20));
        MainMenuBar.add(FileMenu);
        
        NewScenarioMenuItem = new JMenuItem();
        NewScenarioMenuItem.setText("New Scenario");
        NewScenarioMenuItem.setFont(new Font("Bookman Old Style",0,14));
        NewScenarioMenuItem.setPreferredSize(new Dimension(120,20));
        FileMenu.add(NewScenarioMenuItem);
        
        OpenScenarioMenuItem = new JMenuItem();
        OpenScenarioMenuItem.setText("Open Scenario");
        OpenScenarioMenuItem.setFont(new Font("Bookman Old Style",0,14));
        OpenScenarioMenuItem.setPreferredSize(new Dimension(120,20));
        FileMenu.add(OpenScenarioMenuItem);
        
        SaveScenarioMenuItem = new JMenuItem();
        SaveScenarioMenuItem.setText("Save Scenario");
        SaveScenarioMenuItem.setFont(new Font("Bookman Old Style",0,14));
        SaveScenarioMenuItem.setPreferredSize(new Dimension(120,20));
        FileMenu.add(SaveScenarioMenuItem);
        
        ClearOutputWindowMenuItem = new JMenuItem();
        ClearOutputWindowMenuItem.setText("Clear Output Window");
        ClearOutputWindowMenuItem.setFont(new Font("Bookman Old Style",0,14));
        ClearOutputWindowMenuItem.setPreferredSize(new Dimension(170,20));
        FileMenu.add(ClearOutputWindowMenuItem);
        
        ExitMenuItem = new javax.swing.JMenuItem();
        ExitMenuItem.setText("Exit");
        ExitMenuItem.setFont(new Font("Bookman Old Style",0,14));
        ExitMenuItem.setPreferredSize(new Dimension(120,20));
        FileMenu.add(ExitMenuItem);
    
        ScenarioMenu = new javax.swing.JMenu();
        ScenarioMenu.setText("Scenario");
        ScenarioMenu.setFont(new Font("Bookman Old Style",1,14));
        ScenarioMenu.setPreferredSize(new Dimension(73,20));
        MainMenuBar.add(ScenarioMenu);
        
        EditScenarioMenuItem = new JMenuItem();
        EditScenarioMenuItem.setText("Edit Scenario");
        EditScenarioMenuItem.setFont(new Font("Bookman Old Style",0,14));
        EditScenarioMenuItem.setPreferredSize(new Dimension(110,20));
        ScenarioMenu.add(EditScenarioMenuItem);
        
        CheckSyntaxScenarioMenuItem = new JMenuItem();
        CheckSyntaxScenarioMenuItem.setText("Check Scenario Syntax");
        CheckSyntaxScenarioMenuItem.setFont(new Font("Bookman Old Style",0,14));
        CheckSyntaxScenarioMenuItem.setPreferredSize(new Dimension(180,20));
        ScenarioMenu.add(CheckSyntaxScenarioMenuItem);
        
        OpenScenarioGeneratorMenuItem = new JMenuItem();
        OpenScenarioGeneratorMenuItem.setText("Open Scenario Generator");
        OpenScenarioGeneratorMenuItem.setFont(new Font("Bookman Old Style",0,14));
        OpenScenarioGeneratorMenuItem.setPreferredSize(new Dimension(200,20));
        ScenarioMenu.add(OpenScenarioGeneratorMenuItem);
    
        RunMenu = new javax.swing.JMenu();
        RunMenu.setText("  Run");
        RunMenu.setFont(new Font("Bookman Old Style",1,14));
        RunMenu.setPreferredSize(new Dimension(74,20));
        MainMenuBar.add(RunMenu);
        
        RunSimulationMenuItem = new JMenuItem();
        RunSimulationMenuItem.setText("Run Simulation");
        RunSimulationMenuItem.setFont(new Font("Bookman Old Style",0,14));
        RunSimulationMenuItem.setPreferredSize(new Dimension(130,20));
        RunMenu.add(RunSimulationMenuItem);
        
        MakeQueryMenuItem = new JMenuItem();
        MakeQueryMenuItem.setText("Make Query");
        MakeQueryMenuItem.setFont(new Font("Bookman Old Style",0,14));
        MakeQueryMenuItem.setPreferredSize(new Dimension(110,20));
        RunMenu.add(MakeQueryMenuItem);
        
        HelpMenu = new javax.swing.JMenu();
        HelpMenu.setText("Help");
        HelpMenu.setFont(new Font("Bookman Old Style",1,14));
        HelpMenu.setPreferredSize(new Dimension(80,20));
        MainMenuBar.add(HelpMenu);
        
        ReadMeMenuItem = new JMenuItem();
        ReadMeMenuItem.setText("Read Me");
        ReadMeMenuItem.setFont(new Font("Bookman Old Style",0,14));
        ReadMeMenuItem.setPreferredSize(new Dimension(77,20));
        HelpMenu.add(ReadMeMenuItem);
        
        AboutMenuItem = new JMenuItem();
        AboutMenuItem.setText("About");
        AboutMenuItem.setFont(new Font("Bookman Old Style",0,14));
        AboutMenuItem.setPreferredSize(new Dimension(77,20));
        HelpMenu.add(AboutMenuItem);
        return MainMenuBar;
    }

    private JLabel getTitleLabel() {
        TitleLabel = new JLabel();
        TitleLabel.setFont(new Font("Arial",1,16));
        TitleLabel.setText(" Dynamic Environment Reasoning System");
        TitleLabel.setForeground(new Color(139,0,139));
        TitleLabel.setBackground(new Color(153,153,255));
        TitleLabel.setBorder(BorderFactory.createRaisedBevelBorder());
        TitleLabel.setBounds(600,20,340,30);
        return TitleLabel;
    }

    private JPanel getInnerPanel() {
        innerPanel = new JPanel();
        innerPanel.setLayout(null);
        innerPanel.setBackground(new Color(127,255,212));
        innerPanel.setBorder(BorderFactory.createLoweredBevelBorder());
        innerPanel.setBounds(26,75,960,550);
        return innerPanel;
    }

    private void populateInnerPanel() {
        AlgorithmLabel = new JLabel();
        AlgorithmLabel.setBackground(new Color(255,255,240));
        AlgorithmLabel.setFont(new Font("Bookman Old Style",0,13));
        AlgorithmLabel.setText("Selected Inference Algorithm:");
        innerPanel.add(AlgorithmLabel);
        AlgorithmLabel.setBounds(20,10,210,30);
            
        ScenarioLabel = new JLabel();
        ScenarioLabel.setBackground(new Color(255,255,240));
        ScenarioLabel.setFont(new Font("Bookman Old Style",0,13));
        ScenarioLabel.setText("Selected Scenario:");
        innerPanel.add(ScenarioLabel);
        ScenarioLabel.setBounds(260,10,170,30);
        
        GoalLabel = new JLabel();
        GoalLabel.setBackground(new Color(255,255,240));
        GoalLabel.setFont(new Font("Bookman Old Style",0,13));
        GoalLabel.setText("Selected Goal:");
        innerPanel.add(GoalLabel);
        GoalLabel.setBounds(460,10,170,30);
        
        TimeLabel = new JLabel();
        TimeLabel.setBackground(new Color(255,255,240));
        TimeLabel.setFont(new Font("Bookman Old Style",0,13));
        TimeLabel.setText("Selected Time:");
        innerPanel.add(TimeLabel);
        TimeLabel.setBounds(630,10,170,30);
        
        PredicateLabel = new JLabel();
        PredicateLabel.setBackground(new Color(255,255,240));
        PredicateLabel.setFont(new Font("Bookman Old Style",0,13));
        PredicateLabel.setText("Predicate:");
        innerPanel.add(PredicateLabel);
        PredicateLabel.setBounds(810,10,170,30);
    
        AlgorithmTextField = new JTextField();
        AlgorithmTextField.setBackground(new Color(255,255,240));
        innerPanel.add(AlgorithmTextField);
        AlgorithmTextField.setBounds(40,60,100,20);
               
        ScenarioTextField = new JTextField();
        ScenarioTextField.setBackground(new Color(255,255,240));
        innerPanel.add(ScenarioTextField);
        ScenarioTextField.setBounds(270,60,100,20);
        
        GoalTextField = new JTextField();
        GoalTextField.setBackground(new Color(255,255,240));
        innerPanel.add(GoalTextField);
        GoalTextField.setBounds(470,60,100,20);
        
        TimeTextField = new JTextField();
        TimeTextField.setBackground(new Color(255,255,240));
        innerPanel.add(TimeTextField);
        TimeTextField.setBounds(640,60,100,20);
        
        PredicateTextField = new JTextField();
        PredicateTextField.setBackground(new Color(255,255,240));
        innerPanel.add(PredicateTextField);
        PredicateTextField.setBounds(820,60,100,20);
        
        SearchButton = new JButton();
        SearchButton.setFont(new Font("Bookman Old Style",1,13));
        SearchButton.setText("Search");
        innerPanel.add(SearchButton);
        SearchButton.setBounds(280,100,90,20);
        
        HoldsButton = new JButton();
        HoldsButton.setFont(new Font("Bookman Old Style",1,13));
        HoldsButton.setText("Holds");
        innerPanel.add(HoldsButton);
        HoldsButton.setBounds(830,100,80,20);
    
        OutputWindow = new JTextArea();
        OutputWindow.setBackground(new Color(255,255,240));
        Font equalSpacedFont = new Font("Monospaced",Font.PLAIN,14);
        OutputWindow.setFont(equalSpacedFont);
        OutputWindow.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(OutputWindow);
        scrollPane.setBounds(30,145,900,380);
        innerPanel.add(scrollPane);
        
    }
    
    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame.setDefaultLookAndFeelDecorated(true);
                NewClass10 nc10 = new NewClass10();
                nc10.setVisible(true);
            }
        }); 
    }
}



Reply By: jomet Reply Date: 4/16/2008 11:04:49 PM
check,

http://technocrataditi.wordpress.com/2007/03/16/java-how-to-put-background-image-in-swings/

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class ImagePanel extends JPanel
{
public static final int TILED = 0;
public static final int SCALED = 1;
public static final int ACTUAL = 2;

private BufferedImage image;
private int style;
private float alignmentX = 0.5f;
private float alignmentY = 0.5f;

public ImagePanel(BufferedImage image)
{
this(image, TILED);
}

public ImagePanel(BufferedImage image, int style)
{
this.image = image;
this.style = style;
setLayout( new BorderLayout() );
}

public void setImageAlignmentX(float alignmentX)
{
this.alignmentX = alignmentX > 1.0f ? 1.0f : alignmentX < 0.0f ? 0.0f : alignmentX;
}

public void setImageAlignmentY(float alignmentY)
{
this.alignmentY = alignmentY > 1.0f ? 1.0f : alignmentY < 0.0f ? 0.0f : alignmentY;

}

public void add(JComponent component)
{
add(component, null);
}

public void add(JComponent component, Object constraints)
{
component.setOpaque( false );

if (component instanceof JScrollPane)
{
JScrollPane scrollPane = (JScrollPane)component;
JViewport viewport = scrollPane.getViewport();
viewport.setOpaque( false );
Component c = viewport.getView();

if (c instanceof JComponent)
{
((JComponent)c).setOpaque( false );
}
}

super.add(component, constraints);
}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);

if (image == null ) return;

switch (style)
{
case TILED :
drawTiled(g);
break;

case SCALED :
Dimension d = getSize();
g.drawImage(image, 0, 0, d.width, d.height, null);
break;

case ACTUAL :
drawActual(g);
break;
}
}

private void drawTiled(Graphics g)
{
Dimension d = getSize();
int width = image.getWidth( null );
int height = image.getHeight( null );

for (int x = 0; x < d.width; x += width)
{
for (int y = 0; y < d.height; y += height)
{
g.drawImage( image, x, y, null, null );
}
}
}

private void drawActual(Graphics g)
{
Dimension d = getSize();
float x = (d.width - image.getWidth()) * alignmentX;
float y = (d.height - image.getHeight()) * alignmentY;
g.drawImage(image, (int)x, (int)y, this);
}

public static void main(String [] args)
throws Exception
{
BufferedImage image = javax.imageio.ImageIO.read( new java.io.File(”c:\\aditi.jpg”) );

ImagePanel north = new ImagePanel(image, ImagePanel.ACTUAL);
north.setImageAlignmentY(1.0f);
JTextArea text = new JTextArea(5, 40);
JScrollPane scrollPane = new JScrollPane( text );
north.add( scrollPane );

ImagePanel south = new ImagePanel(image, ImagePanel.SCALED);
JPanel buttons = new JPanel();
buttons.add( new JButton(”One”) );
buttons.add( new JButton(”Two”) );
JPanel boxes = new JPanel();
boxes.add( new JCheckBox(”One”) );
boxes.add( new JCheckBox(”Two”) );
south.add(buttons, BorderLayout.NORTH);
south.add(boxes, BorderLayout.SOUTH);

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add( north, BorderLayout.NORTH );
frame.getContentPane().add( south, BorderLayout.SOUTH );
frame.pack();
frame.setVisible(true);
}
}

jomet.
````````````````````````````````````````````````````````````````````````
Once you start a working on something, dont be afraid of failure and dont abandon it.
People who work sincerely are the happiest.
Reply By: Floetic Reply Date: 4/17/2008 9:54:42 PM
Thanks very much




Go to topic 70691

Return to index page 1