A
Alex5222
I am in the early stages of designing an applet and am having some
trouble with this code. Does anyone know what is wrong with this code?
Code:
// Height: 400, Width 650
import java.applet.*;
import java.awt.*;
import java.net.*;
public class main extends Applet {
Image bgImage = null;
Button exitButton;
// public void paint(Graphics g)
// {
// // Display "Hello World!"
// g.drawString("Hello world!", 450, 325);
// }
public void init() {
setLayout(null);
exitButton = new Button("Exit");
exitButton.setBounds(425,325,100,30);
add(exitButton);
try {
MediaTracker tracker = new MediaTracker (this);
bgImage = getImage
(new URL(getCodeBase(), "bgimage.jpg"));
tracker.addImage (bgImage, 0);
tracker.waitForAll();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void update( Graphics g) {
paint(g);
}
public void paint(Graphics g) {
if(bgImage != null) {
int x = 0, y = 0;
while(y < size().height) {
x = 0;
while(x< size().width) {
g.drawImage(bgImage, x, y, this);
x=x+bgImage.getWidth(null);
}
y=y+bgImage.getHeight(null);
}
} else {
g.clearRect(0, 0, size().width, size().height);
}
}
}
trouble with this code. Does anyone know what is wrong with this code?
Code:
// Height: 400, Width 650
import java.applet.*;
import java.awt.*;
import java.net.*;
public class main extends Applet {
Image bgImage = null;
Button exitButton;
// public void paint(Graphics g)
// {
// // Display "Hello World!"
// g.drawString("Hello world!", 450, 325);
// }
public void init() {
setLayout(null);
exitButton = new Button("Exit");
exitButton.setBounds(425,325,100,30);
add(exitButton);
try {
MediaTracker tracker = new MediaTracker (this);
bgImage = getImage
(new URL(getCodeBase(), "bgimage.jpg"));
tracker.addImage (bgImage, 0);
tracker.waitForAll();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void update( Graphics g) {
paint(g);
}
public void paint(Graphics g) {
if(bgImage != null) {
int x = 0, y = 0;
while(y < size().height) {
x = 0;
while(x< size().width) {
g.drawImage(bgImage, x, y, this);
x=x+bgImage.getWidth(null);
}
y=y+bgImage.getHeight(null);
}
} else {
g.clearRect(0, 0, size().width, size().height);
}
}
}