Hi,
I have done my ordering class. which contains all the components.
My problems is how do I set picture as the background and maximize the picture size?

either one of the way

1) i have a class that contains the picture program. How to connect the two program?
the pic as the background for the ordering class.
/**
* @(#)orderingPic.java
*
*
* @author
* @version 1.00 2010/10/4
*/

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class orderingPic {
public static void main(String[] args) {
ImagePanel panel = new ImagePanel(new ImageIcon("all.jpg").getImage());

JFrame frame = new JFrame("Fruit Splash Online system ~");
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}

class ImagePanel extends JPanel {

private Image img;

public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}

public ImagePanel(Image img) { this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);

}

public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}

}
2) add picture to the ordering class.

Recommended Answers

All 8 Replies

Not sure what your problem is here - just use ImagePanel instead of JPanel as the container for all your components. You'll have to make sure they are all setOpaque(false) for the image to show through.

You can adjust the image size to fit the panel by calling getScaledInstance(...) on your image to return a scaled version in the size you want. Don't forget to re-do this when the panel is resized!

I am using JFrame. btw I am currently pursuing my diploma.
JamesCherrill, can you show me how to background pic to

/**
 * @(#)contact.java
 *
 *
 * @author 
 * @version 1.00 2010/9/19
 */

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

public class contact extends JFrame implements ActionListener {
JLabel title;
JButton b1,b2,b3;

    public contact() {
        JFrame f = new JFrame("Choose Your Flavour Online Ordering System.");
        f.setLayout(new BorderLayout());
        Container c=f.getContentPane();    

        //informations
        JPanel p1=new JPanel();   
        title=new JLabel("Branches contact:");
        p1.add(title);

        //buttons   
        JPanel p2=new JPanel();
        b1=new JButton("Previous");
        b2=new JButton("Cancel");
        b3=new JButton("Next");
        p2.add(b1);
        p2.add(b2);
        p2.add(b3);
        //add action listener

        //setting
        c.add("Center",p1);
        c.add("South",p2);
        f.setSize(700,400);
        f.setLocation(600,300);
        f.show(); 
    }

public static void main(String args[]){
    new contact();
}    
}

OK, if you have multiple panels in your JFrame, you need to paint the background of the JFrame itself. Change ImagePanel to extend JFrame instead of JPanel, and use an ImagePanel instead of a JFrame as the parent of your class contact (ps should be Contact). Remember to make everything else setOpaque(false);

Please post any future code in code tags.

is there any simpler way of setting picture as a background?

How simple do you want it?
You just have to change two class names in your existing code. I explained exactly what you need to do.
No, there isn't a simpler way.

okay thank you.
so i need to add a init()method to all the graphics right? do i need to extends JFrame and JApplet?

The code you posted before was just for a JFrame, so no init() is needed.
If you want to do an applet version as well then you'll need to extend JApplet and write an init(). I suggest you get the JFrame version working before starting on the applet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.