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
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.
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.
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.
18 package fr.orsay.lri.varna.views;
20 import java.awt.BorderLayout;
21 import java.awt.Container;
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;
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;
48 * This program demonstrates how to print 2D graphics
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);
60 * This frame shows a panel with 2D graphics and buttons to print the graphics
61 * and to set up the page format.
64 @SuppressWarnings("serial")
65 class PrintTestFrame extends JFrame {
66 public PrintTestFrame() {
67 setTitle("PrintTest");
68 setSize(WIDTH, HEIGHT);
70 Container contentPane = getContentPane();
71 canvas = new PrintPanel();
72 contentPane.add(canvas, BorderLayout.CENTER);
74 attributes = new HashPrintRequestAttributeSet();
76 JPanel buttonPanel = new JPanel();
77 JButton printButton = new JButton("Print");
78 buttonPanel.add(printButton);
79 printButton.addActionListener(new ActionListener() {
81 public void actionPerformed(ActionEvent event) {
83 PrinterJob job = PrinterJob.getPrinterJob();
84 job.setPrintable(canvas);
85 if (job.printDialog(attributes)) {
86 job.print(attributes);
88 } catch (PrinterException exception) {
89 JOptionPane.showMessageDialog(PrintTestFrame.this,
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);
104 contentPane.add(buttonPanel, BorderLayout.NORTH);
107 private PrintPanel canvas;
109 private PrintRequestAttributeSet attributes;
111 private static final int WIDTH = 300;
113 private static final int HEIGHT = 300;
117 * This panel generates a 2D graphics image for screen display and printing.
120 @SuppressWarnings("serial")
121 class PrintPanel extends JPanel implements Printable {
123 public void paintComponent(Graphics g) {
124 super.paintComponent(g);
125 Graphics2D g2 = (Graphics2D) g;
129 public int print(Graphics g, PageFormat pf, int page)
130 throws PrinterException {
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()));
139 return Printable.PAGE_EXISTS;
143 * This method draws the page both on the screen and the printer graphics
147 * the graphics context
149 public void drawPage(Graphics2D g2) {
150 FontRenderContext context = g2.getFontRenderContext();
151 Font f = new Font("Serif", Font.PLAIN, 72);
153 boolean drawOutline = true;
155 * textLayout is not implemented
159 * drawOutline = false;
163 // BH: SwingJS HTML5 would have to use a different method for this
165 GeneralPath clipShape = new GeneralPath();
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);
172 layout = new TextLayout("World", f, context);
173 transform = AffineTransform.getTranslateInstance(0, 144);
174 outline = layout.getOutline(transform);
175 clipShape.append(outline, false);
181 g2.drawString("Hello", 0, 72);
182 g2.drawString("World", 0, 144);
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));