Adding Components to a Transparent JFrame
843810May 4 2005 — edited May 5 2005Well, ive been working at this for the past 2 days in my group. I just finished an intermediate structured programming class and have our project semester now. We needed to create a text messaging application (using a JFrame) with a transparent background. I first started with just making the GUI with a picture background, then adding my components overtop.
When switching to the transparent frame (very thankful for the java.sun.com forums), the only way to get the background image to show up was to put it in a JLabel using ImageIcon. Now when I try to add my components, they dont seem to show up on the frame and I cant figure out why.
This is what we have for now .... may be alittle long and not so clean, but I am a n00b at this :P
import java.awt.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;
public class TransparentFrame extends JFrame implements
ActionListener, MouseListener
{
ImageIcon icon;
JPanel panel;
JTextArea textEnter;
JTextArea receiveText;
JComboBox cboToList;
JLabel lblAvatar;
JInternalFrame frame2;
private static final int[] contactsX = { 51, 89, 89, 51},
contactsY = { 271, 271, 295, 295}, sendX = { 98, 125, 125, 98},
sendY = { 271, 271, 295, 295}, optionsX = { 133, 172, 172, 133},
optionsY = { 271, 271, 295, 295}, onTopX = { 53, 89, 89, 53},
onTopY = { 311, 311, 324, 324}, speechX = { 92, 133, 133, 92},
speechY = { 311, 311, 324, 324}, helpX = { 139, 171, 171, 139},
helpY = { 311, 311, 324, 324}, abbrevX = { 53, 87, 87, 53},
abbrevY = { 331, 331, 355, 352}, plansX = { 92, 133, 133, 92},
plansY = { 331, 331, 355, 355}, shrinkX = { 139, 171, 171, 139},
shrinkY = { 331, 331, 352, 355}, historyX = { 53, 89, 89, 57},
historyY = { 357, 360, 379, 379}, forwardX = { 135, 171, 170, 136},
forwardY = { 359, 356, 379, 379};
Polygon plyContacts, plySend, plyOptions, plyOnTop, plySpeech, plyHelp,
plyAbbrev, plyPlans, plyShrink, plyHistory, plyForward;
Polygon[] polygons;
JFrame frame;
JLabel lblTo, lbl;
String[] contacts;
Robot robot;
BufferedImage screenImg;
Rectangle screenRect;
MyPanel contentPanel=new MyPanel();
boolean userActivate=false;
public TransparentFrame()
{
super();
createScreenImage();
this.setContentPane(contentPanel);
this.addComponentListener(new ComponentAdapter() {
public void componentHidden(ComponentEvent e) {}
public void componentMoved(ComponentEvent e) {
resetUnderImg();
repaint();
}
public void componentResized(ComponentEvent e) {
resetUnderImg();
repaint();
}
public void componentShown(ComponentEvent e) {}
});
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
if (userActivate) {
userActivate=false;
// boolean f=TransparentFrame.this.isShowing();
// if (f)
TransparentFrame.this.setVisible(false);
createScreenImage();
resetUnderImg();
// if (f)
TransparentFrame.this.setVisible(true);
}
else {
userActivate=true;
}
}
public void windowDeactivated(WindowEvent e) {
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(248, 477);
instantiateComponents();
layoutComponents();
setupListeners();
show();
}
protected void createScreenImage() {
try {
if (robot==null)
robot=new Robot();
}
catch (AWTException ex) {
ex.printStackTrace();
}
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
screenRect=new Rectangle(0,0,screenSize.width,screenSize.height);
screenImg=robot.createScreenCapture(screenRect);
}
/* public void show() {
resetUnderImg();
super.show();
}*/
public void resetUnderImg() {
if (robot!=null && screenImg!=null) {
Rectangle frameRect=getBounds();
int x=frameRect.x+4;
contentPanel.paintX=0;
contentPanel.paintY=0;
if (x<0) {
contentPanel.paintX=-x;
x=0;
}
int y=frameRect.y+23;
if (y<0) {
contentPanel.paintY=-y;
y=0;
}
int w=frameRect.width-10;
if (x+w>screenImg.getWidth())
w=screenImg.getWidth()-x;
int h=frameRect.height-23-5;
if (y+h>screenImg.getHeight())
h=screenImg.getHeight()-y;
contentPanel.underFrameImg=screenImg.getSubimage(x,y,w,h);
}
}
public void actionPerformed( ActionEvent e )
{
}
public void mouseClicked( MouseEvent e )
{
Object src = e.getPoint();
System.out.println( src );
for (int i = 0; i < polygons.length; i++)
{
if ( polygons.contains((Point)src))
{
doI(i);
}
}
}
private void doI(int i)
{
switch (i)
{
case 0:
JOptionPane.showMessageDialog(this,"Contacts Window");
break;
case 1:
JOptionPane.showMessageDialog(this,"Send Message");
break;
case 2:
JOptionPane.showMessageDialog(this,"Options Window");
break;
case 3:
JOptionPane.showMessageDialog(this,"OnTop");
break;
case 4:
JOptionPane.showMessageDialog(this,"Speech");
break;
case 5:
JOptionPane.showMessageDialog(this,"Help Window");
break;
case 6:
JOptionPane.showMessageDialog(this,"Abbrev");
break;
case 7:
JOptionPane.showMessageDialog(this,"Plans");
break;
case 8:
JOptionPane.showMessageDialog(this,"Shrink");
break;
case 9:
JOptionPane.showMessageDialog(this,"History");
break;
case 10:
JOptionPane.showMessageDialog(this,"Forward Message");
break;
}
}
public void mouseEntered( MouseEvent e )
{
}
public void mouseExited( MouseEvent e )
{
}
public void mousePressed( MouseEvent e )
{
}
public void mouseReleased( MouseEvent e )
{
}
private void instantiateComponents()
{
icon = new ImageIcon("G:\\Projects\\SYSTEM\\Program\\Ryan\\cell\\nokia3.png");
panel = new JPanel(null)
{
protected void paintComponent(Graphics g)
{
g.drawImage(icon.getImage(), 0, 0, null);
super.paintComponent(g);
}
};
lbl = new JLabel(icon);
lbl.setSize(100, 100);
plyContacts = new Polygon(contactsX, contactsY, 4);
plySend = new Polygon(sendX, sendY, 4);
plyOptions = new Polygon(optionsX, optionsY, 4);
plyOnTop= new Polygon(onTopX, onTopY, 4);
plySpeech = new Polygon(speechX, speechY,4);
plyHelp = new Polygon(helpX, helpY, 4);
plyAbbrev = new Polygon(abbrevX, abbrevY, 4);
plyPlans = new Polygon(plansX, plansY, 4);
plyShrink = new Polygon(shrinkX, shrinkY, 4);
plyHistory = new Polygon(historyX, historyY, 4);
plyForward = new Polygon(forwardX, forwardY, 4);
lblTo = new JLabel("To");
textEnter = new JTextArea();
receiveText = new JTextArea();
lblAvatar = new JLabel("Avatar goes \n here!");
contacts = new String[] { "Ryan Knoke", "Jason Froese", "Bryce Sasko",
"Neal Britton" };
cboToList = new JComboBox(contacts);
}
private void layoutComponents()
{
//setLocationRelativeTo( null );
//panel.setOpaque( false );
polygons = new Polygon[] { plyContacts, plySend, plyOptions,
plyOnTop, plySpeech, plyHelp, plyAbbrev, plyPlans, plyShrink,
plyHistory, plyForward } ;
receiveText.setSize(new Dimension(117, 88));
panel.add(receiveText);
receiveText.setLocation(44, 96);
receiveText.setEditable(false);
textEnter.setLineWrap(true);
lblTo.setForeground(Color.WHITE);
lblTo.setSize(15, 18);
lblTo.setLocation(44, 236);
panel.add(lblTo);
JScrollPane scroller = new JScrollPane(textEnter);
scroller.setSize(117, 42);
scroller.setLocation(44, 190);
cboToList.setSize(new Dimension(102, 18));
cboToList.setLocation(58, 236);
panel.add(cboToList);
cboToList.setEditable(false);
lblAvatar.setSize(new Dimension(117, 15));
panel.add(cboToList);
lblAvatar.setLocation(43, 245);
panel.add(scroller);
getContentPane().add(lbl);
getContentPane().add(panel);
}
private void setupListeners()
{
panel.addMouseListener(this);
}
public static void main(String [] args)
{
TransparentFrame frame = new TransparentFrame();
frame.setVisible(true);
}
}
class MyPanel extends JPanel {
BufferedImage underFrameImg;
int paintX=0;
int paintY=0;
public MyPanel() {
super();
setOpaque(true);
}
public void paint(Graphics g) {
super.paint(g);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(underFrameImg,paintX,paintY,null);
}
}
Any help on figuring out how to get the component back onto the frame would be greatly appreciated