This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Applets and the fly likes try this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "try this" Watch "try this" New topic
Author

try this

lion tete
Greenhorn

Joined: Jul 10, 2001
Posts: 1
why the following code doesn't work?
it compiles but doesn't show anything on the applet.
import java.awt.*;
import java.applet.*;
import java.awt.geom.*; // Required for Rectangle2D
public class sample extends Applet
{
Graphics2D g2; // Reference to Graphics2D context
public sample() {} // Construct the applet
public void init() {} // Initialise the applet
// Applet screen paint method : use this to draw the rectangles.
public void paint(Graphics g)
{
// Cast the applet graphics contect to Graphics2D to allow us to
// draw Rectangle2D objects on it.
g2 = (Graphics2D) g;
// Small rectangle to show that the width and height are larger
// by one pixel than the specified parameters :
// x = 5, y = 5, width = 3, height = 2
Rectangle2D r0 = new Rectangle2D.Double(5,5,50,50);
// Three squares, each the same size, but one above each other.
// These are drawn with 1, 2, and 3 pixel wide borders to show how
// the border relates to the rectangle coordinates and position.
Rectangle2D r1 = new Rectangle2D.Double(15,10,50,50);
Rectangle2D r2 = new Rectangle2D.Double(25,20,50,50);
Rectangle2D r3 = new Rectangle2D.Double(35,30,50,50);
// Use a BasicStroke object to define various line widths, and
// draw these rectangles using these definitions.
BasicStroke s1 = new BasicStroke(2);
g2.setColor(Color.red);
g2.setStroke(s1); // Tell the graphics context about it
g2.draw(r0);
g2.draw(r1); // And ask it to draw the rectangle
g2.draw(r2);
g2.draw(r3);
}
}
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You should be able to see the applet in appletviewer. The reason you are not seeing this applet in your browser is because it uses the Graphics2D class, which is part of Java 2. Most browsers only use Java 1.1. If you want to use Java 2 classes you will need to install the Java-Plugin to your browser, and use HTMLconverter on the web page. ( Details of this process can be found in several other posts here on JavaRanch, just do a search on 'plug in' or 'HTMLConverter'. ) You can make a similiar program that works without the plugin by using the drawRect() method of the regular Graphics class. Unfortunately, the different stroke styles are not available in the old Graphics class, so the edges of the rectangles will be thinner...



HTH,
-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: try this
 
Similar Threads
Applet to draw Ellipse
adding a button
Sorry, one more thing with AWT
Swing related
event model for Rectangle2D object