Results 1 to 12 of 12
  1. #1
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default can you print a JPanel without actually rendering it on screen first?

    How can I print a extended JPanel class (overridden paintComponent) without rendering it to the screen first.

    Perhaps a way to copy a jpanel and its content to a buffered image?


    i know about panel.print(g), but that only works for on screen graphics.
    Have any ideas?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    i know about panel.print(g), but that only works for on screen graphics.
    Just get the graphics of a BufferedImage and paint to that.

    Java Code:
    //pseudo-code
    BufferedImage image = new BufferedImage(....)
    Graphics g = image.getGraphics();
    myJPanel.paint(g);
    g.dispose();

  3. #3
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    Quote Originally Posted by doWhile View Post
    Just get the graphics of a BufferedImage and paint to that.

    Java Code:
    //pseudo-code
    BufferedImage image = new BufferedImage(....)
    Graphics g = image.getGraphics();
    myJPanel.paint(g);
    g.dispose();

    yes I am sure about JComponents print.(g) to an extent, I have only been successful printing this within a paintComponent method and ONLY if it is displayed first.

    since I have to paint with the g2 object (in code below), how can I print a JPanal that is never rendered to screen?

    The reason I want to do this is because I want to use swings FlowLayout to add more extended JPanel objects to a virtual page and then use that as a reference graphic (which will be much smaller).



    Java Code:
    //paperImage is already instantiated to specific size (not shown).
    
    private void createPaperImage() {
    		Graphics2D g2 = paperImage.createGraphics();
    
    		g2.setPaint(Color.WHITE);
    		g2.fillRect(0, 0, PAPER_X, PAPER_Y);
    		g2.translate(DPI, DPI);
    		g2.setPaint(Color.RED);
    		g2.fillRect(0, 0, PAPER_X-DPI*2, PAPER_Y-DPI*2);
    
    
    		//Main page information.
    		//g2.translate(DPI, DPI);
    
                   	[COLOR="Red"]pageInfo.print(g);[/COLOR]
    
    	}
    }
    
    	class PageInfo extends JPanel {
    		PageInfo() {
    			setPreferredSize(new Dimension(250, 250));
    			setBackground(Color.WHITE);
    		}
    		public void paintComponent(Graphics g) {
    			super.paintComponent(g);
    
    			Graphics2D g2 = (Graphics2D) g;
    			g2.translate(50, 50);
    
    			g.setColor(Color.BLACK);
    			g.fillRect(0, 0, 150, 150);
    
    		}
    	}// End pageinfo class.





    cannot find symbol variable g
    Last edited by AcousticBruce; 03-07-2011 at 04:17 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Um, that's because you called your graphics variable g2, not g...

  5. #5
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    yes that is why I get an error for symbol variable.... renaming to g2 would stop the error, but would call the wrong g2. I even tried instatiating a Graphics object in the class but still nothing shows. Probibly because it is not rendered ons screen.

    But i am still wondering if there is a way to print a JPanel to another paintComponent or even copy a JPanel to a BufferedImage without displaying it on screen first?
    Last edited by AcousticBruce; 03-07-2011 at 05:44 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    You've overidden paintComponent.
    print() uses printComponent().

  7. #7
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    But i am still wondering if there is a way to print a JPanel to another paintComponent or even copy a JPanel to a BufferedImage without displaying it on screen first?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    But this isn't displaying it on a screen.
    You are supplying the Graphics object from your Image.
    It will draw onto your Image, not the screen.
    That's the point.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    You do need to set a size for the JPanel otherwise, since it is not added to any realized component hierarchy, the size will be [0, 0].
    Java Code:
    panel.setSize(panel.getPreferredSize());
    db

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Sorry.
    Just realised what's going on.

    Since Swing seems to do it's layout stuff on being setVisible (which you want to avoid doing) you might have to manually set the size and location of your components. Until you do that they will all have a 0 width and height, as well as no location (I think). And note, it's setSize, not setPreferredSize in this case.

    ETA: Or what Darryl said...:)

  11. #11
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    Thanks to darryl and tolls.

    Now that I know this works and I have messed with it. I think I still need to take a different angle with my program.

    Im also going to study setSize vs setPreferred and understand that more.

    thanks guys

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

Similar Threads

  1. Replies: 1
    Last Post: 04-21-2010, 02:28 AM
  2. print multiple jpanel
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 02-05-2010, 03:28 PM
  3. print jpanel
    By anilkumar_vist in forum New To Java
    Replies: 3
    Last Post: 01-08-2010, 12:08 PM
  4. print on the client screen
    By a_maged in forum Networking
    Replies: 0
    Last Post: 12-17-2007, 04:10 PM
  5. Replies: 1
    Last Post: 08-07-2007, 05:04 AM

Posting Permissions

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