0

I want my objects to change for that small position, for which screen size is changed, whether is an image or graphic shape..

So I don't want:

g.drawImage(someImg, 10, 10); 

So how should I do this? I have an idea of doing with percentages, is it a good way ? For example, I can give value of 5% to both x and y coordinates, from a total width and total height size.

UPDATE:

public static float getValueOfPercentage(float percentage, float value) {
    return (percentage / 100.0f) * value;
}

float logoX = panelCenterX - logoCenterX;
float logoY = getValueOfPercentage(5, panelMaxY);
g.drawImage(logoImage, logoX, logoY);
nellykvist
  • 125
  • 1
  • 1
  • 8
  • For each object, you need to supply weights which define the percentage of their position. When paint is called, you get the components width and height and apply the positional weight to calculate the location (you can do the same thing for sizes as well). You can use something Point2D to store the weights and some kind of Map to associate the objects with the weights – MadProgrammer Oct 19 '13 at 20:13
  • What MadProgrammer said. Using math is the only way to calculate scaling. If you want a rectangle that's half the width of a panel and placed in the center, the width is `panel.getWidth() / 2` or `0.5 * panel.getWidth()` and the x coordinate is `panel.getWidth() / 4` or `0.25 * panel.getWidth()` or `(panel.getWidth() / 2) - (rect.width / 2)`. Just make the proportions whatever you want and do the math. – Radiodef Oct 19 '13 at 20:16
  • *"I want my objects to change for that small position, for which screen size is changed"* Do they remain the same **size**? If not, look to [`AffineTransform.getScaleInstance(sx,sy)`](http://docs.oracle.com/javase/7/docs/api/java/awt/geom/AffineTransform.html#getScaleInstance%28double,%20double%29). – Andrew Thompson Oct 19 '13 at 20:22
  • You can check [this example](http://stackoverflow.com/questions/11819669/absolute-positioning-graphic-jpanel-inside-jframe-blocked-by-blank-sections/11822601#11822601) out, which uses a simular concept – MadProgrammer Oct 19 '13 at 20:32
  • I forgot to mention, I'm using slick2D. @MadProgrammer I updated post with code, where I tried out with percentage, is that what u mean in first comment, by saying "For each object, you need to supply weights which define the percentage of their position." ? – nellykvist Oct 19 '13 at 21:10

0 Answers0