Courses/CS 203/Fall 2005

From CSWiki

Jump to: navigation, search

Does anybody remeber where the sample print file for project 5 is? The one that actually sends the print job to the printer.

Thanks

[edit] ===================================================

This is code i found on the internet. All you have to do is call the static method PrintUtilities.printComponent(component). A component can be anything from a JFrame to a textfield, or JPanel. It must also be visible.

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

public class PrintUtilities implements Printable {
	private Component componentToBePrinted;

	public static boolean printComponent(Component c) {
		return new PrintUtilities(c).print();
	}

	public PrintUtilities(Component componentToBePrinted) {
		this.componentToBePrinted = componentToBePrinted;
	}

	public boolean print() {
		PrinterJob printJob = PrinterJob.getPrinterJob();
		printJob.setPrintable(this);
		if (printJob.printDialog()){
			try {
				System.out.println("Calling PrintJob.print()");
				printJob.print();
				System.out.println("End PrintJob.print()");
				
			} catch (PrinterException pe) {
				System.out.println("Error printing: " + pe);
			}
			return true;
			}else{
				return false;
			}
	}

	public int print(Graphics g, PageFormat pf, int pageIndex) {
		int response = NO_SUCH_PAGE;
		Graphics2D g2 = (Graphics2D) g;
		// for faster printing, turn off double buffering
		disableDoubleBuffering(componentToBePrinted);
		Dimension d = componentToBePrinted.getSize(); //get size of document
		double panelWidth = d.width; //width in pixels
		double panelHeight = d.height; //height in pixels
		double pageHeight = pf.getImageableHeight(); //height of printer page
		double pageWidth = pf.getImageableWidth(); //width of printer page
		double scale = pageWidth / panelWidth;
		int totalNumPages = (int) Math.ceil(scale * panelHeight / pageHeight);
		// make sure not print empty pages
		if (pageIndex >= totalNumPages) {
			response = NO_SUCH_PAGE;
		} else {
			// shift Graphic to line up with beginning of print-imageable region
			g2.translate(pf.getImageableX(), pf.getImageableY());
			// shift Graphic to line up with beginning of next page to print
			g2.translate(0f, -pageIndex * pageHeight);
			// scale the page so the width fits...
			g2.scale(scale, scale);
			componentToBePrinted.paint(g2); //repaint the page for printing
			enableDoubleBuffering(componentToBePrinted);
			response = Printable.PAGE_EXISTS;
		}
		return response;
	}

	public static void disableDoubleBuffering(Component c) {
		RepaintManager currentManager = RepaintManager.currentManager(c);
		currentManager.setDoubleBufferingEnabled(false);
	}

	public static void enableDoubleBuffering(Component c) {
		RepaintManager currentManager = RepaintManager.currentManager(c);
		currentManager.setDoubleBufferingEnabled(true);
	}
}

Iam having trouble trying to write the information from the 3 fields in my infix to postfix program (first field = infix expression, second field = postfix expression, third field = evalutation value). I use the method bw.write() in my GUI class but it doesnt write any information into the file "result.txt". I have little knowledge of how to properly use the method for BufferedWriter and the Internet isnt helping me. Does Anyone have any tips??? DATE: 10/31/05


I don't know how to help you because you didn't give enough information about what you wanted to know. The example of how to use BufferedWriter on Mr. Chen's web site should be enough to deal with this project. Anyway, if you would like to see more examples, you can go to this web site http://www.java2s.com/ Hope this help.

[edit] ==================================================

Does anyone knows anything about background picture in JFrame?
Nelson has a good suggestion about background image. Look at his contribution here:

===================================================
import java.awt.event.*; 
import javax.swing.*; 
import java.awt.*; 

public class BackgroundLayered extends JFrame 
{ 
  public BackgroundLayered() 
  { 
    ImageIcon image = new ImageIcon("pic.jpg"); 
    JLabel background = new JLabel(image); 
    background.setBounds(0, 0, image.getIconWidth(), image.getIconHeight()); 
    getLayeredPane().add(background, new Integer(Integer.MIN_VALUE)); 
//
    JPanel panel = new JPanel(); 
    panel.setOpaque(false); 
    panel.add( new JButton( "Hello" ) ); 
    setContentPane( panel ); 
    } 

    public static void main(String [] args) 
    { 
      BackgroundLayered frame = new BackgroundLayered(); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setSize(300, 300); 
      frame.setVisible(true); 
    } 
}



Another way to accomplish this is to create a special JPanel,
 which paints a picture in its background:  

=============================================================
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class TestBackground extends JFrame
{

 public TestBackground()
 {
 JPanel panel = new JPanel(){
 public void paintComponent(Graphics g)
 {
 //replace this with your own picture
 ImageIcon img = new ImageIcon("pic.jpg");  
 g.drawImage(img.getImage(), 0, 0, null);
 super.paintComponent(g);
 }
 };

 panel.setOpaque(false);
 JPanel newPanel = new JPanel();
 newPanel.add(new JButton("new Button"));
 newPanel.add(new JButton("second button"));
 //panel.add( new JButton( "Hello" ) );
 panel.add(newPanel);
 setContentPane( panel );
 }

 public static void main(String [] args)
 {
 TestBackground frame = new TestBackground();
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(300, 300);
 frame.setVisible(true);
 }
}
======================================================
====================================

======================================================
====================================
<br>I Still cant figure out how to make the picture I
chose my background. When implement your first class it 
works, but when i try to apply it to my GUI class it doesnt 
work.

========================================================
================================================
Can you post your GUI code so we can see whats going on?

Personal tools