JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / fr / orsay / lri / varna / views / PrintTest.java
1 /*
2  VARNA is a tool for the automated drawing, visualization and annotation of the secondary structure of RNA, designed as a companion software for web servers and databases.
3  Copyright (C) 2008  Kevin Darty, Alain Denise and Yann Ponty.
4  electronic mail : Yann.Ponty@lri.fr
5  paper mail : LRI, bat 490 Université Paris-Sud 91405 Orsay Cedex France
6
7  This file is part of VARNA version 3.1.
8  VARNA version 3.1 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
9  as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10
11  VARNA version 3.1 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  See the GNU General Public License for more details.
14
15  You should have received a copy of the GNU General Public License along with VARNA version 3.1.
16  If not, see http://www.gnu.org/licenses.
17  */
18 package fr.orsay.lri.varna.views;
19
20 import java.awt.BorderLayout;
21 import java.awt.Container;
22 import java.awt.Font;
23 import java.awt.Graphics;
24 import java.awt.Graphics2D;
25 import java.awt.Shape;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.awt.font.FontRenderContext;
29 import java.awt.font.TextLayout;
30 import java.awt.geom.AffineTransform;
31 import java.awt.geom.GeneralPath;
32 import java.awt.geom.Line2D;
33 import java.awt.geom.Point2D;
34 import java.awt.geom.Rectangle2D;
35 import java.awt.print.PageFormat;
36 import java.awt.print.Printable;
37 import java.awt.print.PrinterException;
38 import java.awt.print.PrinterJob;
39
40 import javax.print.attribute.HashPrintRequestAttributeSet;
41 import javax.print.attribute.PrintRequestAttributeSet;
42 import javax.swing.JButton;
43 import javax.swing.JFrame;
44 import javax.swing.JOptionPane;
45 import javax.swing.JPanel;
46
47 /**
48  * This program demonstrates how to print 2D graphics
49  */
50 public class PrintTest {
51         @SuppressWarnings("deprecation")
52         public static void main(String[] args) {
53                 JFrame frame = new PrintTestFrame();
54                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
55                 frame.show();
56         }
57 }
58
59 /**
60  * This frame shows a panel with 2D graphics and buttons to print the graphics
61  * and to set up the page format.
62  */
63
64 @SuppressWarnings("serial")
65 class PrintTestFrame extends JFrame {
66         public PrintTestFrame() {
67                 setTitle("PrintTest");
68                 setSize(WIDTH, HEIGHT);
69
70                 Container contentPane = getContentPane();
71                 canvas = new PrintPanel();
72                 contentPane.add(canvas, BorderLayout.CENTER);
73
74                 attributes = new HashPrintRequestAttributeSet();
75
76                 JPanel buttonPanel = new JPanel();
77                 JButton printButton = new JButton("Print");
78                 buttonPanel.add(printButton);
79                 printButton.addActionListener(new ActionListener() {
80
81                         public void actionPerformed(ActionEvent event) {
82                                 try {
83                                         PrinterJob job = PrinterJob.getPrinterJob();
84                                         job.setPrintable(canvas);
85                                         if (job.printDialog(attributes)) {
86                                                 job.print(attributes);
87                                         }
88                                 } catch (PrinterException exception) {
89                                         JOptionPane.showMessageDialog(PrintTestFrame.this,
90                                                         exception);
91                                 }
92                         }
93                 });
94
95                 JButton pageSetupButton = new JButton("Page setup");
96                 buttonPanel.add(pageSetupButton);
97                 pageSetupButton.addActionListener(new ActionListener() {
98                         public void actionPerformed(ActionEvent event) {
99                                 PrinterJob job = PrinterJob.getPrinterJob();
100                                 job.pageDialog(attributes);
101                         }
102                 });
103
104                 contentPane.add(buttonPanel, BorderLayout.NORTH);
105         }
106
107         private PrintPanel canvas;
108
109         private PrintRequestAttributeSet attributes;
110
111         private static final int WIDTH = 300;
112
113         private static final int HEIGHT = 300;
114 }
115
116 /**
117  * This panel generates a 2D graphics image for screen display and printing.
118  */
119
120 @SuppressWarnings("serial")
121 class PrintPanel extends JPanel implements Printable {
122
123         public void paintComponent(Graphics g) {
124                 super.paintComponent(g);
125                 Graphics2D g2 = (Graphics2D) g;
126                 drawPage(g2);
127         }
128
129         public int print(Graphics g, PageFormat pf, int page)
130                         throws PrinterException {
131                 if (page >= 1)
132                         return Printable.NO_SUCH_PAGE;
133                 Graphics2D g2 = (Graphics2D) g;
134                 g2.translate(pf.getImageableX(), pf.getImageableY());
135                 g2.draw(new Rectangle2D.Double(0, 0, pf.getImageableWidth(), pf
136                                 .getImageableHeight()));
137
138                 drawPage(g2);
139                 return Printable.PAGE_EXISTS;
140         }
141
142         /**
143          * This method draws the page both on the screen and the printer graphics
144          * context.
145          * 
146          * @param g2
147          *            the graphics context
148          */
149         public void drawPage(Graphics2D g2) {
150                 FontRenderContext context = g2.getFontRenderContext();
151                 Font f = new Font("Serif", Font.PLAIN, 72);
152
153                 boolean drawOutline = true;
154                 /**
155                  * textLayout is not implemented 
156                  * 
157                  * @j2sNative 
158                  * 
159                  *            drawOutline = false;
160                  */
161                 {}
162                 if (drawOutline) {
163                         // BH: SwingJS HTML5 would have to use a different method for this
164
165                         GeneralPath clipShape = new GeneralPath();
166
167                         TextLayout layout = new TextLayout("Hello", f, context);
168                         AffineTransform transform = AffineTransform.getTranslateInstance(0, 72);
169                         Shape outline = layout.getOutline(transform);
170                         clipShape.append(outline, false);
171
172                         layout = new TextLayout("World", f, context);
173                         transform = AffineTransform.getTranslateInstance(0, 144);
174                         outline = layout.getOutline(transform);
175                         clipShape.append(outline, false);
176
177                         g2.draw(clipShape);
178                         g2.clip(clipShape);
179                 } else {
180                         g2.setFont(f);
181                         g2.drawString("Hello", 0, 72);
182                         g2.drawString("World", 0, 144);
183                 }
184
185                 final int NLINES = 50;
186                 Point2D p = new Point2D.Double(0, 0);
187                 for (int i = 0; i < NLINES; i++) {
188                         double x = (2 * getWidth() * i) / NLINES;
189                         double y = (2 * getHeight() * (NLINES - 1 - i)) / NLINES;
190                         Point2D q = new Point2D.Double(x, y);
191                         g2.draw(new Line2D.Double(p, q));
192                 }
193         }
194 }