Setting Background in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Macro 90G
    New Member
    • Apr 2008
    • 3

    Setting Background in java

    hi everyone!
    i am using bluej and i want to set a photo as my background by using imagePanel, i think. However, i don't know how to do. can someone help me out giving some code for this as an example. thanks alot.
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Originally posted by Macro 90G
    hi everyone!
    i am using bluej and i want to set a photo as my background by using imagePanel, i think. However, i don't know how to do. can someone help me out giving some code for this as an example. thanks alot.
    I am not familiar with "imagePanel ". What framework are you using?

    Comment

    • Macro 90G
      New Member
      • Apr 2008
      • 3

      #3
      I am using 'JFrame' and i would like to know how to use ImagePanel, ImageFileManage r, OFImage and ImageViewer to set photo as a background of a GUI.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Macro 90G
        I am using 'JFrame' and i would like to know how to use ImagePanel, ImageFileManage r, OFImage and ImageViewer to set photo as a background of a GUI.
        I only know of JFrame being a class in the core set of Java SE classes; are all
        the Image* classes your own or from a third part library? If so you need to consult
        their API documentation for their use because they are not part of the Java SE
        core library.

        kind regards,

        Jos

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by Macro 90G
          I am using 'JFrame' and i would like to know how to use ImagePanel, ImageFileManage r, OFImage and ImageViewer to set photo as a background of a GUI.
          What framework is ImagePanel, ImageFileManage r, OFImage and ImageViewer in?

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            There are a number of ways to get a background image. My favourite way is a bit of a cheat, since I define a Border that draws the image. I like this approach since it doesn't involve subclassing any JComponent type.

            [CODE=Java]import java.awt.*;
            import java.awt.image. *;
            import javax.swing.bor der.*;

            public class CentredBackgrou ndBorder implements Border {
            private final BufferedImage image;

            public CentredBackgrou ndBorder(Buffer edImage image) {
            this.image = image;
            }

            public void paintBorder(Com ponent c, Graphics g, int x, int y, int width, int height) {
            int x0 = x + (width-image.getWidth( ))/2;
            int y0 = y + (height-image.getHeight ())/2;
            g. drawImage(image , x0, y0, null);
            }

            public Insets getBorderInsets (Component c) {
            return new Insets(0,0,0,0) ;
            }

            public boolean isBorderOpaque( ) {
            return true;
            }
            }
            [/CODE]

            [CODE=Java]import java.awt.*;
            import java.io.*;
            import java.net.URL;
            import javax.imageio.* ;
            import javax.swing.*;
            import javax.swing.bor der.*;

            public class BackgroundBorde rExample {
            public static void main(String[] args) throws IOException {
            JFrame.setDefau ltLookAndFeelDe corated(true);
            JFrame f = new JFrame("Backgro undBorderExampl e");
            f.setDefaultClo seOperation(JFr ame.EXIT_ON_CLO SE);
            JTextArea area = new JTextArea(24,80 );
            area.setForegro und(Color.WHITE );
            area.setOpaque( false);
            area.read(new FileReader(new File("Backgroun dBorderExample. java")), null);
            final JScrollPane sp = new JScrollPane(are a);
            sp.setBackgroun d(Color.BLACK);
            sp.getViewport( ).setOpaque(fal se);
            f.getContentPan e().add(sp);
            f.setSize(600,4 00);
            f.setLocationRe lativeTo(null);
            f.setVisible(tr ue);

            String url = "http://blogs.sun.com/jag/resource/JagHeadshot.jpg ";
            final Border bkgrnd = new CentredBackgrou ndBorder(ImageI O.read(new URL(url)));
            Runnable r = new Runnable() {
            public void run() {
            sp.setViewportB order(bkgrnd);
            sp.repaint();
            }
            };
            SwingUtilities. invokeLater(r);
            }
            }
            [/CODE]

            In the above example (where orange really isn't my best colour) I used the JScrollPane method setViewportBord er. For other containers, you typically use setBorder.

            Comment

            Working...