Results 1 to 14 of 14
- 09-28-2010, 10:16 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 7
- Rep Power
- 0
Draw text over background image using JLabel and JFrame
Hello, could anyone help me to finish this simple program? I need to make the program draw the lable over the background but i cant achieve this. I'm totaly new to java...thx in advance.
Here's my code so far:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingConstants;
import javax.swing.ImageIcon;
/**
*
* @author Aidas
*/
public class Langas extends JFrame {
JScrollPane scrollPane;
ImageIcon icon;
JLabel label;
public Langas(){
icon = new ImageIcon("logo.jpg");
JPanel panel= new JPanel()
{
public void paintComponent(Graphics g)
{
g.drawImage(icon.getImage(), 0, 0, null);
super.setOpaque(false);
super.paintComponent(g);
}
};
panel.setOpaque(false);
scrollPane = new JScrollPane( panel );
getContentPane().add( scrollPane );
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JLabel label = new JLabel("Microsoft Office 2003 SP3");
Langas frame = new Langas();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.getContentPane().add(label);
frame.setSize(640, 480);
frame.setLocationRelativeTo( null );
frame.setBackground(Color.red);
frame.setVisible(true);
// TODO code application logic here
}
- 09-28-2010, 10:27 PM #2
please explain your problem in more detail.i cant achieve this.
- 09-29-2010, 06:01 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 7
- Rep Power
- 0
I want to draw text over the background image. For now i only made the program to draw background, but if i add label the background dissapears... it doesnt draw on it.
For now its like in picture number 2, or like in 1 if i add label. I want it to be like in picture 3
- 09-29-2010, 01:52 PM #4
How are you drawing the image and then the text over it?
Do you do both in the same paintComponent method or how?
Please post your code showing how the two are being drawn.
- 09-29-2010, 07:34 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 7
- Rep Power
- 0
Well that's what i'm trying to achieve... if i would know i wouldnt ask. I havent managed to draw them both. So that's what i'm asking :)
- 09-29-2010, 08:12 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,383
- Blog Entries
- 7
- Rep Power
- 17
- 09-29-2010, 08:35 PM #7
Member
- Join Date
- Sep 2010
- Posts
- 7
- Rep Power
- 0
That's my homework, i have to use JLabel and JFrame
- 09-29-2010, 09:55 PM #8
Please post your code showing how the two are being drawn.
How complicated can your code be? Do you know how to create a BufferedImage?
If so, create one, draw the image on it and then draw the string over the top and then pass that image to the label to be shown.
- 09-29-2010, 10:05 PM #9
Member
- Join Date
- Sep 2010
- Posts
- 7
- Rep Power
- 0
- 09-29-2010, 10:27 PM #10
Please post your code showing how the two are being drawn.
- 09-29-2010, 10:32 PM #11
Member
- Join Date
- Sep 2010
- Posts
- 7
- Rep Power
- 0
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingConstants;
import javax.swing.ImageIcon;
/**
*
* @author Aidas
*/
public class Langas extends JFrame {
JScrollPane scrollPane;
ImageIcon icon;
JLabel label;
public Langas(){
icon = new ImageIcon("logo.jpg");
JPanel panel= new JPanel()
{
public void paintComponent(Graphics g)
{
g.drawImage(icon.getImage(), 0, 0, null);
super.setOpaque(false);
super.paintComponent(g);
}
};
panel.setOpaque(false);
scrollPane = new JScrollPane( panel );
getContentPane().add( scrollPane );
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JLabel label = new JLabel("Microsoft Office 2003 SP3");
Langas frame = new Langas();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.getContentPane().add(label);
frame.setSize(640, 480);
frame.setLocationRelativeTo( null );
frame.setBackground(Color.red);
frame.setVisible(true);
// TODO code application logic here
}
This is my code so far...Last edited by vytska; 09-29-2010 at 10:35 PM.
- 09-29-2010, 10:47 PM #12
I guess I'm confused about what you are trying to do.
Where is the drawString method being called? That's what you want, to draw a String of text over an image?
Looking at the comments in your code I have no idea what you want it to do.
- 09-29-2010, 10:51 PM #13
Member
- Join Date
- Sep 2010
- Posts
- 7
- Rep Power
- 0
- 09-29-2010, 10:55 PM #14
Similar Threads
-
JLabel wont appear in front of background image
By Andolph23 in forum AWT / SwingReplies: 5Last Post: 05-27-2010, 01:42 AM -
JFrame image background
By 851marc in forum NetBeansReplies: 5Last Post: 03-09-2010, 06:27 PM -
How to add a background image to JFrame
By dunafrothint in forum AWT / SwingReplies: 1Last Post: 02-26-2010, 10:17 PM -
Need help with JFrame background image
By ProGenius in forum New To JavaReplies: 6Last Post: 12-27-2009, 04:17 PM -
GUI... setting my background to an image, im using a JFrame
By newtojava7 in forum New To JavaReplies: 2Last Post: 03-24-2008, 05:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks