Pondy Prem

Greenhorn
+ Follow
since Oct 28, 2001
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Pondy Prem

I copied this source from somwhre in net and finetuned...
It may be of use :
public class PrintPreview extends JDialog{
protected int m_wPage;
protected int m_hPage;

protected Printable m_target;
protected JComboBox m_cbScale;
protected PreviewContainer m_preview;

//protected PrintUtilities the_PrintUtilities;

public PrintPreview(JDialog parentJDialog, Printable target){
this(parentJDialog, target, "Print Preview");
}// C1 ends

public PrintPreview(JDialog parentJDialog,Printable target, String title){
super(parentJDialog, title);
setSize(800,600);
setModal(true);
this.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);

JToolBar tb = new JToolBar();
JButton bt = new JButton("Print", new ImageIcon("print.gif"));

ActionListener lst = new ActionListener(){
public void actionPerformed(ActionEvent e){
}
};

bt.addActionListener(lst);
bt.setAlignmentY(0.5f);
bt.setMargin(new Insets(4,6,4,6));
//tb.add(bt);

bt = new JButton("Close");
lst = new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
};

bt.addActionListener(lst);
bt.setAlignmentY(0.5f);
bt.setMargin(new Insets(2,6,2,6));
tb.add(bt);

String [] scales = {"10", "25", "50", "100" };

m_cbScale = new JComboBox(scales);

lst = new ActionListener(){
public void actionPerformed(ActionEvent e){
Thread runner = new Thread(){
public void run(){
String str = m_cbScale.getSelectedItem().toString();
System.out.println("m_cbScale: " + str);
if(str.endsWith("%")){
str.substring(0, str.length()-1);
}
str.trim();
int scale = 0;

try{
scale = Integer.parseInt(str);
}
catch(NumberFormatException ex){
System.out.println("scale: " + scale);
return;
}

int w = (int) (m_wPage*scale/100);
int h = (int) (m_hPage*scale/100);

Component [] comps = m_preview.getComponents();

for(int k=0; k<comps.length; k++){
if(!( comps[k] instanceof PagePreview)) {
continue;
}

PagePreview pp = (PagePreview) comps[k];
pp.setScaledSize(w,h);
}// for() ends
m_preview.doLayout();
m_preview.getParent().getParent().validate();

}// run() ends
};// Thread ends

runner.start();
}
};// ActionListener ends

m_cbScale.addActionListener(lst);
m_cbScale.setMaximumSize(m_cbScale.getPreferredSize());
m_cbScale.setEditable(true);
tb.addSeparator();
tb.add(m_cbScale);

getContentPane().add(tb, BorderLayout.NORTH);

m_preview = new PreviewContainer();

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

if(pageFormat.getHeight() ==0 ||pageFormat.getWidth() ==0 ){
//error
return ;
}
m_wPage = (int)(pageFormat.getWidth());
m_hPage = (int)(pageFormat.getHeight());

int scale = 50;

int w = (int)(m_wPage*scale/100);
int h = (int)(m_hPage*scale/100);

int pageIndex = 0;

try{
while(true){
BufferedImage img = new BufferedImage(m_wPage,m_hPage, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0,m_wPage, m_hPage);
if( target.print(g, pageFormat, pageIndex) != Printable.PAGE_EXISTS){
break;
}
PagePreview pp = new PagePreview(w,h, img);
m_preview.add(pp);
pageIndex++;

}
}catch(PrinterException pe){
pe.printStackTrace();
}

JScrollPane ps = new JScrollPane(m_preview);
getContentPane().add(ps, BorderLayout.CENTER);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
}// C2 ends



}// class PrintPreview ends
class PreviewContainer extends JPanel{
protected int H_GAP = 16;
protected int V_GAP = 10;

public Dimension getPreferredSize(){
int n = getComponentCount();

if(n == 0){ return new Dimension(H_GAP, V_GAP);}
Component comp = getComponent(0);
Dimension dc = comp.getPreferredSize();

int w = dc.width;
int h = dc.height;

Dimension dp = getParent().getSize();
int nCol = Math.max( (dp.width-H_GAP)/(w+H_GAP) , 1 );
int nRow = n/nCol;

if(nRow*nCol < n) nRow++;

int ww = nCol*(w+H_GAP) + H_GAP ;
int hh = nRow*(h+V_GAP) + V_GAP ;

Insets ins = getInsets();
return new Dimension(ww+ins.left+ins.right,
hh+ ins.top+ins.bottom);
}// getPreferredSize()ends

public Dimension getMaximumSize(){
return getPreferredSize();
}
public Dimension getMinimumSize(){
return getPreferredSize();
}

public void doLayout(){
Insets ins = getInsets();

int x = ins.left + H_GAP;
int y = ins.top + V_GAP;

int n = getComponentCount();

if(n==0) return;

Component comp = getComponent(0);
Dimension dc = comp.getPreferredSize();
int w = dc.width;
int h = dc.height;

Dimension dp = getParent().getSize();
int nCol = Math.max( (dp.width-H_GAP)/(w+V_GAP) ,1 );
int nRow = n/nCol;

if(nRow*nCol < n) nRow++;

int index = 0;
for(int k=0; k<nRow; k++){
for(int m=0; m<nCol; m++){
if(index >=n ) return;
comp = getComponent(index++);
comp.setBounds(x,y,w,h);
x += w+ H_GAP;
}//for(m.) ends
y += h+V_GAP;
x = ins.left + H_GAP;
}// for(k) ends

}// doLayout() ends

}// class PreviewContainer ends
class PagePreview extends JPanel{
protected int m_w;
protected int m_h;
protected Image m_source;
protected Image m_img;

public PagePreview(int w, int h, Image source){
m_w = w;
m_h = h;

m_source = source;
m_img = m_source.getScaledInstance(m_w, m_h, Image.SCALE_SMOOTH);
m_img.flush();
setBackground(Color.white);
setBorder(new MatteBorder(1,1,2,2,Color.black));
}//PagePreview ends

public void setScaledSize(int w , int h){
m_w = w;
m_h = h;
m_img = m_source.getScaledInstance(m_w, m_h, Image.SCALE_SMOOTH);
repaint();
}//void setScaledSize() ends

public Dimension getPreferredSize(){
Insets ins = getInsets();
return new Dimension(m_w+ins.left+ins.right,
m_h+ins.top + ins.bottom);
}

public Dimension getMaximumSize(){
return getPreferredSize();
}
public Dimension getMinimumSize(){
return getPreferredSize();
}

public void paint(Graphics g){
g.setColor(getBackground());
g.fillRect(0,0, getWidth(), getHeight());
g.drawImage(m_img, 0, 0, this);
paintBorder(g);
}
}//class PagePreview ends
************************ useage **************
public void showPreview(JDialog parentJDialog){
new PrintPreview(parentJDialog, PrintUtilities.this, "MyPage- preview") ;
}
22 years ago
This one may be useful:

