• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Resizing Applet

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is more appropriate for the Applets forum - so I will move it there.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your help. This board is great and I am really learning a lot.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic