• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Adding a picture file to a drawn rectangle

2 posts in this topic

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

Share this post

Link to post
Share on other sites

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


Share this post

Link to post
Share on other sites

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

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  
Followers 0