aspose file tools*
The moose likes Applets and the fly likes Resizing Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Resizing Applet" Watch "Resizing Applet" New topic
Author

Resizing Applet

Randy Helene
Greenhorn

Joined: Apr 16, 2002
Posts: 21
I am drawing a solid T in an applet. I need to resize the T proportionately if the applet window is resized. I have a formula to do it but it is not working, and I don't know what I am doing wrong. This is the code I have. Can you help please. Thank you.
import java.awt.*;
import java.applet.*;
import java.awt.Color;
public class PolygonDemo extends Applet {
public void paint (Graphics g) {
int xpoints[] = {30, 240, 240, 150, 150, 120, 120, 30 };
int ypoints[] = {30, 30, 60, 60, 190, 190, 60, 60 };
int num = 8;
int HN = getHeight();
int WN = getWidth();
int y_new=0;
int x_new=0;
int i,j;
Color red=new Color(255,55,100);
g.setColor(red);
g.fillPolygon(xpoints, ypoints, num);
for (i=0; i<8; i++){
for (j=0; j<8; j++)
y_new=(HN/500)*ypoints[i];
x_new=(WN/300)*xpoints[j];
}
}
}
The 500 and 300 are the height and width I have defined in the HTML file.
[ April 16, 2002: Message edited by: Randy Helene ]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
This is more appropriate for the Applets forum - so I will move it there.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Doanh Nguyen
Ranch Hand

Joined: Dec 02, 2000
Posts: 45
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;
}
}
Randy Helene
Greenhorn

Joined: Apr 16, 2002
Posts: 21
Thank you for your help. This board is great and I am really learning a lot.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Resizing Applet