Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 12
  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2004
    Messaggi
    7

    [Java] Anteprima di stampa

    Ciao a tutti
    uso jdk 1.3.1 e ho sviluppato una classe che esegue la stampa (usando java.awt.print) di alcune tabelle e si occupa anche della loro formattazione. Quello che voglio aggiungere alla classe è la possibilità di visualizzare un' anteprima di stampa prima di mandare il tutto alla stampante ma non ho molto chiaro il come...
    Spero di esser stato chiaro nell' esporre il problema!

    Qualsiasi suggerimento è (molto) ben accetto!
    Grazie in anticipo!

  2. #2

  3. #3
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Guarda qui:

    http://forum.java.sun.com/thread.jsp...hreadID=601884

    LEggi tutti i post nella discussione perché ci sono un paio di oggetti da sistemare, in quanto faceva parte dell'applicazione di sto signore... comunque mi pare che dia le dritte necessarie per sistemare il tutto... non l'ho testata.
    Saluti.
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  4. #4
    Penso sia la soluzione adatta vedendo le risposte entusiaste.. però non riesco a capire questo passaggio..

    "Woops, this class if a J Internal Frame and in my program my
    desktop was MercPlot. Just change that to whatever your desktop
    name is - or just make this class J Frame and then delete that
    desktop.add line."

    Cosa intende con Desktop?

  5. #5
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    probabilmente per desktop intendeva la finestra principale dell'applicazione (ossia il classico JFrame, suppongo). Credo che il tizio autore della classe avesse una classe tipo
    codice:
    public class MercPlot extends JFrame implements quellochevuoi {
    }
    e che passasse l'oggetto MercPlot alla classe per l'anteprima di stampa.
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  6. #6
    Lo pensavo pure io ma a sto punto cosa vuole dire sto codice?

    codice:
    MercPlot.desktop.add(this);
    Non riesco a capire come funziona..

  7. #7

  8. #8
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Ho fatto questo... fa qualcosa, ma c'è da giocare ancora con il codice proposto.

    Classe di test (desktop, ma può essere qualsiasi altra cosa che derivi in qualche modo da Component)

    codice:
    import javax.swing.*;
    import java.awt.*;
    
    public class desktop extends JFrame {
    
      public desktop() {
          super("Ciao!");
          this.getContentPane().setLayout(new BorderLayout());
          JLabel north = new JLabel("In alto");
          JLabel south = new JLabel("In Basso");
          JLabel alcentro = new JLabel(new ImageIcon("C:/Documents and Settings/Andrea/Desktop/goofy.jpg"));
          this.getContentPane().add(north, BorderLayout.NORTH);
          this.getContentPane().add(south, BorderLayout.SOUTH);
          this.getContentPane().add(alcentro, BorderLayout.CENTER);
          this.setSize(400, 400);
          this.setVisible(true);
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
    
      public static void main (String[] args) {
        desktop d = new desktop();
        PrintPreview pp = new PrintPreview(d);
      }
    }
    e la classe rimaneggiata (appena) per l'antemprima:
    codice:
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.event.*;
    import java.awt.event.*;
    
    import java.awt.print.*;
    import java.lang.Math.*;
    
    import java.awt.image.*;
    
    
    public class PrintPreview extends JDialog implements ActionListener{
    
    public PrintPreview(Component pc){
    
            super((JFrame)pc, true);
            this.setSize(1024, 768);
    
            PC = pc;
    
    // buttons
    
            pf1 = new JRadioButton("Portrait");
               pf1.setActionCommand("1");
               pf1.setSelected(true);
    
               pf2 = new JRadioButton("Landscape");
               pf2.setActionCommand("2");
    
    
               pf.add(pf1);
               pf.add(pf2);
    
               /*pf1.setBackground(Pref.tbgc);
               pf2.setBackground(Pref.tbgc);
               cp.setBackground(Pref.tbgc);*/
    
    
            preview.addActionListener(this);
               print.addActionListener(this);
    
    
    // set
    
            pp.setPreferredSize(new Dimension(460, 460));
            pp.setBorder(BorderFactory.createLineBorder (Color.black, 2));
           // pp.setBackground(Pref.tbgc);
    
    
            hold.setPreferredSize(new Dimension(200, 280));
            hold.setBorder(BorderFactory.createLineBorder (Color.black, 2));
            //hold.setBackground(Pref.tbgc);
    
    
    // make main panel
    
    
            //mp.setBackground(Pref.tbgc);
    
            GridBagLayout gridbag = new GridBagLayout();
             GridBagConstraints constraints = new GridBagConstraints();
    
            mp.setLayout(gridbag);
    
    // make hold panel
    
    
            GridBagLayout g1 = new GridBagLayout();
             GridBagConstraints c1 = new GridBagConstraints();
    
            hold.setLayout(g1);
    
    // DELETE
    
    
            PF.setOrientation(PF.PORTRAIT);
    
    
    
    // GUI
    
    // hold
    
            c1.insets.top = 5;
            c1.insets.left = 45;
            c1.insets.right = 5;
    
    
            buildConstraints(c1, 0, 2, 2, 1, 1, 1);
            c1.fill = GridBagConstraints.BOTH;
            g1.setConstraints(pf1, c1);
    
            hold.add(pf1);
    
    
    
            c1.insets.top = 2;
    
            buildConstraints(c1, 0, 4, 2, 1, 1, 1);
            c1.fill = GridBagConstraints.BOTH;
            g1.setConstraints(pf2, c1);
    
            hold.add(pf2);
    
    //
            c1.insets.left = 5;
            c1.insets.right = 0;
            c1.insets.top = 25;
    
            buildConstraints(c1, 0, 6, 1, 1, 1, 1);
            c1.fill = GridBagConstraints.BOTH;
            g1.setConstraints(xsl, c1);
    
            hold.add(xsl);
    
            c1.insets.left = 0;
            c1.insets.right = 5;
    
            buildConstraints(c1, 1, 6, 1, 1, 1, 1);
            c1.fill = GridBagConstraints.BOTH;
            g1.setConstraints(xs, c1);
    
            hold.add(xs);
    
    //
            c1.insets.left = 5;
            c1.insets.right = 0;
            c1.insets.top = 5;
    
            buildConstraints(c1, 0, 8, 1, 1, 1, 1);
            c1.fill = GridBagConstraints.BOTH;
            g1.setConstraints(ysl, c1);
    
            hold.add(ysl);
    
            c1.insets.left = 0;
            c1.insets.right = 5;
    
            buildConstraints(c1, 1, 8, 1, 1, 1, 1);
            c1.fill = GridBagConstraints.BOTH;
            g1.setConstraints(ys, c1);
    
            hold.add(ys);
    
            c1.insets.left = 25;
            c1.insets.right = 5;
    
            buildConstraints(c1, 0, 10, 2, 1, 1, 1);
            c1.fill = GridBagConstraints.BOTH;
            g1.setConstraints(cp, c1);
    
            hold.add(cp);
    
    //
    
            c1.insets.left = 35;
            c1.insets.right = 35;
            c1.insets.top = 20;
    
            buildConstraints(c1, 0, 12, 2, 1, 1, 1);
            c1.fill = GridBagConstraints.BOTH;
            g1.setConstraints(ftp, c1);
    
            hold.add(ftp);
    
    
            c1.insets.left = 35;
            c1.insets.right = 35;
            c1.insets.top = 25;
    
            buildConstraints(c1, 0, 14, 2, 1, 1, 1);
            c1.fill = GridBagConstraints.BOTH;
            g1.setConstraints(preview, c1);
    
            hold.add(preview);
    
            c1.insets.bottom = 15;
            c1.insets.top = 5;
    
            buildConstraints(c1, 0, 16, 2, 1, 1, 1);
            c1.fill = GridBagConstraints.BOTH;
            g1.setConstraints(print, c1);
    
            hold.add(print);
    
    //
    
            constraints.insets.top = 10;
            constraints.insets.left = 10;
            constraints.insets.bottom = 10;
    
    
            buildConstraints(constraints, 0, 2, 1, 1, 1, 1);
            constraints.fill = GridBagConstraints.NONE;
            gridbag.setConstraints(hold, constraints);
    
            mp.add(hold);
    
            constraints.insets.right = 10;
    
    
            buildConstraints(constraints, 1, 2, 1, 1, 1, 1);
            constraints.fill = GridBagConstraints.NONE;
            gridbag.setConstraints(pp, constraints);
    
            mp.add(pp);
    
    
    
    
    // display
    
            this.setContentPane(mp);
    
            this.setVisible(true);
            this.setResizable(false);
    
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            setSize(720, 520);
    
               Dimension windowSize = getSize();
            setLocation(Math.max(0,(screenSize.width -windowSize.width)/2),
            (Math.max(0,(screenSize.height-windowSize.height)/2)));
    
    
    
    
    
    }
    
    public void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, double wx, double wy){
    
            gbc.gridx = gx;
            gbc.gridy = gy;
            gbc.gridwidth = gw;
            gbc.gridheight = gh;
            gbc.weightx = wx;
            gbc.weighty = wy;
    
    }
    
    public class PreviewPage extends JPanel{
    
    public PreviewPage(){
    
    }
    
    public void paint(Graphics g){
    
            super.paint(g);
            Graphics2D g2 = (Graphics2D)g;
    
    // PORTRAIT
    
            if(PF.getOrientation() == PF.PORTRAIT){
    
            g.setColor(Color.black);
            g.drawRect(60, 10, 340, 440);
    
            int x1 = (int)Math.rint( ( (double)PF.getImageableX() / 72 ) * 40 );
            int y1 = (int)Math.rint( ( (double)PF.getImageableY() / 72 ) * 40 );
    
            int l1 = (int)Math.rint( ( (double)PF.getImageableWidth() / 72 ) * 40 );
            int h1 = (int)Math.rint( ( (double)PF.getImageableHeight() / 72 ) * 40 );
    
            g.setColor(Color.red);
            g.drawRect(x1+60, y1+10, l1, h1);
    
            setScales();
    
            int x2 = (int)Math.rint( 1028 / XSC );
            int y2 = (int)Math.rint( 768 / YSC );
    
    
            Image image = new BufferedImage( x2, y2, BufferedImage.TYPE_INT_ARGB);
    
            PC.paint( image.getGraphics() );
    
            g.drawImage(image, x1+60, y1+10, l1, h1, this);
    
    
    
    
    
            }
    
    // LANDSCAPE
    
            if(PF.getOrientation() == PF.LANDSCAPE){
    
            g.setColor(Color.black);
            g.drawRect(10, 60, 440, 340);
    
            int x1 = (int)Math.rint( ( (double)PF.getImageableX() / 72 ) * 40 );
            int y1 = (int)Math.rint( ( (double)PF.getImageableY() / 72 ) * 40 );
    
            int l1 = (int)Math.rint( ( (double)PF.getImageableWidth() / 72 ) * 40 );
            int h1 = (int)Math.rint( ( (double)PF.getImageableHeight() / 72 ) * 40 );
    
            g.setColor(Color.red);
            g.drawRect(x1+10, y1+60, l1, h1);
    
            setScales();
    
            int x2 = (int)Math.rint( 1028 / XSC );
            int y2 = (int)Math.rint( 768 / YSC );
    
    
            Image image = new BufferedImage( x2, y2, BufferedImage.TYPE_INT_ARGB);
    
            PC.paint( image.getGraphics() );
    
            g.drawImage(image, x1+10, y1+60, l1, h1, this);
    
    
            }
    
    
    }
    
            Graphics P;
    
    }
    
    public void actionPerformed(ActionEvent e){
    
    
    // fit to page
            if(e.getSource() == ftp){
    
            }
    
    // preview
            if(e.getSource() == preview){
    
            setProperties();
            }
    
    
    // print
            if(e.getSource() == print){
            doPrint();
            }
    
    }
    
    public void setProperties(){
    
            if(pf1.isSelected() == true){
            PF.setOrientation(PF.PORTRAIT);
            }
    
            if(pf2.isSelected() == true){
            PF.setOrientation(PF.LANDSCAPE);
            }
    
            setScales();
    
            pp.repaint();
    
    }
    
    public void setScales(){
    
            try{
            XSC = Double.parseDouble( xs.getText() );
            } catch (NumberFormatException e) { }
    
            try{
            YSC = Double.parseDouble( ys.getText() );
            } catch (NumberFormatException e) { }
    
    
    }
    
    public void doPrint(){
    
            PrintThis();
    
    }
    
    public void PrintThis(){
    
            PrinterJob printerJob = PrinterJob.getPrinterJob();
    
            Book book = new Book();
            book.append( new PrintPage(), PF );
    
            printerJob.setPageable(book);
    
            boolean doPrint = printerJob.printDialog();
            if (doPrint) {
    
            try {
            printerJob.print();
            } catch (PrinterException exception) {
            System.err.println("Printing error: " + exception);
            }
    
            }
    
    }
    
    public class PrintPage implements Printable{
    
    public int print(Graphics g, PageFormat format, int pageIndex) {
    
    
    
            Graphics2D g2D = (Graphics2D) g;
    
            g2D.translate(format.getImageableX (), format.getImageableY ());
    
            disableDoubleBuffering(mp);
    
            System.out.println("get i x " + format.getImageableX ());
            System.out.println("get i x " + format.getImageableY ());
            System.out.println("getx: " + format.getImageableWidth() );
            System.out.println("getx: " + format.getImageableHeight() );
    
    
    
            // scale to fill the page
            double dw = format.getImageableWidth();
            double dh = format.getImageableHeight();
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    
            setScales();
    
            double xScale = dw / (1028/XSC);
            double yScale = dh / (768/YSC);
            double scale = Math.min(xScale,yScale);
    
    
    
            System.out.println("" + scale);
    
            g2D.scale( xScale, yScale);
    
    
            ((JPanel)PC).paint(g);
    
            enableDoubleBuffering(mp);
    
    
            return Printable.PAGE_EXISTS;
    
    }
    
    public void disableDoubleBuffering(Component c) {
        RepaintManager currentManager = RepaintManager.currentManager(c);
        currentManager.setDoubleBufferingEnabled(false);
    }
    
    public void enableDoubleBuffering(Component c) {
        RepaintManager currentManager = RepaintManager.currentManager(c);
        currentManager.setDoubleBufferingEnabled(true);
    }
    
    }
    
    
            Component PC;
    
            PageFormat PF = new PageFormat();
    
            double XSC = 1;
            double YSC = 1;
    
    // gui comp
    
            JPanel mp = new JPanel();
            JPanel hold = new JPanel();
            JPanel pp = new PreviewPage();
    
            ButtonGroup pf = new ButtonGroup();
            JRadioButton pf1;
            JRadioButton pf2;
    
            JLabel xsl = new JLabel("X Scale:", JLabel.LEFT);
            JTextField xs = new JTextField("1");
    
            JLabel ysl = new JLabel("Y Scale:", JLabel.LEFT);
            JTextField ys = new JTextField("1");
    
            JButton ftp = new JButton("Fit to Page");
            JCheckBox cp = new JCheckBox("Constrain Proportions");
    
            JButton preview = new JButton("PREVIEW");
            JButton print = new JButton("PRINT");
    
    
    }
    Auguri, in tutti i sensi
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  9. #9
    Non mi cammina manco a piangere... nè fa l'anteprima giusta nè stampa..

    Io dovrei stampare una pagina formattata abbastanza complessa (dovrebbe rappresentare una fattura con almeno due tabelle). Non conoscete una classe disponibile free che mi permetta dato come parametro un pannello (con JTable e JLabel) di ottenere una bella pagina stampata?

    Lo so che dovrei svilupparla io.. ma questa stampa mi manda ai pazzi..


  10. #10
    Originariamente inviato da superpelo
    Non mi cammina manco a piangere... nè fa l'anteprima giusta nè stampa..

    Io dovrei stampare una pagina formattata abbastanza complessa (dovrebbe rappresentare una fattura con almeno due tabelle). Non conoscete una classe disponibile free che mi permetta dato come parametro un pannello (con JTable e JLabel) di ottenere una bella pagina stampata?

    Lo so che dovrei svilupparla io.. ma questa stampa mi manda ai pazzi..

    Perchè non usi JFreeReport?Devi studiarci un po sopra ma i risultati sono garantiti e poi hai gratis non solo l'anteprima di stampa e la stamapa ma anche l'eesportazione in vari formati (ottima l'esportazione in pdf). Si tratta di una libreria di reporting con licenza LGPL. I dati glie li passi appunto fornendogli un TableModel (che è quello che serve a te) e poi gli "speighi" come presentarli mediante uno stylesheet xml.

    PS.Non vedo perchè ti debba sentire in dovere di svilupparla tu se esistono già delle ottime soluzioni mediante le quali ottieni risultati migliori, maggior affidabilità (perchè sono testate),e maggior efficienza in genere. Io sono per il non reinventare la ruota quando possibile.Sinceramente non mi impegno mai in un progetto che risolve un problema per il quale esiste già una soluzione che ritengo buona o ottima. Se ritengo che tale soluzione abbia delle pecche mi impegno nel modificare quella e solo se non esiste proprio una soluzione mi lancio nel programmare da zero.Mi sembra la cosa più sensata del resto!
    Il centro dell'attenzione non è sempre un buon posto in cui trovarsi

    Mai discutere con uno stupido, la gente potrebbe non capire la differenza. (O. W.)

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.