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

    Default How can I make the graphics visible?

    My purpose is to make a MainGraphicsPanel that holds the raw information that will be printed as well as displayed on screen. So I Made a GraphicsView class that copies from MainGraphicsPanel and displays the graphics on screen. The public int print() method will also copy from MainGraphicsPanel and translate and scale what it needs.

    I have the print method commented out because I am simply trying to display the GraphicsView class on to the MainPrintPanel.

    here is a SSCCE. The main idea right now is to get the graphics to display that is all.

    Fubarable you helped me quite a bit with the Component.print(graphics) method. Thank you again. Now I am trying to seperate classes so they can do their own thing.

    Again

    MainPrintPanel - class that holds the paintComponent I would like to display on screen.
    MainGraphicsReference - class that holds the graphics I would like to reference.

    As you can see in the code I commented out hugeButton JButton and when I add that instead... the button displays fine. But when I switch back to the Panel nothing displays.... perhaps I have been doing too much java?

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.print.*;
    
    public class PrintSSCCE extends JFrame {
    
    	MainPrintPanel mpp = new MainPrintPanel();
    	JPanel mainPanel = new JPanel();
    
        //Panel with Add and Print buttons
        JPanel buttonPanel = new JPanel();
        JButton addButton, printButton;
    
        PrintSSCCE() {
    
        	super("Printy Printerston");
        	setSize(1280, 700);
        	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        	//Button Panel
        	addButton = new JButton("Add");
        	printButton = new JButton("Print");
        	buttonPanel.add(addButton);
        	buttonPanel.add(printButton);
        	add(buttonPanel, BorderLayout.NORTH);
    
        	//Test Display
        	//JButton hugeButton = new JButton("YEAHHHH!!!");
        	//hugeButton.setPreferredSize(new Dimension(500, 500));
        	//mainPanel.add(hugeButton);
        	//
        	mainPanel.setPreferredSize(new Dimension(500, 500));
        	mainPanel.add(mpp);
        	add(mainPanel, BorderLayout.SOUTH);
    
        	setVisible(true);
    
        }
    
        // ************** MAIN PRINT PANEL ***********
        private class MainPrintPanel extends JPanel  {
    
    		MainGraphicsReference mgr = new MainGraphicsReference();
    		//PrinterJob job = PrinterJob.getPrinterJob();
    
    		MainPrintPanel() {
    
    			setPreferredSize(new Dimension(500, 500));
    			//JFrame frame = new JFrame();
    			//mgr.setPreferredSize(new Dimension(500, 500));
    			//frame.getContentPane().add(mgr);
    			//frame.setVisible(true);
    			//add(gv);
    		}
    
    		public void paintComponent(Graphics g) {
    			super.paintComponent(g);
    			mgr.print(g);
    			Graphics2D g2d = (Graphics2D) g;
    			g2d.translate(10, 10);
    			g2d.scale(.5, .5);
    		}
    
    		private class MainGraphicsReference extends JPanel {
    
    			public void paintComponent(Graphics g) {
    				super.paintComponent(g);
    
    				//Page Graphics
    				g.setColor(Color.WHITE);
    				g.fillRect(0, 0, 850, 1100);
    			}
    		}
    
    		/*public int Print(Graphics g, PageFormat pf, int page) {
    			if(page > 0) return NO_SUCH_PAGE;
    
    			this.print(g);
    
    			return PAGE_EXISTS;
    		}*/
        }
    
        public static void main(String[] args) {
    
        	new PrintSSCCE();
        }
    }

  2. #2
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    441
    Rep Power
    6

    Default

    Hi,

    I am not sure. However, I guess the problem is in your MainPrintPanel constructor.
    Mak
    (Living @ Virtual World)

  3. #3
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    441
    Rep Power
    6

    Default

    Use my below code .It s working fine.


    import javax.swing.*;
    import java.awt.*;
    import java.awt.print.*;

    public class PrintSSCCE extends JFrame {

    MainPrintPanel mpp = new MainPrintPanel();
    JPanel mainPanel = new JPanel();

    //Panel with Add and Print buttons
    JPanel buttonPanel = new JPanel();
    JButton addButton, printButton;

    PrintSSCCE() {

    super("Printy Printerston");
    setSize(1280, 700);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Button Panel
    addButton = new JButton("Add");
    printButton = new JButton("Print");
    buttonPanel.add(addButton);
    buttonPanel.add(printButton);
    add(buttonPanel, BorderLayout.NORTH);

    //Test Display
    //JButton hugeButton = new JButton("YEAHHHH!!!");
    //hugeButton.setPreferredSize(new Dimension(500, 500));
    //mainPanel.add(hugeButton);
    //
    add(mpp, BorderLayout.SOUTH);

    //mainPanel.setPreferredSize(new Dimension(500, 500));
    //mainPanel.add(mpp);
    //add(mainPanel, BorderLayout.SOUTH);

    setVisible(true);

    }

    // ************** MAIN PRINT PANEL ***********
    private class MainPrintPanel extends JPanel {

    MainGraphicsReference mgr = new MainGraphicsReference();
    //PrinterJob job = PrinterJob.getPrinterJob();

    MainPrintPanel() {

    setPreferredSize(new Dimension(500, 500));
    //JFrame frame = new JFrame();
    mgr.setPreferredSize(new Dimension(500, 500));
    //frame.getContentPane().add(mgr);

    add(mgr);
    setVisible(true);
    //paintComponent(new Graphics2D(10,20,30,10));
    }

    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    mgr.print(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(10, 10);
    g2d.scale(.5, .5);
    }

    private class MainGraphicsReference extends JPanel {

    public void paintComponent(Graphics g) {
    super.paintComponent(g);

    //Page Graphics
    g.setColor(Color.RED);
    g.fillRect(0, 0, 850, 1100);
    }
    }

    /*public int Print(Graphics g, PageFormat pf, int page) {
    if(page > 0) return NO_SUCH_PAGE;

    this.print(g);

    return PAGE_EXISTS;
    }*/
    }

    public static void main(String[] args) {

    new PrintSSCCE();
    }
    }
    Mak
    (Living @ Virtual World)

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

    Default

    Fubarable. I need you man :)

    I made a SSCCE and all!



    Quote Originally Posted by makpandian View Post
    Use my below code .It s working fine.
    I do not understand why you would add both
    Java Code:
    add(mgr);
    as well as... wouldnt that just be copying right over the paint component or vise versa?

    Java Code:
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    mgr.print(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(10, 10);
    g2d.scale(.5, .5);
    }

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

    Default

    I'm not 100% sure what you're trying to display with this code. Also, what is the print inside of the paintComponent supposed to do?

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

    Default

    I have been having an issue with already transformed and printed graphics to print properly. SO i decided to make multiple Components and take the raw info from one central location. Basically kinda like a parent class with children.

    So MainPrintPanel is a seperate class and I want its paintComponent to display on the screen you see pop up, right underneith the buttons. So the paintComponent pulls the Graphics object from MainGraphicsReference. MainGraphicsReference is the main source for the Graphics object. I am also going to add a printable interface that pulls from the same source.

    the print method in the paintComponent is from what you showed me the otherday in the API:
    void print(Graphics g)
    Invoke this method to print the component to the specified Graphics.

    I hope this clears up what I am doing.
    Last edited by AcousticBruce; 02-28-2011 at 09:17 PM.

  7. #7
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Maybe I'm missing something, but it looks like you forgot to add your MainPrintPanel to your JFrame's content pane (getContentPane.add()). The huge button is displayed just fine because the old AWT add() method in java.awt.Container (of which JFrame is a subclass) knows what to do with a button.

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

    Default

    Well my friend you are a genius. That is exactly the deal.

    I learned something about your post. Even though I knew it was a JPanel is a subcomponent, i didnt realise i needed get contentPane().

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

    Default

    thanks for taking the time on this NRiTH.

    A simple issue like that can be hard to spot sometimes.

  10. #10
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by AcousticBruce View Post
    Well my friend you are a genius. That is exactly the deal.

    I learned something about your post. Even though I knew it was a JPanel is a subcomponent, i didnt realise i needed get contentPane().
    That's Swing Rule #1, and easy to forget if you're just learning,or if you've been away from Swing for too long.

    Quote Originally Posted by AcousticBruce View Post
    thanks for taking the time on this NRiTH.

    A simple issue like that can be hard to spot sometimes.
    Believe me, I've been stumped even longer on simpler problems. It happens to all of us.

Similar Threads

  1. Drawing a graphics onto another Graphics ?
    By Ziden in forum Java Applets
    Replies: 0
    Last Post: 01-08-2011, 07:30 PM
  2. Replies: 3
    Last Post: 10-20-2010, 06:24 AM
  3. Replies: 2
    Last Post: 01-30-2010, 05:14 AM
  4. GUI is visible but content is not.
    By seemant.bisht in forum AWT / Swing
    Replies: 3
    Last Post: 10-07-2009, 06:28 PM
  5. Text are not visible in GUI
    By VinTiger in forum New To Java
    Replies: 5
    Last Post: 05-15-2009, 08:14 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
  •