1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
|
/*
* Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Checkbox;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.JobAttributes;
import java.awt.Label;
import java.awt.List;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.PageAttributes;
import java.awt.Panel;
import java.awt.PrintJob;
import java.awt.Scrollbar;
import java.awt.ScrollPane;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.JobAttributes.DialogType;
import java.awt.PageAttributes.OriginType;
import java.util.Enumeration;
import java.util.Properties;
/*
* @test
* @bug 4247583
* @key printer
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @summary Tests that the old Properties API still works
* @run main/manual PrintCompatibilityTest
*/
public class PrintCompatibilityTest {
public static void main(String[] args) throws Exception {
String INSTRUCTIONS = """
A frame window will appear.
Choose 'Print to Printer...' from the 'Print' menu. Make sure that you print
to a printer, not a file. Examine the output and verify that the frame and all
the components in it get printed properly.
Known problems:
* The text in the second row of the menubar is not indented correctly.
You can also use the 'Print to Screen...' command for a quick manual check that
printing works, but this is only for debugging purposes.""";
PassFailJFrame.builder()
.title("PrintComponentTest Test Instructions")
.instructions(INSTRUCTIONS)
.rows((int) INSTRUCTIONS.lines().count() + 2)
.columns(60)
.testTimeOut(10)
.testUI(new MainFrame())
.logArea(8)
.build()
.awaitAndCheck();
}
}
class MainFrame extends Frame {
private LWContainer lwc;
public MainFrame() {
super("PrintCompatibilityTest");
setSize(800, 400);
setLayout(new FlowLayout());
// peered components
Button button = new Button("Button");
button.setFont(new Font("Dialog", Font.PLAIN, 12));
add(button);
add(new TestCanvas());
Checkbox cbox = new Checkbox("Checkbox", true);
cbox.setFont(new Font("DialogInput", Font.PLAIN, 12));
add(cbox);
Choice choice = new Choice();
choice.add("Choice 1");
choice.add("Choice Two");
choice.setFont(new Font("Monospaced", Font.PLAIN, 12));
add(choice);
Label label = new Label("Label");
label.setFont(new Font("Serif", Font.PLAIN, 12));
add(label);
List list = new List();
list.add("List 1");
list.add("List Two");
list.setFont(new Font("SansSerif", Font.PLAIN, 12));
add(list);
add(new Scrollbar(Scrollbar.VERTICAL) );
add(new Scrollbar(Scrollbar.HORIZONTAL) );
ScrollPane scrollpane = new ScrollPane();
Button spButton = new Button("Button in a scrollpane");
spButton.setFont(new Font("Monospaced", Font.PLAIN, 12));
scrollpane.add(spButton);
add(scrollpane);
TextArea textarea = new TextArea("TextArea", 3, 30);
textarea.setFont(new Font("Dialog", Font.ITALIC, 10));
add(textarea);
TextField textfield = new TextField("TextField");
textfield.setFont(new Font("DialogInput", Font.ITALIC, 10));
add(textfield);
// nested components
Panel panel1 = new Panel();
panel1.setLayout(new FlowLayout());
panel1.setBackground(Color.red);
this.add(panel1);
Button p1Button = new Button("level 2");
p1Button.setFont(new Font("Monospaced", Font.ITALIC, 10));
panel1.add(p1Button);
Panel panel2 = new Panel();
panel2.setLayout(new FlowLayout());
panel2.setBackground(Color.green);
panel1.add(panel2);
Button p2Button = new Button("level 3");
p2Button.setFont(new Font("Serif", Font.ITALIC, 10));
panel2.add(p2Button);
// lightweight components
LWButton lwbutton = new LWButton("LWbutton");
lwbutton.setFont(new Font("SansSerif", Font.ITALIC, 10));
add(lwbutton);
lwc = new LWContainer("LWContainerLWContainerLWContainerLWContainerLWContainerLWContainerLWContainerLWContainerLWContainerLWContainerLWContainerLWContainerLWContainer");
lwc.setFont(new Font("Monospaced", Font.ITALIC, 10));
add(lwc);
Button lwcButton1 = new Button("HW Button 1");
Button lwcButton2 = new Button("HW Button 2");
LWButton lwcButton3 = new LWButton("LW Button");
lwcButton1.setFont(new Font("Dialog", Font.BOLD, 14));
lwcButton2.setFont(new Font("DialogInput", Font.BOLD, 14));
lwcButton3.setFont(new Font("Monospaced", Font.BOLD, 14));
lwc.add(lwcButton1);
lwc.add(lwcButton2);
lwc.add(lwcButton3);
// overlapping components
add(new ZOrderPanel());
///////////////////////
Menu menu = new Menu("Print");
Menu menu2 = new Menu("File");
Menu menu3 = new Menu("Edit");
Menu menu4 = new Menu("ReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLong");
menu2.setFont(new Font("SansSerif", Font.BOLD, 20));
menu2.setEnabled(false);
menu3.setFont(new Font("Monospaced", Font.ITALIC, 18));
menu3.setEnabled(false);
menu4.setEnabled(false);
MenuItem itemPrinter = new MenuItem("Print to Printer...");
MenuItem itemScreen = new MenuItem("Print to Screen...");
menu.add(itemPrinter);
menu.add(itemScreen);
MenuBar menuBar = new MenuBar();
menuBar.add( menu );
menuBar.add( menu2 );
menuBar.add( menu3 );
menuBar.add( menu4 );
setMenuBar(menuBar);
itemPrinter.addActionListener( new ActionPrint() );
itemScreen.addActionListener( new ActionPrintToScreen() );
setVisible(true);
}
static void printProps(Properties props)
{
Enumeration propNames = props.propertyNames();
while (propNames.hasMoreElements()) {
String propName = (String)propNames.nextElement();
PassFailJFrame.log( propName + " = " + props.getProperty(propName));
}
}
class ActionPrint implements ActionListener {
private final int ITERATIONS = 1;
private Properties props = new Properties();
public void actionPerformed(ActionEvent ev) {
PassFailJFrame.log("About to show print dialog...");
printProps(props);
PrintJob pj = getToolkit().getPrintJob(
MainFrame.this, "Print test!", props);
if (pj == null) {
return;
}
Dimension d = pj.getPageDimension();
PassFailJFrame.log("About to print...");
PassFailJFrame.log("Dimensions: " + d);
printProps(props);
// For xor mode set, there is a printing issue with number of copies to be print.
// So, ITERATIONS are changed to 1 from 3.
// So, for now the XOR related code is commented out.
//boolean xor = false;
for (int i = 0; i < ITERATIONS; i++) {
Graphics g = pj.getGraphics();
g.setColor(Color.red);
//if (xor) {
// g.setXORMode(Color.blue);
//}
g.translate(13, 13);
printAll(g);
g.dispose();
//xor = (xor) ? false : true;
}
// For xor mode set, LWC components don't get printed.
// So, for now the code is commented out and separate bug
// (JDK-8340495) is filed to handle it.
// one more page so that we can test printing a lightweight
// at the top of the hierarchy (BugId 4212564)
//Graphics g = pj.getGraphics();
//g.setColor(Color.red);
//g.translate(13, 13);
//lwc.printAll(g);
//g.dispose();
// end 4212564
pj.end();
}
}
class ActionPrintToScreen implements ActionListener {
public void actionPerformed(ActionEvent ev) {
PrintFrame printFrame = new PrintFrame(MainFrame.this);
printFrame.show();
Graphics g = printFrame.getGraphics();
g.setColor(Color.red);
printAll(g);
g.dispose();
}
}
// Frame window that displays results of printing
// main window to a screen Graphics-- useful for
// quick testing of printing
class PrintFrame extends Frame
{
private Component printComponent;
public PrintFrame( Component c )
{
super("Print to Screen");
printComponent = c ;
addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
setVisible(false);
dispose();
}
}
);
setSize(printComponent.getSize());
setResizable(false);
}
public void paint( Graphics g ) {
printComponent.printAll(g);
}
}
class LWButton extends Component {
String label;
int width = 100;
int height = 30;
public LWButton(String label) {
super();
this.label = label;
}
public void paint(Graphics g) {
Dimension d = getSize();
g.setColor(Color.orange);
g.setFont(getFont());
g.fillRect(0, 0, d.width, d.height);
g.setColor(Color.black);
int x = 5;
int y = (d.height - 5);
g.drawString(label, x, y);
}
public Dimension getPreferredSize()
{
return new Dimension(width, height);
}
}
class LWContainer extends Container {
String label;
int width = 300;
int height = 100;
public LWContainer(String label) {
super();
this.label = label;
setLayout(new FlowLayout());
}
public void paint(Graphics g) {
super.paint(g);
Dimension d = getSize();
g.setColor(Color.green);
g.setFont(getFont());
g.drawLine(0, 0, d.width - 1, 0);
g.drawLine(d.width - 1, 0, d.width - 1, d.height - 1);
g.drawLine(d.width - 1, d.height - 1, 0, d.height - 1);
g.drawLine(0, d.height - 1, 0, 0);
g.setColor(Color.black);
int x = 5;
int y = (d.height - 5);
g.drawString(label, x, y);
}
public Dimension getPreferredSize()
{
return new Dimension(width, height);
}
}
class TestCanvas extends Canvas {
int width = 100;
int height = 100;
public void paint(Graphics g) {
g.setColor(Color.blue);
g.fillRoundRect(10, 10, 50, 50, 15, 30);
g.setColor(Color.red);
g.fillOval(70, 70, 25, 25);
}
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
}
class ZOrderPanel extends Panel
{
ZOrderPanel()
{
setLayout(null);
Component first, second, third, fourth;
setVisible(true);
// add first component
first = makeBox("Second", Color.blue,
new Font("Serif", Font.BOLD, 14),
-1);
// insert on top
second = makeBox("First", Color.yellow,
new Font("SansSerif", Font.BOLD, 14),
0);
// put at the back
fourth = makeBox("Fourth", Color.red,
new Font("Monospaced", Font.BOLD, 14),
2);
// insert in last position
third = makeBox("Third", Color.green,
new Font("Dialog", Font.PLAIN, 12),
3);
// swap third and fourth to correct positions
remove(third);
add(third, 2);
// re-validate so third and fourth peers change position
validate();
// now make things really interesting with a lightweight
// component at the top of the z-order, that should print
// _below_ the native guys to match the screen...
add(new LWButton("LWButton"), 0);
}
public Dimension preferredSize()
{
return new Dimension(260, 80);
}
public void layout()
{
int i, n;
Insets ins = getInsets();
n = getComponentCount();
for (i = n-1; i >= 0; i--) {
Component p = getComponent(i);
p.setBounds(ins.left + 40 * i, ins.top + 5 * i, 60, 60);
}
}
public Component makeBox(String s, Color c, Font f, int index)
{
Label l = new Label(s);
l.setBackground(c);
l.setAlignment(Label.RIGHT);
l.setFont(f);
add(l, index);
validate();
return l;
}
}
}
|