import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
/** A simple utility class that lets you very simply print
* an arbitrary component. Just pass the component to the
* PrintUtilities.printComponent. The component you want to
* print doesn't need a print method and doesn't have to
* implement any interface or do anything special at all.
* <P>
* If you are going to be printing many times, it is marginally more
* efficient to first do the following:
* <PRE>
* PrintUtilities printHelper = new PrintUtilities(theComponent);
* </PRE>
* then later do printHelper.print(). But this is a very tiny
* difference, so in most cases just do the simpler
* PrintUtilities.printComponent(componentToBePrinted).
*
* 7/99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
* May be freely used or adapted.
*/
public class PrintUtilities implements Printable {
private Component componentToBePrinted;
public static void printComponent(Component c) {
new PrintUtilities(c).print();
}

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

public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog())
try {
printJob.print();
} catch(PrinterException pe) {
System.out.println("Error printing: " + pe);
}
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) {
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
disableDoubleBuffering(componentToBePrinted);
componentToBePrinted.paint(g2d);
enableDoubleBuffering(componentToBePrinted);
return(PAGE_EXISTS);
}
}
/** The speed and quality of printing suffers dramatically if
* any of the containers have double buffering turned on.
* So this turns if off globally.
* @see enableDoubleBuffering
*/
public static void disableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
}
/** Re-enables double buffering globally. */

public static void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
}
}
22 years ago
For all the manipulations that you are mentioning you have to do the page length/width calculations..
Also regarding the size of image... try adding the images in their own JPanel .. before placing it on the global JPanel.
22 years ago
If one of your text-entries in your list-box is longer than the list-box-width.. then u'd see the horizontal scrollbar...
Try increasing the width of listbox.. or reducing the length of longest string in listbox.
Thanks
22 years ago
If you say "I don't want user to use". Then you must use setEnabled(false).
However, if you are talking about achieving your own button behaviour for enabling/disabling.. then you may have to create your own implementation for View&Controller classes.
Thx
[email protected]
22 years ago
Hey Guys ...
Thats' nice. I would be interested in joining the Patterns Club!
Thanks,
Premkumar.N
Sr Programmer Analyst (Bioinformatics )
Malleshwaram,
Bangalore, India.
Ph: 3460115
------------------
[email protected]
Would you paste the Practice Test URL.
Thanks,
Prem

Yes,as you said, singletons are mainly to restrict the number of instance to 0..1 at any given time. Where more than one instance does not make any sense.
Generally they are entity classes providing static or common information to other classes in the system.
No Math & System are not examples of Singletons.
All the methods in those classes are static methods.
Not even a single instance can be made of those classes (the constructors are private).
Math:
- all variables(two)and all the methods are static
public static final double E = 2.7182818284590452354;
public static final double PI = 3.14159265358979323846;
- an empty private constructor
System:
- all variables and all the methods are static
- an empty private constructor
Thanks,
Prem
------------------
[email protected]
Dear David Kane:
Welcome !
I am desigining a wizard that has multiple execution-paths / branching-paths.I wish to know whether any design pattern exists for Wizard creation and control related issues.
and specifically about:
1. What are the collaborating classes to be used & relationship?
2. Who should create the wizard frame ?
3. Who should listen for "Back" "Next" "Cancel" Buttons ?
4. Who should decide what is the next/prev panel to show
5. Who should track the current state of the wizard-process ?
Looking fwd for your suggestions.
Thank you,
Prem

Thank you monofremix for the info.
I would like to know how to manage the Panels ... in the case where there are multiple-branching-paths based on the options that the user chooses at intermediate screens.
.. and everyscreen need to save/retrieve data from database too (i.e: dynamic ).
I would be glad if you/someone could enlighten me about the responsibility allocation.
Specifically which class will decide about the screen-to-show-next.
Thank you
Prem
------------------
[email protected]
I All:
I would like to know whether any design pattern exists for Wizard creation and control

1. What are the collaborating classes ?
2. Who should create the wizard frame ?
3. Who should listen for "Back" "Next" "Cancel" Buttons ?
4. Who should decide what is the next/prev panel to show
5. Who should track the current state of the wizard-process ?

Thank you,
Prem
------------------
[email protected]