View Single Post
Old 22-03-2007, 07:47 PM   #2
Geezer
Abandonia Homie
 
Geezer's Avatar

 
Join Date: Feb 2007
Location: Brooklyn, United States
Posts: 667
Default

Here is a utility class that i put together to paint background images on JPanels with the option of painting a slogan/title in each panel.



Code:
 

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

 

/** Background is a utility that creates a panel with a background image and 
 * a slogan. With the first contructor, the size of the panel, the location of 
 * the image and the color of the slogan are passed as parameters. Another constructor
 * creates a solid color background (no image) with the size of the panel, the 
 * background color and slogan color as parameters.**/ 

 

public class Background extends JPanel
{
 String banner;
 String slogan;
 String imageLocation;
 ImageIcon logoIcon;
 Image logoImage;
 URL imageURL;
 Font logoFont;
 int offsetX;
 int offsetY;
 
 public Background(int backgroundWidth, int backgroundHeight, String imageLocation,
 Color fontColor, String banner, String slogan, Font logoFont, int offsetX, int offsetY)
 {
**this.imageLocation = imageLocation;
**this.banner = banner;
**this.slogan = slogan;
**this.logoFont = logoFont;
**this.offsetX = offsetX;
**this.offsetY = offsetY;**
**
**Dimension backgroundDimension = new Dimension(backgroundWidth, backgroundHeight);
**setPreferredSize(backgroundDimension);
**setForeground(fontColor);
**setFont(logoFont);**
**setOpaque(true);
 }
 
 public Background(int backgroundWidth, int backgroundHeight, Color fontColor,
 Color backColor, String banner, String slogan, Font logoFont, int offsetX, int offsetY)
 {
**this.imageLocation = null;
**this.banner = banner;
**this.slogan = slogan;
**this.logoFont = logoFont;
**this.offsetX = offsetX;
**this.offsetY = offsetY;**
****
**Dimension backgroundDimension = new Dimension(backgroundWidth, backgroundHeight);
**setPreferredSize(backgroundDimension);
**setForeground(fontColor);
**setBackground(backColor);
**setFont(logoFont);**
**setOpaque(true);
 }
 
 protected void paintComponent(Graphics g)
 {
**super.paintComponent(g);
**FontMetrics bfm1 = g.getFontMetrics();**
**** 
**if (imageLocation != null)
**{
** Class metaObject = this.getClass();
** imageURL = metaObject.getResource(imageLocation);
** logoIcon = new ImageIcon(imageURL);
** logoImage = logoIcon.getImage();
** if (logoImage != null)
****g.drawImage(logoImage, 0, 0, getWidth(), getHeight(), this);
**}

**if (banner != "")
**{
** int bannerWidth = bfm1.stringWidth(banner);
** int xc1 = getWidth() / 2 - bannerWidth / 2;
** g.drawString(banner, xc1, offsetY);
**}
****
**if (slogan != "")
**{
** int sloganWidth = bfm1.stringWidth(slogan);
** int xc2 = getWidth() / 2 - sloganWidth / 2;
** g.drawString(slogan, xc2, (offsetY + 25));**
**}
**
//**System.out.println("Graphics painted.");
**
 } 
}
__________________
"The gadfly has returned! Thou simpering, whining stable dropping. I have defeated thee repeatedly and still thou darest crawl back for more punishment. Chastise thee I shall. Thy stature is insignificant and thy name moronic. Brother to the vole! Offspring of money grubbing know nothings!" ... Spellcraft: Aspects of Valor
Geezer is offline                         Send a private message to Geezer
Reply With Quote