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 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();
        }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Take a close look at what the MainGraphic and PrintPreview classes do.

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

    Default

    mainGraphic draws the graphics.
    printPreview copies the graphics.

    Is there something I am missing?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

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

    Default

    hmm... so how would one create an image without it painting it on the screen?

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

    Default

    Is this graphic stuff in the wrong forum? Is there a better place to ask graphics and graphics 2D questions?

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

    Default

    Quote Originally Posted by AcousticBruce View Post
    hmm... so how would one create an image without it painting it on the screen?
    I have heard of BufferedImage but and glanced at it and some tutorials.. but is this what i need?

    Anyone?

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

    Default

    bump bump bump

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

    Default

    Quote Originally Posted by AcousticBruce View Post
    I have heard of BufferedImage but and glanced at it and some tutorials.. but is this what i need?

    Anyone?
    What happened when you tried it?

    db

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

    Default

    Quote Originally Posted by AcousticBruce View Post
    Is this graphic stuff in the wrong forum? Is there a better place to ask graphics and graphics 2D questions?
    Java 2D - Java Forums

    db

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

    Default

    What happened when you tried it?

    db
    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?

    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();
        }
    }

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

    Default

    So how can i copy from another Graphics object without displaying it? Or if someone can help me think of this in a different way?

Similar Threads

  1. Replies: 1
    Last Post: 11-23-2010, 06:26 PM
  2. Invalidatig session when URL is copied on new browser window
    By maverick_friend in forum Java Servlet
    Replies: 0
    Last Post: 03-13-2010, 09:12 AM
  3. Replies: 5
    Last Post: 04-25-2009, 08:00 PM
  4. intimating file(s) have reached/copied in directory
    By ashu261 in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-23-2008, 06:24 PM
  5. how can objects themselves be copied???
    By ishakteyran in forum New To Java
    Replies: 1
    Last Post: 12-29-2007, 10: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
  •