I've written a simple test script, It has a jFrame containing two jPanels, one with control buttons and the other with some graphics. I cannot work out how to print the jPanel containing my graphical content. The best I have achieved so far is to print a grey square the same size as the jPanel, but not it's contents (at present the contents are just a couple of circles to test the script, but will ultimately be a company invoice).
Can anyone help me to get this to work please? Things are starting to get a little desperate.
Thanks in advance.
Can anyone help me to get this to work please? Things are starting to get a little desperate.
Code:
/*
* gPrint.java
*
* Created on 16 November 2005, 15:21
*/
import javax.print.*;
import javax.print.event.*;
import javax.print.attribute.*;
import java.awt.print.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*; // For JPanel, etc.
import java.awt.*; // For Graphics, etc.
import java.awt.geom.*; // For Ellipse2D, etc.
/**
*
* @author gary
*/
public class gPrint extends javax.swing.JFrame {
/** Creates new form gPrint */
public gPrint() {
initComponents();
}
private Rectangle2D.Double square, square2;
private Ellipse2D.Double circle;
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
control = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
content = new javax.swing.JPanel();
content.setBackground(Color.white);
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 or DIE!");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
control.add(jButton1);
jButton2.setText("Draw Me");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
control.add(jButton2);
getContentPane().add(control, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 633, 100));
content.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(255, 0, 0), new java.awt.Color(255, 51, 51), new java.awt.Color(0, 204, 0), new java.awt.Color(0, 255, 51)));
getContentPane().add(content, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 100, 630, 810));
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// draw a funky invoice frame :)
square = new Rectangle2D.Double(30, 30, 550, 550);
square2 = new Rectangle2D.Double(250, 250, 50, 50);
circle = new Ellipse2D.Double(80, 80, 350, 350);
paintComponent(content.getGraphics());
}
private void exiting(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new gPrint().show();
}
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(10));
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;
}
}
// Variables declaration - do not modify
private javax.swing.JPanel content;
private javax.swing.JPanel control;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
// End of variables declaration
}
Thanks in advance.