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
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.
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
:)