Jump to content

View more

Image of the Day

Inventory ! Va falloir trouver une autre couleur pour le cadre D: #AzTroScreenshot #screenshotsaturday https://t.co/PvxhGL7cOH
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Adding a picture file to a drawn rectangle

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 LoneWolfe007   Members   


Posted 13 May 2014 - 08:46 PM

Hello all,


I did research before I decided to post this so at least I know I can read and follow directions. The code I have draws rectangles, which is awesome, but I have been wracking my brain trying to figure out how to replace the rectangles with a picture I made instead of just filling them in with the color brown. I will attach some pictures so this isn't as crazy sounding as I type it here.


Current rectangles: https://db.tt/Nq72xGm8

New rectangles after picture added: https://db.tt/ny9rD2ye


After doing a search on your site I found some references but not sure if they exactly answer the question I am asking. Here is the code chunk I would like help with:

// Add rectangles at top increasing height at start and decreasing
// at the end. These rectangles will move from right to left side
// of the screen and increase in height with time to make game herder.
float upX= 0;
float heightUp= 30;
heightPlus= 5;
for(int i= 0; i<30; i++){
final int num= i;
final Rectangle rect= new Rectangle(upX, 0, 60, heightUp){
protected void onManagedUpdate(float pSecondsElapsed){
if(state== 50){
heightPlus= 0;
if(this.getX()+ this.getWidth()<0){
this.setHeight(this.getHeight()+ heightPlus);
this.setPosition(rectsUp.get(29).getX()+ rectsUp.get(29).getWidth(), this.getY());
this.setPosition(rectsUp.get(num-1).getX()+ rectsUp.get(num-1).getWidth(), this.getY());

if(this.collidesWith(copterCollider) && !gameOver){
gameOver= true;
rect.setColor(colorRed/255f, colorGreen/255f, colorBlue/255f);
final PhysicsHandler rectHandler= new PhysicsHandler(rect);
heightUp+= 3;
heightUp-= 3;
upX+= 60;

I understand that the picture has to be called and inserted but I suppose I assumed it would be a lot easier than what it is turning out to be because the rectangles are already drawn.


My thought on the subject is that at the point where the code reads: rect.setColor(colorRed/255f, colorGreen/255f, colorBlue/255f); I would keep the code the way it is basically but change setColor to call the picture or even change the variables in a way to show the picture instead of colors. After looking on Google I found a few posts that were in the ball park that talked about using a specific code, but it is slightly confusing to me how to implement it. The main article I found is located  here: http://stackoverflow.com/questions/15247971/fill-rectangle-with-image-in-java and in that same article is a second link which takes me to a website with a tutorial: http://docs.oracle.com/javase/tutorial/2d/images/drawimage.html.


Could someone explain to me in a more simplified English what exactly to do? I'm not looking for step by step just a better idea on how to accomplish my goal.

Any help on the subject would be fantastic, thank you in advance.


Edited by LoneWolfe007, 13 May 2014 - 08:47 PM.

#2 Eck   Members   


Posted 14 May 2014 - 11:19 AM

Try starting at the start of that tutorial in your second link.



It seems to walk you through the necessary steps to load an image and draw it to the screen. The internal links even have full code samples.


Basically there's some image/graphics library code that you import into your code which handles things like loading an image from a file and drawing an image to the screen. After it's imported, all you have to do is use it in your code.


In the code sample you've provided, it looks like you're drawing several filled in boxes. If you consider those rectangles as your destination coordinates, you should be able to plug them into the call to Graphics.drawImage and draw the image you loaded instead of the brown rectangle. 


If you still need more help, let me know,

- Eck

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG

#3 LoneWolfe007   Members   


Posted 16 May 2014 - 07:00 PM

Thank you very much for your help. I have added:

//****************************************CAVE IMAGE TO LOAD****************************************************//
final BitmapTextureAtlas caveTopTexture= new BitmapTextureAtlas(60, 180, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
caveTopRegion= MyTextureRegionFactory.createFromAsset(caveTopTexture, context, "caveTop.png", 0, 0);

This is the command which loads the image of "caveTop." I also added:

//****************************Cave Variable Images*******************************//
public static TextureRegion caveTopRegion;

This command sets up the variable used to initiate TextureRegion and naming it caveTopRegion. From here I believe I can start tinkering with the code: 

rect.setColor(colorRed/255f, colorGreen/255f, colorBlue/255f);

and trying to add in the picture. Am I on the right track?

Edited by LoneWolfe007, 16 May 2014 - 07:02 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.