Results 1 to 4 of 4
  1. #1
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Smile Drawing onto a buffered image

    Hey iv been wanting to try to draw ontop of a buffered image, at the moment i can only seem to load an image from a file from code iv been testing from on the internet, but i cant seem to find a way to create a bufferedimage and then draw using java2d ontop of it so its still an buffered image and editable.

    here is the code im using:

    Java Code:
           public void paintComponent(Graphics g)
    	{  
    		 Graphics2D g2d = (Graphics2D) g;
    		 
    	    
    		 g2d.drawImage(castle, 0, 0, null);
    		 g2d.setColor (Color.LIGHT_GRAY);  
    		 g2d.drawRoundRect(0, 0, 200, 200, 0, 0);
    		 GrayImage();
    	}
    	
    	public void GrayImage() 
    	{
    
            size = new Dimension();
            castle = new ImageIcon("castle.gif").getImage();
            size = new Dimension();
            size.width = 40;
          //  size.height = castle.getHeight(null);
            size.height = 100;
            setPreferredSize(size);
            bufferedImage =  new BufferedImage(size.width, size.height, 
                                 BufferedImage.TYPE_BYTE_GRAY);
    
            Graphics g = bufferedImage.getGraphics();
            g.drawImage(castle, 0, 0, null);
            
            g.setColor (Color.BLACK);  
            g.drawRect(0, 0, 500, 500);
            g.dispose();
        }
    As above i tried to load the image from a file, which worked and then draw black rectangle ontop but it never shows up. So if anyone could tell me how to just create a buffered image and draw onto that instead of the screen id appreciate it :) even a link would suffice.
    Last edited by Fubarable; 04-09-2011 at 08:57 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Moderator Edit: Code tags added to the post above.

    To the OP, in the future, to do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag
    Java Code:
     above your pasted code and the tag
    below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    Best of luck

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You definitely don't want to create the buffered image from within the paintComponent or a method called in the paintComponent as you're doing. What you want to do is to first create the BufferedImage and draw on it in the class constructor, and then instead of drawing castle in the paintComponent, draw the BufferedImage itself (first checking that it's not null). So paintComponent becomes:

    Java Code:
    public void paintComponent(Graphics g) {  
           Graphics2D g2d = (Graphics2D) g;
           
           //g2d.drawImage(castle, 0, 0, null);
    
           if (bufferedImage != null) {
              g2d.drawImage(bufferedImage, 0, 0, null);
           }
    
           g2d.setColor (Color.LIGHT_GRAY);  
           g2d.drawRoundRect(0, 0, 200, 200, 0, 0);
           // GrayImage();
    }

    If you're still stuck, then please create and post an SSCCE so we can run and modify your code.

  4. #4
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Default

    thats fantastic, thanks a ton and ill make sure i add in the code tags next time :)

Similar Threads

  1. Rotating Buffered Image distorts image
    By VortexSpin in forum Java 2D
    Replies: 1
    Last Post: 02-13-2011, 05:54 AM
  2. adding a buffered image over a jpanel
    By fatmat9 in forum New To Java
    Replies: 1
    Last Post: 11-23-2010, 03:23 AM
  3. Replies: 4
    Last Post: 03-23-2010, 03:46 PM
  4. Update existing buffered image
    By rosh72851 in forum New To Java
    Replies: 24
    Last Post: 12-04-2008, 03:43 AM
  5. Unable to draw buffered image
    By pedjasmek in forum Java 2D
    Replies: 7
    Last Post: 08-08-2008, 03:49 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
  •