Originally posted by Randy Helene:
...I don't know what I am doing wrong...]
You were close. Here's the revised version that works. The key is to establish the change ratio between rounds of resize, and then reset your polygon coordinates accordingly:
import java.awt.*;
import java.applet.*;
import java.awt.Color;
public class JavaA extends Applet {
Dimension originalDimension;
double Yratio;
double Xratio;
int[] xpoints = new int[] {30, 240, 240, 150, 150, 120, 120, 30 };
int[] ypoints = new int[] {30, 30, 60, 60, 190, 190, 60, 60 };
public void init () {
originalDimension = getSize();
}
public void paint (Graphics g) {
Color red=new Color(255,55,100);
g.setColor(red);
Dimension resizedDimension = getSize();
if (resizedDimension.height == 0) resizedDimension.height = 1;
if (resizedDimension.width == 0) resizedDimension.width = 1;
Yratio = resizedDimension.height;
Xratio = resizedDimension.width;
Yratio /= originalDimension.height;
Xratio /= originalDimension.width;
for (int i = 0; i < xpoints.length; i++) {
xpoints[i] = (int) (Xratio * xpoints[i]);
ypoints[i] = (int) (Yratio * ypoints[i]);
}
g.fillPolygon(xpoints, ypoints, xpoints.length);
//save dimension for next round of resize calculation
originalDimension.width = resizedDimension.width;
originalDimension.height = resizedDimension.height;
}
}