Link to home
Start Free TrialLog in
Avatar of bhuey_ling
bhuey_ling

asked on

how to write print function for a frame

hello!

how to write print function for a frame to print contains in that frame....my frame have 3 list box, a canvas fucntion.....i would like to print it all out by clicking "print" function....

thnx for reply......*^^*
ASKER CERTIFIED SOLUTION
Avatar of jerch
jerch

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bhuey_ling
bhuey_ling

ASKER

hello.....

i find few error

70:can't make static reference to method java.awt.toolkit.getToolkit() in class java.awt.window.

70:undentify variable: Graphframe1

75:can't make static reference to method void printAll(java.awt.Graphics) in class java.awt.component

thnx for reply

hlb
Can you show me some lines in your code?




i'm sorry.......i forget to attach my program.........

here r my program( i may use not the latest code to write like for the awt)


import java.awt.*;                                                                                                                                                 import java.awt.*;
import java.applet.*;


public class GraphFrame1 extends Frame{
  double[] x,y;
  int indexes;
  boolean flags;
  List listGain;
  List listIp;
  List listOp;
  Graphics g;
  GraphDisplay graph;      
      
  public GraphFrame1(String title, boolean isapp){
    super(title);
    setBackground(Color.lightGray);
    init();
  }
      
  public void start(double[] inputX, double[] outputY, int index, boolean flag){
    x=inputX;
    y=outputY;
    indexes=index;
    flags=flag;
    init();            
    graph.repaint();
  }
            
  public void init(){
    MenuBar mBar = new MenuBar();
    this.setMenuBar(mBar);
   
    Menu file = new Menu("File");
    file.add(new MenuItem("Print..."));
    file.addSeparator();
    file.add(new MenuItem("Quit"));
    mBar.add(file);
    Menu help = new Menu("Help");
    help.add(new MenuItem("About"));
    mBar.add(help);
          
    listIp=new List(25,true);
    listOp=new List(25,true);
    listGain=new List(25,true);
    Panel p = new Panel();
    p.setLayout(new FlowLayout());
    p.add(listIp);
    p.add(listOp);
    p.add(listGain);
    listIp.addItem("Input(V):");
    listOp.addItem("Output(V):");
    listGain.addItem("Gain(Ip/Op)[dB]:");
    add();
                        
    graph = new GraphDisplay(g,x,y,indexes,flags);
    graph.setBackground(Color.black);
    setLayout(new BorderLayout());
   
    add("Center",graph);
    add("East", p);
    graph.repaint();
  }

  public boolean handleEvent(Event event){
    switch(event.id){
      case Event.ACTION_EVENT:
      if (event.target instanceof MenuItem){
            if(((String)event.arg).equals("Print...")){
              PrintJob pjob = GraphFrame1.getToolkit().getPrintJob(GraphFrame1, "Printing Test", null);

          if (pjob != null) {
            Graphics pg = pjob.getGraphics();
            if (pg != null) {
              GraphFrame1.printAll(pg);  // <-- use printall
              pg.dispose(); // flush page
            }
            pjob.end();
          }
        }
            if(((String)event.arg).equals("Quit")){
              hide();
          removeAll();
          dispose();
        }
            
            else if (((String)event.arg).equals("About")){
              InfoDialog d;
              d=new InfoDialog(this, "About Virtual LAb",
                    "This demo was written by hlBoey\n"+
                    "Copyright(c) 2000 UTM");
              d.show();
        }
      }
      break;
     
      case Event.WINDOW_DESTROY:
        hide();
        removeAll();
        dispose();
      break;
    }  
     return true;  
  }
        
  public void add(){
    double[] gain = new double[40];
    double[] dB=new double[40];
    int[] gain1 = new int[40];
    for(int i=0; i<indexes; i++){
      if(y[i]==0.)    listGain.addItem("      -");
      else{
        gain[i]=(double)((int)(1000.*y[i]/x[i])/1000.);
        dB[i]=(double)((int)(1000.*
         20*Math.log(gain[i])/Math.log(10))/1000.);
        listGain.addItem(""+dB[i]);
      }
      listIp.addItem(""+x[i]);
      listOp.addItem(""+y[i]);
    }
  }
}
Change GraphFrame1 to this in line 70 and 75.

line 70:
this.getToolkit().getPrintJob(GraphFrame1, "Printing Test", null);

line 75:
this.printAll(pg);

Hope this works...

You can't use Class name to access an instance method.
Jerson
hello!

i still got few problem.....there is no compile error but runtime error....

following r some of the error..

at java.security.AccessControlContext.checkPermission(Compiled Code)

java.lang.securityManager.checkPrintJobAccess(securityManager.java:1290)

sun.awt.windows.WTookit.getPrintJob(Wtookit.java:)

graphFrame1.handleEvent

java.awt.window.postEvent(Compiled Code)

and so on.....

thanks for reply.....
If you're developing an applet, you wouldn't be able to print. You need to have your applet signed (See www.verisign.com for more details).  And if you're developing an application, it suppose to work. I don't think there's a security restriction for this.  But for a work
around, try this. Maybe it will work.

These are the steps:

Define a policy file which contains:

grant {
permission java.security.AllPermission;
};

and save it as mypolicy

and run your program like this

java -Djava.security.policy=mypolicy YourProgram

or you can add these line in your main() method.

Properties props = System.getProperties();

props.setProperty("java.security.policy", "mypolicy");

System.setProperties(props);

Don't forget to import java.util.*

Hope this helps.

Jerson
hello..

my programme r an apllet..is that means i can use those latest code u give ...r u mean that then i have no other choice but have applet signned.....is it cost money ....actually wat applet signned means...is it....after signned it.....any user can use print function of the applet i develope or every user have to sign the applet in their own PC?

thnx for reply....
If you get your applet signed, some security restrictions will be removed such as reading the local file of the user, printing in the user's machine etc.  Yes, you have to pay certain amount for your applet to get signed.

check www.verisign.com

Jerson
thnx......
hlb
:-)