Applet problems in adding a bgImage

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);
}
}
}
 
A

Andrew Thompson

I am in the early stages of designing an applet and am having some
trouble with this code.

What 'trouble'. Is it staying out too late, and
coming home drunk and punchy?

...maybe you should put it in your words (specifically).
..Does anyone know what is wrong with this code?
Code: ...

public void init() {
setLayout(null);

We start to go wrong here. Stop that!

It is a common mistake promulgated by a great
many bad programming examples.

If this comes from a 'GUI' book. Burn it,
it may be of some (little) use as fuel.
public void paint(Graphics g) {
if(bgImage != null) {

And why are you overriding paint()?

Overriding paint is one of those times when all
your laying out, adding buttons and setting bounds
becomes redundant!

You should either ..

Be doing a general programming tutorial and posting to
c.l.j.help. I base that on lines like this..
public class main extends Applet {

...or doing a GUI tutorial and posting to c.l.j.gui,
but I do not feel you should be attemtping UI's at
all as yet, let alone Applets.
<http://www.physci.org/codes/javafaq.jsp#appfirst>

Please read the *entire* PhySci Java FAQ, there are
many references to Applets, group descriptions, a link
to an (older) version of the GUI FAQ, and much more
that might be of use to you..

HTH
 
J

jan V

Does anyone know what is wrong with this code?
public class main extends Applet {

Your class names should start with a capital. Follow the universal
convention.
Image bgImage = null;
Button exitButton;

Your fields should be properly scoped. You probably meant to declare these
fields private, but fell in the common laziness trap of not specifying a
keyword, thus ending up with package scope - something that does not make
sense in your applet's case.
setLayout(null);

Naughty. Bypassing the AWT/Swing layouts framework is not the way to go..
exitButton.setBounds(425,325,100,30);

Naughty. Absolute positioning of GUI components is a big no-no in Java
circles.
catch (Exception e) {

Naughty, naughty. Never catch Exception.. only catch the precise
exception(s) that can be thrown, leaving RuntimeException subclasses like
NullPointerException well alone.
 
A

Alex5222

I got most of this code from the tutorial found here:
http://www.rgagnon.com/javadetails/java-0233.html

If you think I should start by writing an app first, then a gui, then
an applet, then is there anyway to make it so people can run my gui
directly from the internet like you can with an applet without them
having to download the Java Runtime Environment?
 
A

Andrew Thompson

On 9 Aug 2005 09:28:01 -0700, (e-mail address removed) wrote:

Please 'reply' to the message you are referring to, and
paste a little of the earlier conversation, so people don't get lost.
I got most of this code from the tutorial found here:
http://www.rgagnon.com/javadetails/java-0233.html

Real's How-To is very handy.
If you think I should start by writing an app first, then a gui, then
an applet, then is there anyway to make it so people can run my gui
directly from the internet like you can with an applet without them
having to download the Java Runtime Environment?

You are mistaken. An applet requires the JRE.

Point your browser here, to see details on yours..
<http://www.physci.org/pc/property.jsp?prop=java.version+java.vendor>

If an applet works, the end user can (most likely*)
already run Java on the desktop.

* If the report from the applet is Java >1.2,
then Java Webstart (installer) is available).
 
A

Alex5222

ok....
"You are mistaken. An applet requires the JRE. "
An applet doesn't require the JRE, it requires Microsoft Virtual
Machine or something else that does the same thing, but those plugins
aren't able to run java apps, are they?
 
A

Andrew Thompson

ok....
"You are mistaken. An applet requires the JRE. "
An applet doesn't require the JRE, it requires Microsoft Virtual
Machine or something else that does the same thing,

A 'runtime environment' for Java covers MSVM and Sun VM
and many other (Java) Runtime Environments.
..but those plugins aren't able to run java apps, are they?

Yes, they sure *are* capable of running desktop
Java applications. (Though the MSVM will not
run modern Java applications, nor does not
understand WebStart).

But coming back to something you put earlier.
"directly from the internet like you can with an
applet without them having to download the Java
Runtime Environment?"

Browsers do *not* always come so that they run Java
as installed. In some browsers, Java is treated as
a 'Plug-In' so that the user can ..
- download the browser with the Java plug-in.
- download the borwser *without* the Java
plug-in and, either..
- install Java later, or
- *not* install Java at all.

Your applet will *not* run in all browsers, only
browsers with a Java runtime environnemt (plug-in)
installed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top