• Post Reply Bookmark Topic Watch Topic
  • New Topic

try this  RSS feed

lion tete
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.setStroke(s1); // Tell the graphics context about it
g2.draw(r1); // And ask it to draw the rectangle
Nathan Pruett
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!