Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Norm

Thread: specifying which image to draw with drawimage with a string

  1. #1
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    100
    Rep Power
    0

    Post 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...

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default 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
    natdizzle likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    100
    Rep Power
    0

    Default 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

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default 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,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default 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.

  6. #6
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    100
    Rep Power
    0

    Default 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;
    	}
    }
    now im goint to acess this object from another class and pass int "int 9" and get "9.png"

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default 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.

  8. #8
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    100
    Rep Power
    0

    Default Re: specifying which image to draw with drawimage with a string

    ya i want to simplify this but atm this is as simple as it gets with my code base

Similar Threads

  1. Replies: 4
    Last Post: 10-14-2012, 10:16 PM
  2. How to draw over a jpg image
    By vagras in forum New To Java
    Replies: 2
    Last Post: 12-28-2011, 04:27 AM
  3. Replies: 1
    Last Post: 11-30-2011, 04:18 PM
  4. Replies: 1
    Last Post: 07-28-2011, 08:48 AM
  5. Load and Draw image
    By TheSheepeh in forum New To Java
    Replies: 2
    Last Post: 03-19-2011, 12:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •