Resizing output to fit the current size of a window. : Graphics « 2D Graphics « Java Tutorial






import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MainClass extends Applet {
  final int inc = 25;

  int max = 500;

  int min = 200;

  Dimension d;

  public MainClass() {
    addMouseListener(new MouseAdapter() {
      public void mouseReleased(MouseEvent me) {
        int w = (d.width + inc) > max ? min : (d.width + inc);
        int h = (d.height + inc) > max ? min : (d.height + inc);
        setSize(new Dimension(w, h));
      }
    });
  }

  public void paint(Graphics g) {
    d = getSize();

    g.drawLine(0, 0, d.width - 1, d.height - 1);
    g.drawLine(0, d.height - 1, d.width - 1, 0);
    g.drawRect(0, 0, d.width - 1, d.height - 1);
  }
}








16.2.Graphics
16.2.1.Draw rectangles
16.2.2.Fill a solid three-dimensional rectangle
16.2.3.Draw Ellipses
16.2.4.Draw 2D shapeDraw 2D shape
16.2.5.Draw Arcs
16.2.6.Draw PolygonDraw Polygon
16.2.7.Resizing output to fit the current size of a window.
16.2.8.Demonstrate XOR mode.Demonstrate XOR mode.