Urgent print!

Hello,

I'm having some probelms with my code and i can seem to figure out with the problem is in line 82 of my code i'm suppose to give in a type. but ven if i do give in the type i would still get an error...

my codes are as below.

import javax.swing.*;

import java.awt.*;

import java.util.*;

import java.awt.event.*;

import javax.swing.event.*;

import java.awt.geom.*;

import java.awt.print.*;

class SketchView extends JComponent

implements Observer,

ActionListener,

Printable,

Pageable{

//Always two pages

public int getNumberOfPages()

{return 2;}

//Return the printable object that will render the page

public Printable getPrintable(int pageIndex)

{

if (pageIndex == 0)

return new SketchCoverPage(theApp);

else

return this;

}

public PageFormat getPageFormat(int pageIndex)

{

//get the default page format and its Paperobject.

PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage();

Paper paper = pageFormat.getPaper();

if(pageIndex == 0)//if its the cover page..

{//..make the margins twiece the size

double leftMargin = paper.getImagebleX()-paper.getImagebleWidth()-leftMargin;

double bottomMargin = paper.getImagebleY()-paper.getHeight()-paper.getImagebleHeight()-topMargin;

//Double the margins sizes

leftMargin *= 2;

rightMargin *= 2;

topMargin *= 2;

bottomMargin *= 2;

paper.setImageableArea(leftMargin, topMargin, //Set new printable area

paper.getWidth()-leftMargin-rightMargin,

paper.getHeight()-topMargin-bottomMargin);

paperFormat.setPaper(paper);//Restore the paper

}

else

{ //We are printing a skecth so decide on portrait ot landscape

Rectangle rect = getModelExtent();

//If the width is more than the height, set landscape

if(rect.width > rect.height)

pageFormat.setOrientation(pageFormat.LANDSCAPE);

}

return pageFormat;

}

public int print(Graphics g,PageFormat pageFormat, int pageIndex)throws PrinterException

{

if(pageIndex != 1)

return NO_SUCH_PAGE;

Graphics2D g2D = (Graphics2D)g;

Rectangle rect = getModelExtent();

//Calculate the scale to fit sketch to page

double scaleX = pageFormat.getImagebleWidth() /rect.width;

double scaleY = pageFormat.getImagebleHeight() /rect.height;

double scale = Math.min(scaleX,scaleY); //getminimum scale factor

//Move origin to page printing area corner

g2D.translate(pageFormat.getImagebleX(), pageFormat.getImagebleY());

g2D.scale(scale,scale);

g2D.translate(-rect.x, -rect.y);

print(g2D);

return PAGE_EXISTS;

}

if (name.equals(printAction.getValue(NAME)))

{

PrinterJob = PrinterJob.getPrinterJob();//get a printing object

printJob.setPageble(theApp.getView());//the view is the page source

if (printJob.printDialog());//display print dialog

{//if true is returned

try

{

printJob.print();

}

catch(PrinterException pe)

{

System.out.println(pe);

JOptionPane.showMessageDialog(SketchFrame.this,

"Error printing a sketch.",

"Printer Error",

JOptionPane.ERROR_MESSAGE);

}

}

}

}

Thanks!

Your help is greatly appreaciated!

-palv-

[3530 byte] By [palv] at [2007-9-26 1:24:35]
# 1

You have a piece of code outside of every method. Probably you mistyped some curly braces.

import javax.swing.*;

/* ... */

class SketchView extends JComponent

implements Observer,

ActionListener,

Printable,

Pageable{

/* ... */

public Printable getPrintable(int pageIndex)

{

/* ... */

}

public PageFormat getPageFormat(int pageIndex)

{

/* ... */

}

public int print(Graphics g,PageFormat pageFormat, int pageIndex)throws PrinterException

{

/* ... */

}

// *********************************************

// Hey, this if statement is not inside a method!

//

// *********************************************

if (name.equals(printAction.getValue(NAME)))

{

/* ... */

}

}

fabiocar at 2007-6-29 1:04:54 > top of Java-index,Archived Forums,Java Programming...