473,419 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

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

Mary Seelye
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:
Expand|Select|Wrap|Line Numbers
  1.       public void drawHouse() {
  2.     //get graphics
  3.     Graphics graphics = this.getGraphics();
  4.     //start with black color
  5.     graphics.setColor(Color.blue);
  6.     //draw a filled rectangle
  7.     graphics.fillRect(10,210,200,100);
  8.     //set color back to black
  9.     graphics.setColor(Color.black);
  10.     //draw triangle
  11.     Polygon poly = new Polygon();
  12.     poly.addPoint(10, 210);
  13.     poly.addPoint(110, 50);
  14.     poly.addPoint(210, 210);
  15.     graphics.fillPolygon(poly);
  16.     //draw windows with yellow
  17.     graphics.setColor(Color.yellow);
  18.     graphics.fillRect(20,220,50,45);
  19.     graphics.fillRect(150,220,50,45);
  20.     //oval window
  21.     graphics.fillOval(85,150,50,45);
  22.     //draw door with black
  23.     graphics.setColor(Color.black);
  24.     graphics.fillRect(85,220,50,90);
Attached Images
File Type: jpg drawtown.jpg (66.5 KB, 345 views)
Oct 19 '10 #1
1 1820
Nepomuk
3,112 Expert 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. //...
  2. graphics.fillRect(x,y,20*scale,10*scale);
  3. //...
  4. poly.addPoint(x, y);
  5. poly.addPoint(x + 10*scale, 5*scale);
  6. poly.addPoint(x, x + 20*scale);
  7. //...
or
Expand|Select|Wrap|Line Numbers
  1. //...
  2. graphics.fillRect(x,y-height/3,width,2*height/3);
  3. //...
  4. poly.addPoint(x, y);
  5. poly.addPoint(x + width, width/2);
  6. poly.addPoint(x, x + width);
  7. //...
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
Oct 19 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: lawrence | last post by:
I've the form you see below, which is supposed to upload an image. I expect the image name as a var to be $uploadedFile. In the receiving function, I've this code: global $uploadedFile,...
2
by: Bill N. | last post by:
Using VB .Net 2003 Standard Edition. This simple form has a "File in Use" problem while the executable is running. Created this simple program to make sure that I hadn't done something in my...
2
by: kris_scheyer | last post by:
Hi, I've searched dozens of forums and websites to find a way to have javascript copy an image I click on into the clipboard of windows. I found a few examples but I couldn't get any of them to...
1
by: Frank Rizzo | last post by:
How can I retrieve the dimentions of the image that's been loaded into a picture box? Thanks
0
by: Ricardo Furtado | last post by:
I'm developing a drawing software in visual basic .Net and i need to copy the image that exists in a picturebox and automaticaly paste it in another picturebox as being part of that other image....
0
by: fergarduno | last post by:
I do have a RAW image file and i want to load it to a picture box i want to know please if it es posible and how to do it without having to buy an ActiveX component Thanks
1
by: Shmedlap | last post by:
I want a picture of my form for documentation, but when I use <Print Scrn> with or without <Ctrl><Alt> I get also the entire Access application. I just want the form window. True I could open up the...
10
by: aeris | last post by:
Hello, sorry for disturbing. I have a pictureBox on my form. May i know how to open the image from the pictureBox in Windows Picture and Fax Viewer? I've tried the code below, but it failed...
2
by: nuhbye | last post by:
I want to display a bitmap in its original size using a picturebox and scrollbar, but i couldn't find any option to clip a bitmap and display it in the picturebox, i am searching for one like we have...
2
by: Eugene Gephart | last post by:
Trying to link radio buttons with images in a picture box, but can't remember how to start the coding.
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.