Sign in to follow this  
World69star

[java] Manipulating values in Array Help!!!!!

Recommended Posts

Hi everyone .....I am trying to make a simple method to scale a drawing bigger or smaller. Here is the code to the drawing which works btw..without scaling method:
[Source]
package Chapter17Graphics;
import java.awt.*;
/**
 * Created by IntelliJ IDEA.
 * User: Rob H
 * Date: Aug 29, 2005
 * Time: 3:16:00 AM
 * Workshop for chapter 17
 */
public class Drawing extends java.applet.Applet {
    Polygon hair;
	
	public void init() {

		int[] hairX = {
	        125, 131, 156, 217, 270, 314, 244, 233, 196, 162, 147, 153, 180, 189,
	        125
	    };
	    int[] hairY = {
	        314, 122, 75, 57, 96, 287, 319, 118, 87, 92, 133, 203, 231, 258, 314
	    };
	    hair = new Polygon(hairX, hairY, 15);
	    setBackground(Color.LIGHT_GRAY);
	}

    public void paint(Graphics g) {
        g.setColor(Color.white);
        g.fillRoundRect(147, 84, 103, 74, 23, 23);
        g.fillOval(147, 94, 103, 132);

        g.setColor(Color.black);
        g.fillPolygon(hair);

        int[] eyebrow1X = { 151, 168, 174, 171, 178, 193 };
        int[] eyebrow1Y = { 145, 140, 148, 184, 191, 188 };
        g.drawPolyline(eyebrow1X, eyebrow1Y, 6);

        int[] eyebrow2X = { 188, 197, 213, 223 };
        int[] eyebrow2Y = { 146, 141, 142, 146 };
        g.drawPolyline(eyebrow2X, eyebrow2Y, 4);

        int[] mouthX = { 166, 185, 200 };
        int[] mouthY = { 199, 200, 197 };
        g.drawPolyline(mouthX, mouthY, 3);

        g.fillOval(161, 148, 10, 3);
        g.fillOval(202, 145, 12, 5);
    }
}
[/Source]
Now what I need to do is make a "Scaling" method that can multiply every integer in an array by some kind of common factor. So I can change the points in a polygon to make it bigger or smaller before it is created. Here is what I tried:
[Source]
  public void scaling(int a, int[] polyArray){
    //you would use HairX as an example in the parameter
       //and say 2 for int a parameter to scale the part hair up by 2
     polyArray2= a*polyArray;
	int[]polyArray2;
		polyArray2 = new Polygon;
}
[/Source]
This cleary doesn't work and just shows how really newbieish I really am when it comes to trying to work with Arrays. I always get a error about how int's and int[] can't uses operators(+-*/etc.)with each other and I've tried to cast 1 to the other with parenthatiese but that doesn't work either(works for freakn doubls to ints etc.. so I gave it a try... ....If anyone can help me rebuild this method and tell me how to properly implement it, I would appreciate it. I know I will need this again. (btw I couldn't find this in the tutorials) thanks for the help...Rob

Share this post


Link to post
Share on other sites
Something like this should work



public void ScaleArray( int factor, int[] array )
{
for ( int iter = 0;
iter < array.length;
++iter )
{
array[iter] *= factor;
}
}





I'm not a Java coder but its pretty similar to C++ so something like that should work. If it doesn't then it isnt far off.

ace

* Edited since lucky_monkey's post [grin]

Share this post


Link to post
Share on other sites
You need to traverse through the entire array individually changing each element.

IE

int [] list = new int[100];
int scale = 2

for(int index = 0; index < list.length; index++)
{
list[index] *= scale;
}




EDIT: Damn I was beat!

Share this post


Link to post
Share on other sites
Quote:
Original post by ace_lovegrove
Something like this should work

*** Source Snippet Removed ***

I'm not a Java coder but its pretty similar to C++ so something like that should work. If it doesn't then it isnt far off.

ace
That works, but in Java you can use array.length instead of passing in the length.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this