Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Printing with Java

Printing with Java
Thread Tools
Fresh-Faced Recruit
Join Date: Aug 2001
Location: Germany
Status: Offline
Reply With Quote
Dec 23, 2001, 07:55 PM
 
I've been looking for samplecode of printing from a java app and OS X but the Informations from Apple are not so easy to understand ;-))

Can anybody publish a sample like hello world?

Thanks Mark
     
Fresh-Faced Recruit
Join Date: May 2001
Location: India
Status: Offline
Reply With Quote
Dec 25, 2001, 10:23 PM
 
hi Mark,

Printing in java for mac OS X is not different from printing for windows OS. Here is the sample code which u can use. I've used similar code form printing in my java app running on Mac OS X.

good luck.

import java.awt.geom.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.print.PrinterJob;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;

public class ShapesPrint extends JPanel implements Printable, ActionListener
{
final static JButton button = new JButton("Print");

public ShapesPrint()
{
setBackground(Color.white);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() instanceof JButton)
{
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog())
{
try
{
printJob.print();
} catch (Exception ex)
{
ex.printStackTrace();
}
}
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawString("Hello World", 50,50);
}

public int print(Graphics g, PageFormat pf, int pi) throws PrinterException
{
if (pi >= 1)
{
return Printable.NO_SUCH_PAGE;
}
g.drawString("Hello world", 50,50);
return Printable.PAGE_EXISTS;
}

public static void main(String s[])
{
WindowListener l = new WindowAdapter()
{
public void windowClosing(WindowEvent e) {System.exit(0);}
public void windowClosed(WindowEvent e) {System.exit(0);}
};
JFrame f = new JFrame();
f.addWindowListener(l);
JPanel panel = new JPanel();
panel.add(button);
f.getContentPane().add(BorderLayout.SOUTH, panel);
f.getContentPane().add(BorderLayout.CENTER, new ShapesPrint());
f.setSize(200, 200);
f.show();
}
}
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:09 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2