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

    Default I would like to print this JPanel class. How?

    I thought that by transfering the Graphics g with paintThis that it would work.

    I just want to print the PrintThis Jpanel class. What am I doing wrong? The print screen opens up but when I confirm i get a nullpointerexception.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.print.*;
    
    public class PrintTest implements Printable {
    
    
    	PrintThisGraphic printThisGraphic = new PrintThisGraphic();
    	public Graphics paintThis;
    	public int stringGap = 25, fretGap = 25, frets = 2;
    
        public static void main(String args[]) {
            new PrintTest();
        }
    
    	PrintTest() {
    		PrinterJob job = PrinterJob.getPrinterJob();
            job.setPrintable(this);
            boolean ok = job.printDialog();
            if (ok) {
                try {
                     job.print();
                } catch (PrinterException ex) {
                }
             }
    	}
    
        public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
    
            if (page > 0) {
                return NO_SUCH_PAGE;
            }
    		g = paintThis;
    
            Graphics2D g2d = (Graphics2D)g;
            g2d.translate(pf.getImageableX(), pf.getImageableY());
    
            return PAGE_EXISTS;
        }
    
        //******** I want this to print.
        class PrintThisGraphic extends JPanel {
    
    		public void paintComponent(Graphics g) {
    
    			g.setColor(Color.BLACK);
    			for(int string = 0; string < 6; string++) {
    				for(int fret = 0; fret <= frets; fret++) {
    					//Strings
    					g.drawLine(stringGap+(stringGap*(string)), fretGap, stringGap+(stringGap*(string)), fretGap+(fretGap*(frets)));
    					//Frets
    					g.drawLine(stringGap-2, (fretGap) + fretGap * fret, stringGap*6+2, (fretGap) + fretGap * fret);
    
    				}
                            // paintThis = g; was moved out of the for loop... ridiculous mistake! Also renamed it to something clearer. 
    			}
    
    		paintThis= g;
    
    		}
    	}
    }
    Last edited by AcousticBruce; 02-26-2011 at 05:49 PM.

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

    Default

    bumb bump bump

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

    Default

    Your Graphics/Graphics2D object is null, but that's not surprising as you're getting it (in a nested for loop?) from within a paintComponent method of a JComponent. Those Graphics objects aren't meant to persist.

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

    Default

    Quote Originally Posted by Fubarable View Post
    Your Graphics/Graphics2D object is null, but that's not surprising as you're getting it (in a nested for loop?) from within a paintComponent method of a JComponent. Those Graphics objects aren't meant to persist.

    Your Graphics/Graphics2D object is null
    The NullPinterException is because of
    Java Code:
    paintThis  = g;

    (in a nested for loop?) from within a paintComponent method of a JComponent. Those Graphics objects aren't meant to persist.
    Are you saying do not use nested for loops in a paintComponent?



    btw: I can get it it print if I move this in the print() method.

    Java Code:
    g.setColor(Color.BLACK);
    			for(int string = 0; string < 6; string++) {
    				for(int fret = 0; fret <= frets; fret++) {
    					//Strings
    					g.drawLine(stringGap+(stringGap*(string)), fretGap, stringGap+(stringGap*(string)), fretGap+(fretGap*(frets)));
    					//Frets
    					g.drawLine(stringGap-2, (fretGap) + fretGap * fret, stringGap*6+2, (fretGap) + fretGap * fret);
    
    					paintThis = g;
    				}
    			}

    So how can I copy Graphics objects that are drawn on screen to a new object to recall in a print() method?

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

    Default

    Quote Originally Posted by AcousticBruce View Post
    Are you saying do not use nested for loops in a paintComponent?

    No, but it makes no sense to try to extract a Graphics object from inside that method, and even if theoretically you could do it, why do it inside of nested for loops, where you'll be extracting the object a new time with every loop?


    So how can I copy Graphics objects that are drawn on screen to a new object to recall in a print() method?
    I'm no print expert, but you're going to get a viable Graphics object passed into the Printable's print method:

    Java Code:
       public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
    Why not use this object since it's not null, and is the one used for printing?

    perhaps call print(Graphics g) on your GUI object after it is rendered, passing into the print method the Graphics object above? You'll need to have an instance of your GUI visible in the class.

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

    Default

    Oh haha.. yeah i tried to make a quick SSCCE and put that in the for loop.. didnt even notice. i am changing the main post.. cause that is a blatent error.

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

    Default

    For instance, something like this (my comments and changes are denoted by // !!):

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.print.*;
    
    public class PrintTest2 implements Printable {
    
       // !! public Graphics paintThis;
       private PrintThis printThis = new PrintThis(); // !!
    
       public static void main(String args[]) {
          new PrintTest2();
       }
    
       PrintTest2() {
          // !! new PrintThis();
          // !! need a way to render the JPanel, so put in JFrame
          JFrame frame = new JFrame();
          printThis.setPreferredSize(new Dimension(600, 600));
          frame.getContentPane().add(printThis);
          frame.pack(); // !! and render it by calling pack().
          // !!
    
          PrinterJob job = PrinterJob.getPrinterJob();
          job.setPrintable(this);
          boolean ok = job.printDialog();
          if (ok) {
             try {
                job.print();
             } catch (PrinterException ex) {
             }
          }
       }
    
       public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
    
          if (page > 0) {
             return NO_SUCH_PAGE;
          }
          // !! g = paintThis;
    
          Graphics2D g2d = (Graphics2D) g;
          // !! System.out.printf("g2d null: %b;  pf null: %b" , g2d == null, pf ==
          // null);
          g2d.translate(pf.getImageableX(), pf.getImageableY());
    
          // !! call print on the JComponent, passing in the Graphics object given to this method
          printThis.print(g); // !!
    
          return PAGE_EXISTS;
       }
    
       class PrintThis extends JPanel {
    
          public int stringGap = 25, fretGap = 25, frets = 2;
    
          public void paintComponent(Graphics g) {
    
             g.setColor(Color.BLACK);
             for (int string = 0; string < 6; string++) {
                for (int fret = 0; fret <= frets; fret++) {
                   // Strings
                   g.drawLine(stringGap + (stringGap * (string)), fretGap, stringGap
                            + (stringGap * (string)), fretGap + (fretGap * (frets)));
                   // Frets
                   g.drawLine(stringGap - 2, (fretGap) + fretGap * fret, stringGap * 6 + 2, (fretGap)
                            + fretGap * fret);
    
                   // !! paintThis = g;
                }
             }
          }
       }
    }

    but again, please note that I know next to nothing about printing in Java, so I'm just going by what seems like common sense. I will need to review the tutorials.

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

    Default

    I didnt realize you can call a printThis.print(g). I still dont understand, I will look over the API in more detail.

    I also didnt realize you needed a frame. The reason i didnt think you need a frame is because you can build the graphics right in the print method.

    Well your code does work. I will study it.



    I think its time to start studying Bruce Eckels book :)

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

    Default

    Fubarable thank you so much. the print() method in a JComponent is new to me. This is the method that I needed to know about which I wasnt finding in the print tutorials.


    from API
    print(Graphics g) : Invoke this method to print the component to the specified Graphics.
    So happy now!

Similar Threads

  1. Print to file from GUI using second class
    By iamjonsmith in forum New To Java
    Replies: 29
    Last Post: 02-18-2011, 04:06 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. Replies: 3
    Last Post: 03-19-2009, 05:00 PM
  5. system-print(ln) on jtextarea or jpanel
    By Tamu in forum Advanced Java
    Replies: 1
    Last Post: 11-25-2008, 05:18 PM

Posting Permissions

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