Results 1 to 12 of 12
- 03-07-2011, 06:45 AM #1
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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?
- 03-07-2011, 03:22 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Just get the graphics of a BufferedImage and paint to that.i know about panel.print(g), but that only works for on screen graphics.
Java Code://pseudo-code BufferedImage image = new BufferedImage(....) Graphics g = image.getGraphics(); myJPanel.paint(g); g.dispose();
- 03-07-2011, 03:57 PM #3
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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 gLast edited by AcousticBruce; 03-07-2011 at 04:17 PM.
- 03-07-2011, 05:04 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Um, that's because you called your graphics variable g2, not g...
- 03-07-2011, 05:40 PM #5
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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.
- 03-08-2011, 08:17 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You've overidden paintComponent.
print() uses printComponent().
- 03-08-2011, 02:34 PM #7
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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?
- 03-08-2011, 02:40 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 03-08-2011, 03:03 PM #9
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].
dbJava Code:panel.setSize(panel.getPreferredSize());
- 03-08-2011, 03:13 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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...:)
- 03-08-2011, 08:02 PM #11
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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
- 03-09-2011, 08:18 AM #12
Similar Threads
-
Streaming an image byte by byte (and similtaneosly rendering it on screen)
By ea25 in forum New To JavaReplies: 1Last Post: 04-21-2010, 02:28 AM -
print multiple jpanel
By anilkumar_vist in forum New To JavaReplies: 0Last Post: 02-05-2010, 03:28 PM -
print jpanel
By anilkumar_vist in forum New To JavaReplies: 3Last Post: 01-08-2010, 12:08 PM -
print on the client screen
By a_maged in forum NetworkingReplies: 0Last Post: 12-17-2007, 04:10 PM -
How do i tell it to print out the result to the screen???
By paul in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:04 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks