- Joined
- Jun 29, 2022
- Messages
- 20
- Reaction score
- 0
In eclipse, I've been working on a java web applet (or what I believed was a java web applet). I attempted to incorporate it in an HTML doc after successful tests on my machine. The next thing I know, I'm inundated with mistakes. Based on my investigation, these problems indicate that I'm utilizing code that isn't compatible with a web applet. Unfortunately, I have no idea what code is or isn't compatible with the applet, and hence have no idea how to resolve this issue. To be honest, I'm not sure where to begin. My code is somewhat complicated:
I'm curious if anything stands out as incorrect or incompatible right immediately. I would be grateful if someone could assist me with this code directly or tell me how I can start making this code appropriate for a web applet. I've previously read the java applet documentation and the distinction between applet and application from here. Still, it's rather perplexing, especially given how much I've typed so far since I'm having trouble plucking out certain items in my code.
HTML:
import java.applet.Applet;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
public class DerpAHerp extends Applet implements KeyListener, MouseListener, Runnable {
int speed = 3;
int andelay = 2;
Thread animator;
Point velocity = new Point(0,0);
Point pos = new Point(50,50);
Point ppos = new Point(0,0);
int imgCount = 1;
Image[] person_S = makeAnimation(6,"person_S-","png");
Image[] person_E = makeAnimation(6,"person_E-","png");
Image[] person_W = makeAnimation(6,"person_W-","png");
Image[] person_N = makeAnimation(6,"person_N-","png");
Image personDefault = person_S[0];
Image person = personDefault;
Image offImage;
Graphics offGraphics;
Boolean moving = false;
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
public void getConnection(){
String asdf = "";
try {
URL ourURL = new URL("http://dannyflax.antserve.com/d/"); //Coding Forums RSS Feed
HttpURLConnection huc = (HttpURLConnection)ourURL.openConnection();
huc.setRequestMethod("GET");
huc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; JVM)");
huc.setRequestProperty("Pragma", "no-cache");
huc.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(huc.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
// Either do your parsing here, or append it to a StringBuffer for later use
asdf = asdf.concat(line);
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
catch(Exception e)
{
System.err.println("General Exception " + e);
e.printStackTrace();
}
}
public void paintFrame(Graphics g) {
g.setColor(getBackground());
g.fillRect(0, 0, person.getWidth(null),person.getHeight(null));
Graphics2D g2 = (Graphics2D) g;
if(velocity.x!=0 || velocity.y!=0){
if(velocity.x>0){
//East
if(imgCount%andelay == 0){
person = person_E[(imgCount/andelay)%person_S.length];
}
}
else if(velocity.x<0){
//West
if(imgCount%andelay == 0){
person = person_W[(imgCount/andelay)%person_S.length];
}
}
else if(velocity.y>0){
//South
if(imgCount%andelay == 0){
person = person_S[(imgCount/andelay)%person_S.length];
}
}
else if(velocity.y<0){
//North
if(imgCount%andelay == 0){
person = person_N[(imgCount/andelay)%person_S.length];
}
}
imgCount++;
}
else{
person = personDefault;
imgCount = 0;
}
g2.drawImage(person, 0, 0, this);
g2.finalize();
}
public void update(Graphics g) {
if (offGraphics == null){
if(person.getWidth(null) > 0 && person.getHeight(null) > 0)
offImage = createImage(person.getWidth(null),person.getHeight(null));
else{
offImage = createImage(1,1);
}
offGraphics = offImage.getGraphics();
paintFrame(offGraphics);
}
Color bg = getBackground();
paintFrame(offGraphics);
g.setColor(bg);
g.fillRect(ppos.x-(person.getWidth(null)/2), ppos.y-(person.getHeight(null)/2), person.getWidth(null),person.getHeight(null));
g.drawImage(person, pos.x-(person.getWidth(null)/2), pos.y-(person.getHeight(null)/2), this);
g.finalize();
}
public void paint(Graphics g) {
update(g);
}
public void init() {
//Execute a job on the event-dispatching thread; creating this applet's GUI.
addKeyListener( this );
addMouseListener( this );
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
int code = arg0.getKeyCode();
switch(code){
case 37:
//West
velocity.y = 0;
velocity.x = -1 * speed;
personDefault = person_W[0];
break;
case 38:
//North
velocity.x = 0;
velocity.y = -1 * speed;
personDefault = Toolkit.getDefaultToolkit().getImage("person_N-0.png");
break;
case 39:
//East
velocity.y = 0;
velocity.x = speed;
moving = true;
personDefault = person_E[0];
break;
case 40:
//South
velocity.x = 0;
velocity.y = speed;
personDefault = person_S[0];
break;
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
int code = arg0.getKeyCode();
switch(code){
case 37:
//Left
velocity.x = 0;
break;
case 38:
//Up
velocity.y = 0;
break;
case 39:
//Right
velocity.x = 0;
break;
case 40:
//Down
velocity.y = 0;
break;
}
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public void start() {
animator = new Thread(this);
animator.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while (Thread.currentThread() == animator) {
// Display the next frame of animation.
ppos.x = pos.x;
ppos.y = pos.y;
pos.x = pos.x + velocity.x;
pos.y = pos.y + velocity.y;
repaint();
// Delay for a while
try {
Thread.sleep(50);
} catch (InterruptedException e) {
System.out.println("Failed");
break;
}
}
}
public Image[] makeAnimation(int size, String name, String extension){
Image[] imgAr = new Image[size];
while(size>0){
String src = name + size + "." + extension;
imgAr[size-1] = Toolkit.getDefaultToolkit().getImage(src);
size--;
}
return imgAr;
}
public void stop() {
animator = null;
}
}