Skip to content

Commit 27ac39a

Browse files
author
Damon Nguyen
committedMar 25, 2024
8328719: Convert java/awt/print/PageFormat/SetOrient.html applet test to main
Reviewed-by: psadhukhan, prr
1 parent fb8f2a0 commit 27ac39a

File tree

2 files changed

+81
-90
lines changed

2 files changed

+81
-90
lines changed
 

‎test/jdk/java/awt/print/PageFormat/SetOrient.html

-49
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,56 +21,96 @@
2121
* questions.
2222
*/
2323

24-
/**
25-
* @bug 4186119: setting orientation does not affect printer
24+
/*
25+
* @test
26+
* @key printer
27+
* @bug 4186119
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
2630
* @summary Confirm that the clip and transform of the Graphics2D is
2731
* affected by the landscape orientation of the PageFormat.
28-
* @run applet/manual=yesno SetOrient.html
32+
* @run main/manual SetOrient
2933
*/
3034

31-
import java.awt.*;
32-
import java.awt.geom.*;
33-
import java.awt.print.*;
34-
import java.applet.Applet;
35-
36-
public class SetOrient extends Applet implements Printable {
37-
PrinterJob pjob;
35+
import java.awt.Color;
36+
import java.awt.Graphics;
37+
import java.awt.Graphics2D;
38+
import java.awt.geom.Ellipse2D;
39+
import java.awt.print.Book;
40+
import java.awt.print.PageFormat;
41+
import java.awt.print.Printable;
42+
import java.awt.print.PrinterException;
43+
import java.awt.print.PrinterJob;
3844

39-
public void init() {
40-
pjob = PrinterJob.getPrinterJob();
45+
import javax.swing.JButton;
4146

42-
Book book = new Book();
43-
PageFormat pf = pjob.defaultPage();
44-
pf.setOrientation(PageFormat.PORTRAIT);
45-
book.append(this, pf);
46-
pf = pjob.defaultPage();
47-
pf.setOrientation(PageFormat.LANDSCAPE);
48-
book.append(this, pf);
49-
pjob.setPageable(book);
47+
public class SetOrient {
48+
private static final String INSTRUCTIONS =
49+
"""
50+
This test prints two pages and sends them to the printer.
51+
One page is in PORTRAIT orientation and the other is in LANDSCAPE
52+
orientation. On each page it draws an ellipse inscribed in the clip
53+
boundary established by the PrinterJob. The ellipse should fill the
54+
page within the bounds established by the default margins and not
55+
extend off any end or side of the page. Also, the string "Portrait"
56+
or "Landscape" should be oriented correctly.
57+
""";
5058

51-
try {
52-
pjob.print();
53-
} catch (PrinterException e) {
54-
throw new RuntimeException(e.getMessage());
55-
}
59+
public static void main(String[] args) throws Exception {
60+
PassFailJFrame
61+
.builder()
62+
.title("SetOrient Test Instructions")
63+
.instructions(INSTRUCTIONS)
64+
.rows((int) INSTRUCTIONS.lines().count() + 2)
65+
.columns(40)
66+
.splitUIBottom(SetOrient::createAndShowGUI)
67+
.build()
68+
.awaitAndCheck();
5669
}
5770

58-
public int print(Graphics g, PageFormat pf, int pageIndex) {
59-
Graphics2D g2d = (Graphics2D)g;
60-
drawGraphics(g2d, pf);
61-
return Printable.PAGE_EXISTS;
62-
}
71+
public static JButton createAndShowGUI() {
72+
JButton btn = new JButton("PRINT");
73+
btn.addActionListener(e -> {
74+
PrinterJob pjob = PrinterJob.getPrinterJob();
75+
76+
Printable p = new Printable() {
77+
public int print(Graphics g, PageFormat pf, int pageIndex) {
78+
Graphics2D g2d = (Graphics2D)g;
79+
drawGraphics(g2d, pf);
80+
return Printable.PAGE_EXISTS;
81+
}
82+
83+
void drawGraphics(Graphics2D g, PageFormat pf) {
84+
double ix = pf.getImageableX();
85+
double iy = pf.getImageableY();
86+
double iw = pf.getImageableWidth();
87+
double ih = pf.getImageableHeight();
88+
89+
g.setColor(Color.black);
90+
g.drawString(((pf.getOrientation() == PageFormat.PORTRAIT)
91+
? "Portrait" : "Landscape"),
92+
(int) (ix + iw / 2), (int) (iy + ih / 2));
93+
g.draw(new Ellipse2D.Double(ix, iy, iw, ih));
94+
}
95+
};
6396

64-
void drawGraphics(Graphics2D g, PageFormat pf) {
65-
double ix = pf.getImageableX();
66-
double iy = pf.getImageableY();
67-
double iw = pf.getImageableWidth();
68-
double ih = pf.getImageableHeight();
97+
Book book = new Book();
98+
PageFormat pf = pjob.defaultPage();
99+
pf.setOrientation(PageFormat.PORTRAIT);
100+
book.append(p, pf);
101+
pf = pjob.defaultPage();
102+
pf.setOrientation(PageFormat.LANDSCAPE);
103+
book.append(p, pf);
104+
pjob.setPageable(book);
69105

70-
g.setColor(Color.black);
71-
g.drawString(((pf.getOrientation() == PageFormat.PORTRAIT)
72-
? "Portrait" : "Landscape"),
73-
(int) (ix+iw/2), (int) (iy+ih/2));
74-
g.draw(new Ellipse2D.Double(ix, iy, iw, ih));
106+
try {
107+
pjob.print();
108+
} catch (PrinterException ex) {
109+
ex.printStackTrace();
110+
String msg = "PrinterException: " + ex.getMessage();
111+
PassFailJFrame.forceFail(msg);
112+
}
113+
});
114+
return btn;
75115
}
76116
}

0 commit comments

Comments
 (0)
Please sign in to comment.