Results 1 to 12 of 12
- 03-01-2011, 04:57 AM #1
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
Trying to display a copied Graphic object - SSCCE
mainGraphic has the original Graphics object.
printPreview copies the Graphics object from mainGraphic and scales it down.
if I add them both, they both show up. Great!
But why when I comment out the mainGraphic (in red below) does neither one show up?
Can someone please explain this?
Java Code:import javax.swing.*; import java.awt.*; import java.awt.print.*; public class TransferGraphics { JFrame frame = new JFrame(); JPanel mainPanel = new JPanel(); MainGraphic mainGraphic = new MainGraphic(); PrintPreview printPreview = new PrintPreview(); TransferGraphics() { mainGraphic.setPreferredSize(new Dimension(850, 750)); printPreview.setPreferredSize(new Dimension(400, 640)); //[COLOR="Red"]mainPanel.add(mainGraphic, BorderLayout.WEST); WHY IS THIS NEEDED?[/COLOR] mainPanel.add(printPreview, BorderLayout.EAST); mainPanel.setBackground(Color.darkGray); frame.add(mainPanel); frame.setSize(1280, 760); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public class MainGraphic extends JPanel { public void paintComponent(Graphics g) { g.setColor(Color.WHITE); g.fillRect(0, 0, 850, 1100); g.setColor(Color.RED); g.fillRect(0, 0, 50, 50); g.fillRect(55, 0, 50, 50); g.fillRect(0, 55, 50, 50); g.fillRect(55, 55, 50, 50); g.setColor(Color.BLACK); g.fillRect(800, 0, 50, 50); g.setColor(Color.BLUE); g.fillRect(800, 1050, 50, 50); g.setColor(Color.GREEN); g.fillRect(0, 1050, 50, 50); } } public class PrintPreview extends JPanel { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.scale(.4, .4); mainGraphic.print(g); } } // MAIN METHOD public static void main(String[] args) { TransferGraphics tg = new TransferGraphics(); } }
- 03-01-2011, 05:06 AM #2
Take a close look at what the MainGraphic and PrintPreview classes do.
- 03-01-2011, 05:09 AM #3
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
mainGraphic draws the graphics.
printPreview copies the graphics.
Is there something I am missing?
- 03-01-2011, 05:12 AM #4
The only time the code does any painting is in the MainGraphic class. If you don't add the MainGraphic class to the mainPanel then obviosuly you are not going to see anything. The painting is being performed on a panel that is not visible.
- 03-01-2011, 05:24 AM #5
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
hmm... so how would one create an image without it painting it on the screen?
- 03-01-2011, 07:47 PM #6
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
Is this graphic stuff in the wrong forum? Is there a better place to ask graphics and graphics 2D questions?
- 03-01-2011, 09:13 PM #7
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
- 03-02-2011, 06:27 PM #8
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
bump bump bump
- 03-02-2011, 08:47 PM #9
- 03-02-2011, 08:48 PM #10
- 03-03-2011, 03:10 AM #11
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
I have been reading about bufferedimage on my spare time. The purpose I go to this forum is to save my time. I only have so much time to study java. Can you please just direct me to the right area to study?What happened when you tried it?
db
How can i create a Graphics object that is not painted on screen and copy it?
My program....
There is a blank white page of a 8.5 X 11 paper with a 3X4 grid on it.
There is a user area to create grids on the right side. SO basically 2 JPanels EAST and WEST.
When the user clicks the "ADD" button it adds the current grid to one of the 3X4 units.
There is a print button - when u click print it prints basically what you see on the left side minus a few grid lines and indention.
**** MY QUESTION *****
Since the left side is just a preview it is also scalable to the user. Which means if I copy that Graphics object from the preview to the Printable print method. It is hard to scale it right for print.
So I figure I would make one central reference graphic object and copy from that. The problem is I cannot use a Graphic object that hasnt actualyl been painted on screen. How can I do this?
here is reduced SSCCE version of code showing when I remove (comment out) the mainGraphic the scaled preview on the right doesnt show anymore.
Java Code:import javax.swing.*; import java.awt.*; import java.awt.print.*; public class TransferGraphics { JFrame frame = new JFrame(); JPanel mainPanel = new JPanel(); MainGraphic mainGraphic = new MainGraphic(); PrintPreview printPreview = new PrintPreview(); TransferGraphics() { mainGraphic.setPreferredSize(new Dimension(850, 750)); printPreview.setPreferredSize(new Dimension(400, 640)); //mainPanel.add(mainGraphic, BorderLayout.WEST); mainPanel.add(printPreview, BorderLayout.EAST);[COLOR="Red"]How can I display this without displaying mainGraphic first?[/COLOR] mainPanel.setBackground(Color.darkGray); frame.add(mainPanel); frame.setSize(1280, 760); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public class MainGraphic extends JPanel { public void paintComponent(Graphics g) { g.setColor(Color.WHITE); g.fillRect(0, 0, 850, 1100); g.setColor(Color.RED); g.fillRect(0, 0, 50, 50); g.fillRect(55, 0, 50, 50); g.fillRect(0, 55, 50, 50); g.fillRect(55, 55, 50, 50); g.setColor(Color.BLACK); g.fillRect(800, 0, 50, 50); g.setColor(Color.BLUE); g.fillRect(800, 1050, 50, 50); g.setColor(Color.GREEN); g.fillRect(0, 1050, 50, 50); } } public class PrintPreview extends JPanel { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.scale(.4, .4); mainGraphic.print(g); } } // MAIN METHOD public static void main(String[] args) { TransferGraphics tg = new TransferGraphics(); } }
- 03-03-2011, 03:18 AM #12
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
Similar Threads
-
display object corresponding to JComboBox selection
By kerode in forum New To JavaReplies: 1Last Post: 11-23-2010, 06:26 PM -
Invalidatig session when URL is copied on new browser window
By maverick_friend in forum Java ServletReplies: 0Last Post: 03-13-2010, 09:12 AM -
How to change font/ font color etc in a graphic object using JCombobox?
By JavaInLove in forum AWT / SwingReplies: 5Last Post: 04-25-2009, 08:00 PM -
intimating file(s) have reached/copied in directory
By ashu261 in forum Threads and SynchronizationReplies: 1Last Post: 04-23-2008, 06:24 PM -
how can objects themselves be copied???
By ishakteyran in forum New To JavaReplies: 1Last Post: 12-29-2007, 10:04 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks