View Javadoc

1   /*
2    *  UnBBayes
3    *  Copyright (C) 2002, 2008 Universidade de Brasilia - http://www.unb.br
4    *
5    *  This file is part of UnBBayes.
6    *
7    *  UnBBayes is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  UnBBayes is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with UnBBayes.  If not, see <http://www.gnu.org/licenses/>.
19   *
20   */
21  package unbbayes.controller;
22  
23  import java.awt.Graphics;
24  import java.awt.Graphics2D;
25  import java.awt.Rectangle;
26  import java.awt.geom.AffineTransform;
27  import java.awt.print.PageFormat;
28  import java.awt.print.Pageable;
29  import java.awt.print.Printable;
30  
31  import javax.swing.JComponent;
32  
33  /**
34   * Useful class for printing a view of a graphic-mode network
35   */
36  public class PrintNet implements Printable, Pageable {
37  
38    protected JComponent net;
39    protected PageFormat pageFormat;
40    protected Rectangle rect;
41    protected String title;
42  
43    public PrintNet(String title, JComponent net, Rectangle rect, PageFormat pf) {
44       this.net = net;
45       this.title = title;
46       pageFormat = pf;
47       this.rect = rect;
48    }
49  
50    /**
51     * Perform the printing here
52     */
53    public int print(Graphics g, PageFormat pf, int index) {
54        if (index == 0) {
55            paintComponent(g);
56            return Printable.PAGE_EXISTS;
57        }
58        return Printable.NO_SUCH_PAGE;
59    }
60  
61    /**
62     * Paint / print a portion of the component
63     */
64    protected void paintComponent(Graphics g) {
65      double scaleX = (pageFormat.getImageableWidth()) / rect.width;
66      double scaleY = (pageFormat.getImageableHeight()-30) / (rect.height);
67      double scaleFactor = Math.min(Math.min(scaleX, scaleY), 1.0);
68  
69      Graphics2D g2 = (Graphics2D)g;
70      Rectangle clipRect = g2.getClipBounds();
71      AffineTransform at = g2.getTransform();
72  
73      int x = (int)(pageFormat.getImageableX());
74      int y = (int)(pageFormat.getImageableY());
75  //    int w = (int)(pageFormat.getImageableWidth());
76  //    int h = (int)(pageFormat.getImageableHeight());
77  
78      g.drawString(title,x,y+15);
79      g2.translate(x - rect.getX(), y - rect.getY() + 30);
80      g2.setClip(rect);
81      g2.scale(scaleFactor, scaleFactor);
82      net.paint(g);
83      g2.setTransform(at);
84      g2.setClip(clipRect);
85    }
86  
87  
88    /**
89     * Calculate the number of pages it will take to print the entire Network
90     */
91    public int getNumberOfPages() {
92      return 1;
93    }
94  
95    public Printable getPrintable(int index) {
96      return this;
97    }
98  
99    public PageFormat getPageFormat(int index) {
100     return pageFormat;
101   }
102 
103 }