How to copy an image a few dozen times into the same picture with dif coordinates.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mary Seelye
    New Member
    • Oct 2010
    • 14

    How to copy an image a few dozen times into the same picture with dif coordinates.

    I am trying to draw a town. I have the pic of one house drawn. I need to be able to write a code to copy that picture and multiply it at different coordinates and different sizes. I have the code for the one house written, but I can't figure out how to multiply it. (Pic attached of example) I'm using DrJava.

    My code for the house is:
    Code:
          public void drawHouse() {
        //get graphics
        Graphics graphics = this.getGraphics();
        //start with black color
        graphics.setColor(Color.blue);
        //draw a filled rectangle
        graphics.fillRect(10,210,200,100);
        //set color back to black
        graphics.setColor(Color.black);
        //draw triangle
        Polygon poly = new Polygon();
        poly.addPoint(10, 210);
        poly.addPoint(110, 50);
        poly.addPoint(210, 210);
        graphics.fillPolygon(poly);
        //draw windows with yellow
        graphics.setColor(Color.yellow);
        graphics.fillRect(20,220,50,45);
        graphics.fillRect(150,220,50,45);
        //oval window
        graphics.fillOval(85,150,50,45);
        //draw door with black
        graphics.setColor(Color.black);
        graphics.fillRect(85,220,50,90);
    Attached Files
    Last edited by Nepomuk; Oct 19 '10, 02:02 PM. Reason: Please use [CODE] tags
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Well, the general idea would be to create a drawHouse() function which takes the arguments
    • x The x-coordinate of, say, the top left corner.
    • y The y-coordinate of the same corner.
    • scale A scaling factor.
    Or alternatively:
    • x The x-coordinate of, say, the top left corner.
    • y The y-coordinate of the same corner.
    • width The width of the house.
    • height Th height of the house
    The code you already wrote would have to be modified something like this:
    Code:
    //...
    graphics.fillRect(x,y,20*scale,10*scale);
    //...
    poly.addPoint(x, y);
    poly.addPoint(x + 10*scale, 5*scale);
    poly.addPoint(x, x + 20*scale);
    //...
    or
    Code:
    //...
    graphics.fillRect(x,y-height/3,width,2*height/3);
    //...
    poly.addPoint(x, y);
    poly.addPoint(x + width, width/2);
    poly.addPoint(x, x + width);
    //...
    and so on and so forth.

    You don't have to create a new Graphics- or Polygon-Object for each house, by the way (though you could).

    Greetings,
    Nepomuk

    Comment

    Working...