need a transparent background Text Field

Hello people,Is it possible to make the background of a Text Field transparent (and preferably semitransparent)?I've tried using new Color(...), but it always ignores the ALPHA value. Am I missing something?Thanks in advance.
[294 byte] By [robinjama] at [2007-10-3 9:37:35]
# 1
hi!in JTextField try to use setOpaque(false)hope this can solve your problem.:)
Aniruddha-Herea at 2007-7-15 4:53:15 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks for your reply. I am aware that this works in a JTextField, however due to problems when converting my applet to a JApplet I am forced to use a regular TextField, which doesn't support setOpaque. Any other ideas?
robinjama at 2007-7-15 4:53:15 > top of Java-index,Desktop,Core GUI APIs...
# 3
Do not mix Swing and AWT! That will solve some of your probelms.
zadoka at 2007-7-15 4:53:15 > top of Java-index,Desktop,Core GUI APIs...
# 4
hello,do not mind, when you are using JApplet then you have got the swing library. why can't you use the JTextField? is it project constraints? else you better follow the previous suggestion, do not mix heavy-weight & light-weight components.:)
Aniruddha-Herea at 2007-7-15 4:53:16 > top of Java-index,Desktop,Core GUI APIs...
# 5
I have tried upgrading my applet to a JApplet, but whenever I do the compiler throws about 20 errors when I compile.
robinjama at 2007-7-15 4:53:16 > top of Java-index,Desktop,Core GUI APIs...
# 6
What are the errors?
CaptainMorgan08a at 2007-7-15 4:53:16 > top of Java-index,Desktop,Core GUI APIs...
# 7
Also, theres a constructor in the Color class to make translucent colors.//make a translucent red colorColor translucentRed = new Color(255, 0, 0, 128);
CaptainMorgan08a at 2007-7-15 4:53:16 > top of Java-index,Desktop,Core GUI APIs...
# 8

I tried making a translucent colour using new Color(...)

but the alpha value was ignored by Java.

There are 22 errors in total, including:

method repaint() doesnt exist

getImage() doesnt exist in Image

drawImage() doesnt exist Image

etc

robinjama at 2007-7-15 4:53:16 > top of Java-index,Desktop,Core GUI APIs...
# 9
repaint(), drawImage(), etc. are methods that belong to certain classes. You cant just randomly call them. Try posting some of your code.
CaptainMorgan08a at 2007-7-15 4:53:16 > top of Java-index,Desktop,Core GUI APIs...
# 10

here's the code: (works fine as an Applet but not as a JApplet)

import java.io.*;

import java.awt.*;

import java.applet.*;

import java.net.*;

