How to Increase the Size of Graphics in Applets
An applet is a Java code that allows you to insert Java into an HTML page. Using an applet, you can employ the "draw image" function that will display an image in your Web browser. You can also define the positioning and size of this graphic within the applet that you create. You must first create a new HTML page and then include the proper Java applet code.
Instructions
-
-
1
Open a text editor, such as Notepad or Text Edit. Begin by inserting the Java code needed to insert an applet. Type in:
import java.awt.*;
import java.applet.*;
import java.net.*;
This will import the Java code needed to run the applet.
-
2
Define your applet as an image by typing in the code:
public class ImageExample extends Applet
{ Image imagename; }
You may replace "imagename" with whatever you would like to call your image. Next, insert Media Tracker so that you can control the loading of the applet:
MediaTracker mt;
public void paint(Graphics g) {
mt = new MediaTracker(this);
-
-
3
Insert the source of your image code so that your HTML page knows where to pull it from.
imagename = getImage(base,"imageExample.gif");
mt.addImage(imagename,1);
Replace "imageExample.gif" with the name of your image. Make sure that your image is in the same directory as your HTML file. The second line of code will add this image to your media tracker so that it loads into the applet.
-
4
Set the characteristics of your image and close out the code.
tr.addImage(imagename,0, 0, this);
g.drawImage(imagename, 0, 0, 100, 100, this);
}
}
The first two numbers in "drawImage" indicate the position of your graphic. In this example, the graphic is positioned in the top left corner of the applet. The next two numbers indicate the width and height, respectively. If these are not set, then the image will be set at its default size. To increase the size of your graphic, increase the values of these numbers to your desired size.
-
5
Save your file with a .html extension and then upload it and your image to your Web browser. Your HTML file will now display a graphic with an increased size inside of an applet.
-
1
References
- Photo Credit Comstock/Comstock/Getty Images