Results 1 to 8 of 8
- 10-15-2013, 07:20 PM #1
specifying which image to draw with drawimage with a string
say i have "1.png" and integer x = 1 and i want to draw 1.png which is Bufferedimage b1
so i can say g2d.drawimage(b1, new AffineTransform(), this);
but what if i wanted to say g2d.drawimage(b + x (equals "b1" right cause x = 1 b + x = "b1") but that makes my image argument slot or that doesnt work i mean it doesnt add the two with the + it just says cannon find "b"
im trying to make my integer x (which equals 1) the name of the image i want to draw so if i have 1.png which is a picture of a "1" and integer 1 i can say draw my image 1.png with drawimage(x (which is 1
anyone know what im kinda saying i have ints 1 2 3 4 5 all the way to 9 and a 0 and images if i have int 6 i want to g2d.drawimage(image6
does anyone hav e any ideas?
i could resort to : if (myint = 6){
g2d.drawimage(my6image
}
etc...Nathaniel Victor Nelson
- 10-15-2013, 07:46 PM #2
Re: specifying which image to draw with drawimage with a string
Is your problem how to create the name of a file? Use String concatentation:
Java Code:String fn = "b" + i +".jpg"; // sets fn to b1.jpg for i = 1
If you don't understand my response, don't ignore it, ask a question.
- 10-15-2013, 08:19 PM #3
Re: specifying which image to draw with drawimage with a string
ya but drawimage wants a image not a string i think ill just write a class its actually not that much i was trying to save code
Nathaniel Victor Nelson
- 10-15-2013, 08:23 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: specifying which image to draw with drawimage with a string
Sounds to me like you want to create a dynamic variable of some sort where you have an image type of b1 and you want to access it via a string reference of some type "b" + 1. If that's the case, you can do it with reflection. However, if you know ahead of time the image names you can store them in a map and access them that way.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-15-2013, 08:24 PM #5
Re: specifying which image to draw with drawimage with a string
You need to read the image file (using the name that was built in a String) into a object like BufferedImage so it can be used in the drawImage() method. Then use jim829's idea.
If you don't understand my response, don't ignore it, ask a question.
- 10-15-2013, 09:03 PM #6
Re: specifying which image to draw with drawimage with a string
it wasnt that bad, heres how i did it:
Java Code:package drawFramePackage; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class Numbers { BufferedImage n1, n2, n3, n4, n5, n6, n7, n8, n9, n0, placeholder; BufferedImage BiL[] = {n1, n2, n3, n4, n5, n6, n7, n8, n9, n0}; //, placeholder}; String names[] = {"1.png", "2.png", "3.png", "4.png", "5.png", "6.png", "7.png", "8.png", "9.png", "0.png"}; /** * @param args * @throws IOException */ /* public static void main(String[] args) { // TODO Auto-generated method stub } */ public Numbers() throws IOException{ for (int i = 0; i < 9; i++){ BiL[i] = ImageIO.read(new File(names[i])); } } public BufferedImage numbers(Integer integer){ if (integer == 0){ //return placeholder = BiL[9]; //n0; } if (integer == 1){ placeholder = BiL[0]; //n1; } if (integer == 2){ placeholder = BiL[1]; //n2; } if (integer == 3){ placeholder = BiL[2]; //n3; } if (integer == 4){ placeholder = BiL[3]; //n4; } if (integer == 5){ placeholder = BiL[4]; //n5; } if (integer == 6){ placeholder = BiL[5]; //n6; } if (integer == 7){ placeholder = BiL[6]; //n7; } if (integer == 8){ placeholder = BiL[7]; //n8; } if (integer == 9){ placeholder = BiL[8]; //n9; } /* if (integer == 0){ placeholder = n0; } */ return placeholder; } }
Nathaniel Victor Nelson
- 10-15-2013, 09:23 PM #7
Re: specifying which image to draw with drawimage with a string
If the index to the BiL array is always one less than the int arg passed to the method: instead of a group of if statements, just subtract 1 from the int arg, test if it is in bounds and use it as index to the BiL array.
There is no need for the empty placeholder variables. Just define the BiL array the size you want and then fill it in the constructor.If you don't understand my response, don't ignore it, ask a question.
- 10-16-2013, 03:11 AM #8
Similar Threads
-
Really need help with this one.. (Clopping the image using drawImage)
By Lionlev in forum Java 2DReplies: 4Last Post: 10-14-2012, 11:16 PM -
How to draw over a jpg image
By vagras in forum New To JavaReplies: 2Last Post: 12-28-2011, 05:27 AM -
An image drawn by drawimage on top of Swing application flikers and not stable
By Ghozia in forum AWT / SwingReplies: 1Last Post: 11-30-2011, 05:18 PM -
How to import image, draw circles/text on it, and save a new image to disk
By InTheEndo in forum Java 2DReplies: 1Last Post: 07-28-2011, 09:48 AM -
Load and Draw image
By TheSheepeh in forum New To JavaReplies: 2Last Post: 03-19-2011, 01:19 PM
Bookmarks