dcsimg
how do i print from applet
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   pranesh_sharma
Posted On:   Saturday, August 10, 2002 04:35 AM

i m attaching my code, pls help me out,it is repeating the process of printing... import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; import java.awt.print.*; public class PrintLabel extends Applet implements Printable{ boolean isStandalone = false; BorderLayout borderLayout1 = new BorderLayout(); Panel panel1 = new Panel(); JButton print = new JButton(); JPanel jPanel1 = new JPanel(); JScrollPane jScrollPane1 = new JScrollPane(); BorderLayout borderLayout2 = new BorderLayout(); PageFormat pageFormat; /**Get a parameter value*/ public String getParameter(String key, String def   More>>

i m attaching my code, pls help me out,it is repeating the process of printing...

			

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

public class PrintLabel extends Applet implements Printable{
boolean isStandalone = false;
BorderLayout borderLayout1 = new BorderLayout();
Panel panel1 = new Panel();
JButton print = new JButton();
JPanel jPanel1 = new JPanel();
JScrollPane jScrollPane1 = new JScrollPane();
BorderLayout borderLayout2 = new BorderLayout();
PageFormat pageFormat;
/**Get a parameter value*/
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}

/**Construct the applet*/
public PrintLabel() {
}
/**Initialize the applet*/
public void init() {
try {
arrLength = Integer.parseInt(this.getParameter("arrLength",""));
}
catch(Exception e) {
e.printStackTrace();
arrLength = 0;
}
if(arrLength != 0) {
arr = new String[arrLength];
}
if(arr != null) {
for (int ii = 0; ii < arrLength; ii++)
{
arr[ii] = this.getParameter("name_"+ii,"");
}
}
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
this.setLayout(borderLayout1);
print.setText("Print");
print.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
print_actionPerformed(e);
}
});
jPanel1.setLayout(borderLayout2);
this.add(panel1, BorderLayout.NORTH);
panel1.add(print, null);
this.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jScrollPane1, BorderLayout.CENTER);

printerjob = PrinterJob.getPrinterJob();
pageFormat = printerjob.defaultPage();
Paper paper = pageFormat.getPaper();
int k = 0;
int j = 15;
int counter = 0;
for (int ii = 0; ii < arrLength; ii++) {
(j+=15);
counter++;
if (counter == 5) {
counter=0;
j+=50;
}
}
j+=50;
paper.setImageableArea(10,10, 288, j);
pageFormat.setPaper(paper);
printerjob.setPrintable(this, pageFormat);
}

int arrLength = 0;
String arr[];
/**Start the applet*/
public void start() {
}

public void print(){
if(printerjob.printDialog()) {
try {
System.out.println("Start Printing....");
printerjob.print();
System.out.println("...Printed");
}
catch(Exception exception) {
System.out.println("Error printing: " + exception);
}
}
}

public int print(Graphics g, PageFormat pageformat, int i) {
Graphics2D graphics2d = (Graphics2D)g;
int j = 15;
int counter=0;
if(arr != null) {
for (int ii = 0; ii < arrLength; ii++) {
graphics2d.drawString(arr[ii],100,(j+=15));
System.out.println("arr["+ii+"] :- "+arr[ii]);
counter++;
if (counter == 5) {
counter=0;
j+=50;
}
}
}
return 0;
}

void print_actionPerformed(ActionEvent e) {
Graphics gg = getGraphics();
print(gg,pageFormat,1);
print();
}
PrinterJob printerjob;
}


   <<Less
About | Sitemap | Contact