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......*^^*
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you show me some lines in your code?
ASKER
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.lightG ray);
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).equ als("Print ...")){
PrintJob pjob = GraphFrame1.getToolkit().g etPrintJob (GraphFram e1, "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).equ als("Quit" )){
hide();
removeAll();
dispose();
}
else if (((String)event.arg).equal s("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)(100 0.*y[i]/x[ i])/1000.) ;
dB[i]=(double)((int)(1000. *
20*Math.log(gain[i])/Math. log(10))/1 000.);
listGain.addItem(""+dB[i]) ;
}
listIp.addItem(""+x[i]);
listOp.addItem(""+y[i]);
}
}
}
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.lightG
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/
add();
graph = new GraphDisplay(g,x,y,indexes
graph.setBackground(Color.
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).equ
PrintJob pjob = GraphFrame1.getToolkit().g
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).equ
hide();
removeAll();
dispose();
}
else if (((String)event.arg).equal
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)(100
dB[i]=(double)((int)(1000.
20*Math.log(gain[i])/Math.
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().getPrint Job(GraphF rame1, "Printing Test", null);
line 75:
this.printAll(pg);
Hope this works...
You can't use Class name to access an instance method.
Jerson
line 70:
this.getToolkit().getPrint
line 75:
this.printAll(pg);
Hope this works...
You can't use Class name to access an instance method.
Jerson
ASKER
hello!
i still got few problem.....there is no compile error but runtime error....
following r some of the error..
at java.security.AccessContro lContext.c heckPermis sion(Compi led Code)
java.lang.securityManager. checkPrint JobAccess( securityMa nager.java :1290)
sun.awt.windows.WTookit.ge tPrintJob( Wtookit.ja va:)
graphFrame1.handleEvent
java.awt.window.postEvent( Compiled Code)
and so on.....
thanks for reply.....
i still got few problem.....there is no compile error but runtime error....
following r some of the error..
at java.security.AccessContro
java.lang.securityManager.
sun.awt.windows.WTookit.ge
graphFrame1.handleEvent
java.awt.window.postEvent(
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.AllPermissio n;
};
and save it as mypolicy
and run your program like this
java -Djava.security.policy=myp olicy YourProgram
or you can add these line in your main() method.
Properties props = System.getProperties();
props.setProperty("java.se curity.pol icy", "mypolicy");
System.setProperties(props );
Don't forget to import java.util.*
Hope this helps.
Jerson
around, try this. Maybe it will work.
These are the steps:
Define a policy file which contains:
grant {
permission java.security.AllPermissio
};
and save it as mypolicy
and run your program like this
java -Djava.security.policy=myp
or you can add these line in your main() method.
Properties props = System.getProperties();
props.setProperty("java.se
System.setProperties(props
Don't forget to import java.util.*
Hope this helps.
Jerson
ASKER
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....
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
check www.verisign.com
Jerson
ASKER
thnx......
hlb
hlb
:-)
ASKER
i find few error
70:can't make static reference to method java.awt.toolkit.getToolki
70:undentify variable: Graphframe1
75:can't make static reference to method void printAll(java.awt.Graphics
thnx for reply
hlb