Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing jPanel prints the panel and the frame with a grey background

Status
Not open for further replies.

mes123

Programmer
Jul 29, 2005
62
0
0
GB
Ok, solved the original problem but now have a new one thats totally got me.

The story so far: A jFrame containing two jPanels. Panel one has a button labeled 'print'. Panel two has a nice picture of a square and a circle.

When the print button is pressed the printer springs to life and out pops a printed page.

Two problems.

The page has a double image on it. The jpanel with the picture is printed and over the top of this the whole jframe has been printed.

Why is this, since (see code below) the printerjob object has only had the jpanel set as it's printable?

The second problem is that the background is grey and not white.

Help!


Code:
public class ggtest extends javax.swing.JFrame {
    
    /** Creates new form  */
    public ggtest() {
        initComponents();
    }
 
    private void initComponents() {
        control = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        contentx = new TestCanvas();

        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                exiting(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        control.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
        jButton1.setText("Print");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        control.add(jButton1);
    
        getContentPane().add(control, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 410, 50));

        contentx.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
        getContentPane().add(contentx, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 50, 410, 400));

        pack();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        PrinterJob printjob = PrinterJob.getPrinterJob();
        printjob.setPrintable(contentx);
        try {
            printjob.print();
        } catch (Exception e){};

    }

    public static void main(String args[]) {
        new ggtest().show();
    }

    // Variables declaration 
    private TestCanvas contentx;
    private javax.swing.JPanel control;
    private javax.swing.JButton jButton1;
    // End of variables declaration
  
    class TestCanvas extends JPanel implements Printable {
        private Rectangle2D.Double square, square2;
        private Ellipse2D.Double circle;
        
        public TestCanvas (){
            super();
            this.setBackground(Color.white);
        }
        
        public void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D)g;
            square =  new Rectangle2D.Double(5,5, 390, 390);
            square2 =  new Rectangle2D.Double(177, 177, 50, 50);
            circle =  new Ellipse2D.Double(5, 5, 390, 390);
            
            g2d.setStroke(new BasicStroke(4));
            g2d.draw(square2);
            g2d.draw(square);
            g2d.draw(circle);
            
        }
        
        public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
            
            int x = (int)pageFormat.getImageableX();
            int y = (int)pageFormat.getImageableY();
            Graphics2D g2d = (Graphics2D)g;
            g2d.translate(x, y);
            if (pageIndex == 0) {
                paint(g2d);
                return Printable.PAGE_EXISTS;
            } else {
                return Printable.NO_SUCH_PAGE;
            }
        }
        
    }

    
}

 
we now have a white background :)
getContentPane.setBAckground(Color.white);

But I still get the two images on top of each other.
 
the solutions were as follows:

The grey background. Although the jpanel has its background set to white, the contentpane was still grey so
Code:
getContentPane().setBackground(Color.white);
solved the problem

Fram printing over the jpanel:
This was because my extended jPanel had a method paintComponent. When I renamed this method to paint the problem dissappeared. Not entiry sure why but it worked!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top