473,513 Members | 2,454 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting Background in java

4 New Member
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.
Apr 27 '08 #1
5 4043
BigDaddyLH
1,216 Recognized Expert Top Contributor
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?
Apr 28 '08 #2
Macro 90G
4 New Member
I am using 'JFrame' and i would like to know how to use ImagePanel, ImageFileManager, OFImage and ImageViewer to set photo as a background of a GUI.
Apr 29 '08 #3
JosAH
11,448 Recognized Expert MVP
I am using 'JFrame' and i would like to know how to use ImagePanel, ImageFileManager, 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
Apr 29 '08 #4
BigDaddyLH
1,216 Recognized Expert Top Contributor
I am using 'JFrame' and i would like to know how to use ImagePanel, ImageFileManager, OFImage and ImageViewer to set photo as a background of a GUI.
What framework is ImagePanel, ImageFileManager, OFImage and ImageViewer in?
Apr 29 '08 #5
BigDaddyLH
1,216 Recognized Expert Top Contributor
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.

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.image.*;
  3. import javax.swing.border.*;
  4.  
  5. public class CentredBackgroundBorder implements Border {
  6.     private final BufferedImage image;
  7.  
  8.     public CentredBackgroundBorder(BufferedImage image) {
  9.         this.image = image;
  10.     }
  11.  
  12.     public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  13.         int x0 = x + (width-image.getWidth())/2;
  14.         int y0 = y + (height-image.getHeight())/2;
  15.         g. drawImage(image, x0, y0, null);
  16.     }
  17.  
  18.     public Insets getBorderInsets(Component c) {
  19.         return new Insets(0,0,0,0);
  20.     }
  21.  
  22.     public boolean isBorderOpaque() {
  23.         return true;
  24.     }
  25. }
  26.  
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.io.*;
  3. import java.net.URL;
  4. import javax.imageio.*;
  5. import javax.swing.*;
  6. import javax.swing.border.*;
  7.  
  8. public class BackgroundBorderExample {
  9.     public static void main(String[] args) throws IOException {
  10.         JFrame.setDefaultLookAndFeelDecorated(true);
  11.         JFrame f = new JFrame("BackgroundBorderExample");
  12.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13.         JTextArea area = new JTextArea(24,80);
  14.         area.setForeground(Color.WHITE);
  15.         area.setOpaque(false);
  16.         area.read(new FileReader(new File("BackgroundBorderExample.java")), null);
  17.         final JScrollPane sp = new JScrollPane(area);
  18.         sp.setBackground(Color.BLACK);
  19.         sp.getViewport().setOpaque(false);
  20.         f.getContentPane().add(sp);
  21.         f.setSize(600,400);
  22.         f.setLocationRelativeTo(null);
  23.         f.setVisible(true);
  24.  
  25.         String url = "http://blogs.sun.com/jag/resource/JagHeadshot.jpg";
  26.         final Border bkgrnd = new CentredBackgroundBorder(ImageIO.read(new URL(url)));
  27.         Runnable r = new Runnable() {
  28.             public void run() {
  29.                 sp.setViewportBorder(bkgrnd);
  30.                 sp.repaint();
  31.             }
  32.         };
  33.         SwingUtilities.invokeLater(r);
  34.     }
  35. }
  36.  
In the above example (where orange really isn't my best colour) I used the JScrollPane method setViewportBorder. For other containers, you typically use setBorder.
Apr 29 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
4723
by: DexHex | last post by:
Hi, I am stumpt. Is there anyway to set the background image on a webpart title?
2
1236
by: Sam | last post by:
Hi all I'm looking for a way to detect the background color of my internal webpage through a stylesheet. And so far, the only solution I find is to open the styles.css file and parse the...
1
6443
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
12
22162
by: mi0sis | last post by:
Right now I am currently using this: ------------------------------------------------------------ .div0 { background-image:url(http://www.rubnip.com/Pictures/topback.gif);...
19
3962
by: david.karr | last post by:
If in my CSS I set the "background-color" property on the "body" element, it only covers the background of the elements defined in the body, up to the current width and height of the page. However,...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.