Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2007, 12:58 AM
Member
 
Join Date: Jul 2007
Posts: 40
toby is on a distinguished road
Background image in java
Hi,I have a question about displaying text over a background image in java, can anybody tell me how to do this?

thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-29-2007, 08:15 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Text on background image
Code:
import java.awt.*; import java.awt.font.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; public class TextOnBackground extends JPanel { BufferedImage image; String text = "Hello World"; public TextOnBackground(BufferedImage image) { this.image = image; } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); int w = getWidth(); int h = getHeight(); // Draw image, centered. int x = (w - image.getWidth())/2; int y = (h - image.getHeight())/2; g2.drawImage(image, x, y, this); // Draw text centered over image. Font font = g2.getFont().deriveFont(36f); g2.setFont(font); FontRenderContext frc = g2.getFontRenderContext(); float width = (float)font.getStringBounds(text, frc).getWidth(); LineMetrics lm = font.getLineMetrics(text, frc); float sx = (w - width)/2; float sy = (h + lm.getHeight())/2 - lm.getDescent(); g2.setPaint(Color.red); g2.drawString(text, sx, sy); } public static void main(String[] args) throws IOException { String path = "images/cougar.jpg"; BufferedImage image = ImageIO.read(new File(path)); TextOnBackground test = new TextOnBackground(image); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(test); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Center background image Floetic AWT / Swing 1 04-24-2008 06:50 PM
GUI... setting my background to an image, im using a JFrame newtojava7 New To Java 2 03-24-2008 06:29 AM
Converting multiple banded image into single banded image... Image enhancement archanajathan Advanced Java 0 01-08-2008 06:29 PM
Set the background in Java barney New To Java 1 08-07-2007 08:13 AM
Why this image background is black ? samson Java 2D 1 07-17-2007 05:24 AM


All times are GMT +3. The time now is 02:57 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org