Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By kneitzel

Thread: Can I enlarge an image as it is print out by Graphics by the scale of 2?

  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    176
    Rep Power
    1

    Default Can I enlarge an image as it is print out by Graphics by the scale of 2?

    So I have scale, and you can change it, like I print out a bufferedimage read by imageio from a file, can I enlarge it by 2x, because my tiles are too small eventhough they're 16x16, should I decided to decrease my tile grid from 40 to 20, but instead change the scale by 2. Can I enlarge the image as it is put on the screen?

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Can I enlarge an image as it is print out by Graphics by the scale of 2?

    Hi,

    so you are already using Graphics.drawImage to draw your picture? Then you might have missed the overload of the function that takes areas of the source and target. If the size of these areas is different, then the image is scaled.
    Drawing an Image (The Java™ Tutorials > 2D Graphics > Working with Images)

    With kind regards,

    Konrad

  3. #3
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    176
    Rep Power
    1

    Default Re: Can I enlarge an image as it is print out by Graphics by the scale of 2?

    Konrad, could you give me an example of:
    Java Code:
    boolean Graphics.drawImage(Image img,
           int dstx1, int dsty1, int dstx2, int dsty2,
           int srcx1, int srcy1, int srcx2, int srcy2,
           ImageObserver observer);
    So I can use it on my images?
    So like can you write an example of this code printing the image on 0, 0 in the scale of 1, and then another for which would also print the image on 0, 0 but in the scale of 2. I'm not really understanding this command, even if I read through the Java Doc.

    Thanks,
    Augustas

    EDIT:

    Just in case, my code for the drawing, what do I change?:
    Java Code:
    	public void rPREINIT(Graphics g, BufferStrategy bs) {
    		
    		g.setColor(Color.BLACK);
    		g.fillRect(0, 0, getWidth(), getHeight());
    		
    		g.setColor(Color.WHITE);
    		gRECT(g,5,5,645,485,true);
    		
    		g.drawImage(tile("res/example_tile_grass_01.png"), 5, 5, null);
    		g.drawImage(tile("res/example_tile_grass_01.png"), 21, 5, null);
    		
    		g.dispose();
    		bs.show();
    	}
    	
    	public BufferedImage tile(String path) {
    		BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    		try { image = ImageIO.read(new File(path)); } //(new File(path)
    		catch (IOException e) { e.printStackTrace(); }
    		
    		/*try { image = ImageIO.read(Game.class.getResourceAsStream(path)); }
    		catch (IOException e) { e.printStackTrace(); }*/
    				
    		return image;
    	}
    Last edited by augustas656; 04-08-2014 at 03:57 PM.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,086
    Rep Power
    4

    Default Re: Can I enlarge an image as it is print out by Graphics by the scale of 2?

    What happened to experimenting your way through this? Have you tried entering different numbers as parameters to see what happens?
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  5. #5
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    176
    Rep Power
    1

    Default Re: Can I enlarge an image as it is print out by Graphics by the scale of 2?

    Well, there are 8 parameters, and they seem to vary very much, I'd need to do like 20 experiments maybe, seems a bit too much, someone could just write three sentences or 6 lines of code and I'd understand, doesn't seem so hard for a respondant who knows how to do it.

  6. #6
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    176
    Rep Power
    1

    Default Re: Can I enlarge an image as it is print out by Graphics by the scale of 2?

    Do I like specify the x,y of the top left corner and the x,y of the bottom right corner in the dst parameters for it to fit the image, or?

  7. #7
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    176
    Rep Power
    1

    Default Re: Can I enlarge an image as it is print out by Graphics by the scale of 2?

    Ahh, I see, can someone just tell me what the src parameters, I'm not going to use them, but just wondering...?

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,086
    Rep Power
    4

    Default Re: Can I enlarge an image as it is print out by Graphics by the scale of 2?

    The JavaDoc is very clear:
    Draws as much of the specified area of the specified image as is currently available, scaling it on the fly to fit inside the specified area of the destination drawable surface.
    In other words: you specify a source rectangle from the given Image that will be scaled to fit the destination rectangle. That's all there is to it. (And 20 experiments is nothing when you're entering a new territory in coding. Expect to do many, many more.)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  9. #9
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    176
    Rep Power
    1

    Default Re: Can I enlarge an image as it is print out by Graphics by the scale of 2?

    Quote Originally Posted by SurfMan View Post
    The JavaDoc is very clear:

    In other words: you specify a source rectangle from the given Image that will be scaled to fit the destination rectangle. That's all there is to it. (And 20 experiments is nothing when you're entering a new territory in coding. Expect to do many, many more.)
    Things can be a lot easier when someone can help, and I only done 3 tests so... P:

  10. #10
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Can I enlarge an image as it is print out by Graphics by the scale of 2?

    Hi,

    you have to give the size that should be printed.

    So imagine that you:
    - have an image img.
    - it has the size dx,dy
    - you want to draw it on screen at tx,ty
    - you want to draw it completly (so from 0,0 to dx,dy)
    - you want to draw it double the size:
    ==> g.drawImage(img, tx, ty, 2*dx, 2*dy, 0,0, dx, dy, null);

    So you give:
    a) the image
    b) The target area where the image should be shown
    c) the source area that should be shown
    d) The observer if required

    And maybe you understand this easier when I try to explain why they did it this way:
    The possibility to just draw a part of an image is very useful if you want to reduce the number of image files in your application. You could simply create one big image and then use that image for all your graphic operations.
    So Imagine that you create a picture of size 160x160. And then you put all your 16x16 images in it ... Then you have 10 rows and 10 columns and you could decide to simply print one image:

    Java Code:
    private Image images; // This is the big image with all your small images!
    final int SIZE_X = 16;
    final int SIZE_Y = 16;
    
    public void printImage(Graphics g, int column, int row, int x, int y) {
      g.drawImage(images, x, y, SIZE_X, SIZE_Y, row*SIZE_X, column*SIZE_Y, SIZE_X, SIZE_Y, null);
    }
    So you just load one Image (images must be loaded first!) and then you can print out the parts. row and column start from 0.
    And be aware that there is no check if you are still in the range of the loaded image ...

    With kind regards,

    Konrad
    SurfMan likes this.

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,086
    Rep Power
    4

    Default Re: Can I enlarge an image as it is print out by Graphics by the scale of 2?

    CSS sprites the Java way :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

Similar Threads

  1. JFrame & Graphics, Width & Height Not To Scale?
    By augustas656 in forum New To Java
    Replies: 0
    Last Post: 04-07-2014, 09:40 PM
  2. How to scale an image to desired size?
    By asifzbaig in forum AWT / Swing
    Replies: 4
    Last Post: 06-26-2011, 10:07 PM
  3. Error in converting colour image to gray scale
    By LankanSniper in forum Java 2D
    Replies: 9
    Last Post: 12-14-2009, 05:30 PM
  4. Replies: 3
    Last Post: 03-21-2009, 10:34 PM
  5. print variable with paint(Graphics g) ??
    By tghn2b in forum New To Java
    Replies: 10
    Last Post: 12-29-2008, 01:11 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
  •