public class LegerDomain extends Applet implements Runnable {

// VARIABLE DECLARATIONS ************************************************************************************************************************

int frame;// The current frame number

Image JimmywareLogo;// The JW logo (800x600)

Image Loading;// The small loading screen

Image backbuffer;// The back buffer in the flipping chain (800x600)

Image login; // The login screen (800x600)

Graphics backbufferg;// The graphics object used to draw on the back buffer

URL base;

MediaTracker mt; // The mediatracker used to check that images are loaded

boolean running=true;// Indicates whether or not the current frame should be rendered

Thread graphicsThread;// The seperate thread that the grpahic processing is executed on

String IP;// The server's IP Address, passed from the webpage

String MOTD; // The server's message of the day, passed from the webpage

TextField UserNameField; // The space where the user enters their username

// ************************ The following variables are involved in the connection to the network.****

Socket ldSocket = null; // The socket used to connect

PrintWriter out = null; // The output stream to be sent to the server

BufferedReader in = null;// The input stream from the server

// END OF VARIABLE DECLARATIONS *****************************************************************************************************************

public void init() { // Initializes the applet ready for use

IP=getParameter("IP");// Get the IP address of the server

MOTD=getParameter("MOTD"); // Get the server's message of the day

mt = new MediaTracker(this);// Prepare the mediatracker to receive images

try {

base = getDocumentBase();// Get the base of the applet for use while loading images

} catch (Exception e) {}

JimmywareLogo = getImage(base,"jimmywarelogo.jpg"); // Load the JW logo

Loading = getImage(base,"loading.jpg"); // Load the loading screen

mt.addImage(JimmywareLogo,1);// Add the JW logo to the loading queue

mt.addImage(Loading,1);// Add the loading screen to the loading queue

try {

mt.waitForAll();// Wait until all the images have loaded before proceeding

} catch (InterruptedException e) {}

backbuffer = createImage(800, 600);

backbufferg = backbuffer.getGraphics(); // Prepare the back buffer for drawing onto

graphicsThread= new Thread(this);

graphicsThread.start();// Create the grpahics thread and start it running

} // END INIT ***********************************************************************************************************************************

public void destroy() { // Kills anything that may cause laag after the applet closes

running = false;// will cause thread to stop looping

graphicsThread = null; // destroy it.

} // END DESTROY ********************************************************************************************************************************

public void update( Graphics g ) { // Replaces the old update function to prevent flicker

g.drawImage( backbuffer, 0, 0, this ); // Draw the back buffer to the screen

} // END UPDATE *********************************************************************************************************************************

public void paint( Graphics g ) { // Called whenever the applet is invalidated or the graphics thread ticks

update( g ); // Just pass the burden onto the update function

} // END PAINT **********************************************************************************************************************************

public void run() { // Called whenever the graphics thread ticks

while (running) { // Make sure the applet isn't exiting

FontMetrics fm= backbuffer.getGraphics().getFontMetrics(backbuffer.getGraphics().getFont());

java.awt.geom.Rectangle2D rect = fm.getStringBounds(MOTD, backbuffer.getGraphics());

int MOTDHeight = (int)(rect.getHeight());

int MOTDWidth = (int)(rect.getWidth()); // Find the position of the MOTD, so It's centered

backbufferg.setColor(Color.black);

backbufferg.fillRect(0,0,800,600); // Clear the screen

if(frame<=25) {

backbufferg.drawImage(JimmywareLogo,0,0,this);

backbufferg.setColor(new Color(0,0,0,255-(frame*10)));

backbufferg.fillRect(0,0,800,600);

} else if(frame<=100) {

backbufferg.drawImage(JimmywareLogo,0,0,this);

} else if(frame<=125) {

backbufferg.drawImage(JimmywareLogo,0,0,this);

backbufferg.setColor(new Color(0,0,0,(frame-100)*10));

backbufferg.fillRect(0,0,800,600);

} else if(frame<=150) {

backbufferg.drawImage(Loading,200,100,this);

backbufferg.setColor(Color.gray);

backbufferg.drawRect(200,375,400,20);

backbufferg.drawString("Loading graphics...",200,365);

backbufferg.setColor( Color.white );

backbufferg.drawString(MOTD, 400-(MOTDWidth/2), 10 );

backbufferg.setColor(new Color(0,0,0,255-((frame-125)*10)));

backbufferg.fillRect(0,0,800,600);

} else if(frame<=151){

backbufferg.drawString("Connecting to server...",200,365);

backbufferg.drawImage(Loading,200,100,this);

backbufferg.setColor(Color.gray);

//backbufferg.drawRect(200,375,400,20);

backbufferg.setColor(Color.gray);

//backbufferg.fillRect(202,377,(int)width,17);

backbufferg.setColor( Color.white );

backbufferg.drawString(MOTD, 400-(MOTDWidth/2), 10 );

login = getImage(base,"login.jpg");

mt.addImage(login,1);

try {

mt.waitForAll();

} catch (InterruptedException e) {}

repaint();

ConnectToServer();

} else {

UserNameField = new TextField("",100);

UserNameField.setBounds(316,286,286,32);

if(frame==152) { add(UserNameField); }

backbufferg.drawImage(login,0,0,this);

} // end if

repaint();

try {

graphicsThread.sleep(30);

} catch (InterruptedException e) {

System.out.println(e);

} // end catch

frame++; // Go to the next frame

} // end while

} // end run

public void ConnectToServer() {

try {

ldSocket = new Socket(IP, 4444);

out = new PrintWriter(ldSocket.getOutputStream(), true);

in = new BufferedReader(new InputStreamReader(ldSocket.getInputStream()));

} catch (UnknownHostException e) {

backbufferg.setColor(Color.white);

backbufferg.drawString("Sorry, the connection to the server " + IP + " on port 4444 failed. Please try again later.",10,10);

repaint();

running=false;

} catch (IOException e) {

backbufferg.setColor(Color.white);

backbufferg.drawString("Sorry, the connection to the server " + IP + " on port 4444 failed. Please try again later.",10,10);

repaint();

running=false;

}

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

String fromServer;

String fromUser;

} // end ConnectToServer

} // end class LegerDomain

P.S. I will break the code up into multiple classes to make it easier to read, as soon as I get it working properly :P

P.P.S. The reason I am forced to use high level components in an applet is because:

1. The user has to log on and I don't want to have to make my own text box control

2. It has to be an applet so I can put it into my webpage

P.P.P.S. Just incase I haven't explained well enough: I'm making an mmorpg and thats why it has to connect to the server.

robinjama at 2007-7-15 4:53:16 > top of Java-index,Desktop,Core GUI APIs...
# 11
What errors are you getting when it extends JApplet?Remember to post the entire error.
CaptainMorgan08a at 2007-7-15 4:53:16 > top of Java-index,Desktop,Core GUI APIs...
# 12

hi!

i have just changed two things. changed Applet to JApplet, and changed TextField to JTextField, i have got no error while it was running. so do you mind to provide more details about what errors are you getting. and how they are reproducable.

my changed code:->

import java.awt.Color;

import java.awt.FontMetrics;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.MediaTracker;

import java.awt.TextField;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.Socket;

import java.net.URL;

import java.net.UnknownHostException;

import javax.swing.JApplet;

import javax.swing.JTextField;

public class LegerDomain extends JApplet implements Runnable {

// VARIABLE DECLARATIONS ************************************************************************************************************************

int frame;// The current frame number

Image JimmywareLogo;// The JW logo (800x600)

Image Loading;// The small loading screen

Image backbuffer;// The back buffer in the flipping chain (800x600)

Image login; // The login screen (800x600)

Graphics backbufferg;// The graphics object used to draw on the back buffer

URL base;

MediaTracker mt; // The mediatracker used to check that images are loaded

boolean running=true;// Indicates whether or not the current frame should be rendered

Thread graphicsThread;// The seperate thread that the grpahic processing is executed on

String IP;// The server's IP Address, passed from the webpage

String MOTD; // The server's message of the day, passed from the webpage

JTextField UserNameField; // The space where the user enters their username

// ************************ The following variables are involved in the connection to the network.****

Socket ldSocket = null; // The socket used to connect

PrintWriter out = null; // The output stream to be sent to the server

BufferedReader in = null;// The input stream from the server

// END OF VARIABLE DECLARATIONS *****************************************************************************************************************

@Override

public void init() { // Initializes the applet ready for use

IP="192.168.0.1";//getParameter("IP");// Get the IP address of the server

MOTD="";//getParameter("MOTD"); // Get the server's message of the day

mt = new MediaTracker(this);// Prepare the mediatracker to receive images

try {

base = getDocumentBase();// Get the base of the applet for use while loading images

} catch (Exception e) {}

JimmywareLogo = getImage(base,"jimmywarelogo.jpg"); // Load the JW logo

Loading = getImage(base,"loading.jpg"); // Load the loading screen

mt.addImage(JimmywareLogo,1);// Add the JW logo to the loading queue

mt.addImage(Loading,1);// Add the loading screen to the loading queue

try {

mt.waitForAll();// Wait until all the images have loaded before proceeding

} catch (InterruptedException e) {}

backbuffer = createImage(800, 600);

backbufferg = backbuffer.getGraphics(); // Prepare the back buffer for drawing onto

graphicsThread= new Thread(this);

graphicsThread.start();// Create the grpahics thread and start it running

} // END INIT ***********************************************************************************************************************************

@Override

public void destroy() { // Kills anything that may cause laag after the applet closes

running = false;// will cause thread to stop looping

graphicsThread = null; // destroy it.

} // END DESTROY ********************************************************************************************************************************

@Override

public void update( Graphics g ) { // Replaces the old update function to prevent flicker

g.drawImage( backbuffer, 0, 0, this ); // Draw the back buffer to the screen

} // END UPDATE *********************************************************************************************************************************

@Override

public void paint( Graphics g ) { // Called whenever the applet is invalidated or the graphics thread ticks

update( g ); // Just pass the burden onto the update function

} // END PAINT **********************************************************************************************************************************

public void run() { // Called whenever the graphics thread ticks

while (running) { // Make sure the applet isn't exiting

FontMetrics fm= backbuffer.getGraphics().getFontMetrics(backbuffer.getGraphics().getFont());

java.awt.geom.Rectangle2D rect = fm.getStringBounds(MOTD, backbuffer.getGraphics());

int MOTDHeight = (int)(rect.getHeight());

int MOTDWidth = (int)(rect.getWidth()); // Find the position of the MOTD, so It's centered

backbufferg.setColor(Color.black);

backbufferg.fillRect(0,0,800,600); // Clear the screen

if(frame<=25) {

backbufferg.drawImage(JimmywareLogo,0,0,this);

backbufferg.setColor(new Color(0,0,0,255-(frame*10)));

backbufferg.fillRect(0,0,800,600);

} else if(frame<=100) {

backbufferg.drawImage(JimmywareLogo,0,0,this);

} else if(frame<=125) {

backbufferg.drawImage(JimmywareLogo,0,0,this);

backbufferg.setColor(new Color(0,0,0,(frame-100)*10));

backbufferg.fillRect(0,0,800,600);

} else if(frame<=150) {

backbufferg.drawImage(Loading,200,100,this);

backbufferg.setColor(Color.gray);

backbufferg.drawRect(200,375,400,20);

backbufferg.drawString("Loading graphics...",200,365);

backbufferg.setColor( Color.white );

backbufferg.drawString(MOTD, 400-(MOTDWidth/2), 10 );

backbufferg.setColor(new Color(0,0,0,255-((frame-125)*10)));

backbufferg.fillRect(0,0,800,600);

} else if(frame<=151){

backbufferg.drawString("Connecting to server...",200,365);

backbufferg.drawImage(Loading,200,100,this);

backbufferg.setColor(Color.gray);

//backbufferg.drawRect(200,375,400,20);

backbufferg.setColor(Color.gray);

//backbufferg.fillRect(202,377,(int)width,17);

backbufferg.setColor( Color.white );

backbufferg.drawString(MOTD, 400-(MOTDWidth/2), 10 );

login = getImage(base,"login.jpg");

mt.addImage(login,1);

try {

mt.waitForAll();

} catch (InterruptedException e) {}

repaint();

ConnectToServer();

} else {

UserNameField = new JTextField("", 100);

UserNameField.setBounds(316,286,286,32);

if(frame==152) { add(UserNameField); }

backbufferg.drawImage(login,0,0,this);

} // end if

repaint();

try {

graphicsThread.sleep(30);

} catch (InterruptedException e) {

System.out.println(e);

} // end catch

frame++; // Go to the next frame

} // end while

} // end run

public void ConnectToServer() {

try {

ldSocket = new Socket(IP, 4444);

out = new PrintWriter(ldSocket.getOutputStream(), true);

in = new BufferedReader(new InputStreamReader(ldSocket.getInputStream()));

} catch (UnknownHostException e) {

backbufferg.setColor(Color.white);

backbufferg.drawString("Sorry, the connection to the server " + IP + " on port 4444 failed. Please try again later.",10,10);

repaint();

running=false;

} catch (IOException e) {

backbufferg.setColor(Color.white);

backbufferg.drawString("Sorry, the connection to the server " + IP + " on port 4444 failed. Please try again later.",10,10);

repaint();

running=false;

}

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

String fromServer;

String fromUser;

} // end ConnectToServer

} // end class LegerDomain

:)

Aniruddha-Herea at 2007-7-15 4:53:16 > top of Java-index,Desktop,Core GUI APIs...
# 13
Thanks for the modified code. It works perfectly.
robinjama at 2007-7-15 4:53:16 > top of Java-index,Desktop,Core GUI APIs...