Skip to content

8357176: java.awt javadoc code examples still use Applet API #25278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions src/java.desktop/share/classes/java/awt/BorderLayout.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2025, 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
Expand Down Expand Up @@ -89,28 +89,37 @@
* the {@code CENTER} component may stretch both horizontally
* and vertically to fill any space left over.
* <p>
* Here is an example of five buttons in an applet laid out using
* Here is an example of five buttons in a window laid out using
* the {@code BorderLayout} layout manager:
* <p>
* <img src="doc-files/BorderLayout-1.gif" alt="Diagram of an applet
* <img src="doc-files/BorderLayout-1.png" alt="Diagram of a window
* demonstrating BorderLayout. Each section of the BorderLayout contains a
* Button corresponding to its position in the layout, one of: North, West,
* Center, East, or South." style="margin: 7px 10px;">
* <p>
* The code for this applet is as follows:
* The code for this program is as follows:
*
* <hr><blockquote><pre>
* import java.awt.*;
* import java.applet.Applet;
* import java.awt.BorderLayout;
* import java.awt.Button;
* import java.awt.EventQueue;
* import java.awt.Frame;
*
* public class buttonDir extends Applet {
* public void init() {
* setLayout(new BorderLayout());
* add(new Button("North"), BorderLayout.NORTH);
* add(new Button("South"), BorderLayout.SOUTH);
* add(new Button("East"), BorderLayout.EAST);
* add(new Button("West"), BorderLayout.WEST);
* add(new Button("Center"), BorderLayout.CENTER);
* public class BorderLayoutExample {
*
* public static void main(String[] args) throws Exception {
*
* EventQueue.invokeAndWait(() -> {
* Frame frame = new Frame("BorderLayout");
* frame.setLayout(new BorderLayout());
* frame.add(new Button("North"), BorderLayout.NORTH);
* frame.add(new Button("South"), BorderLayout.SOUTH);
* frame.add(new Button("East"), BorderLayout.EAST);
* frame.add(new Button("West"), BorderLayout.WEST);
* frame.add(new Button("Center"), BorderLayout.CENTER);
* frame.setSize(300, 300);
* frame.setVisible(true);
* });
* }
* }
* </pre></blockquote><hr>
Expand Down
36 changes: 21 additions & 15 deletions src/java.desktop/share/classes/java/awt/FlowLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,35 @@
* <li>{@link #TRAILING TRAILING}
* </ul>
* <p>
* For example, the following picture shows an applet using the flow
* For example, the following picture shows a window using the flow
* layout manager (its default layout manager) to position three buttons:
* <p>
* <img src="doc-files/FlowLayout-1.gif"
* <img src="doc-files/FlowLayout-1.png"
* ALT="Graphic of Layout for Three Buttons"
* style="margin: 7px 10px;">
* <p>
* Here is the code for this applet:
* Here is the code for this program:
*
* <hr><blockquote><pre>
* import java.awt.*;
* import java.applet.Applet;
* import java.awt.Button;
* import java.awt.EventQueue;
* import java.awt.FlowLayout;
* import java.awt.Frame;
*
* public class myButtons extends Applet {
* Button button1, button2, button3;
* public void init() {
* button1 = new Button("Ok");
* button2 = new Button("Open");
* button3 = new Button("Close");
* add(button1);
* add(button2);
* add(button3);
* }
* public class FlowLayoutExample {
*
* public static void main(String[] args) throws Exception {
*
* EventQueue.invokeAndWait(() -> {
* Frame frame = new Frame("FlowLayout");
* frame.setLayout(new FlowLayout());
* frame.add(new Button("OK"));
* frame.add(new Button("Open"));
* frame.add(new Button("Close"));
* frame.pack();
* frame.setVisible(true);
* });
* }
* }
* </pre></blockquote><hr>
* <p>
Expand Down
104 changes: 49 additions & 55 deletions src/java.desktop/share/classes/java/awt/GridBagLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,15 @@
* left-to-right container and Figure 3 shows the layout for a horizontal,
* right-to-left container.
*
* <div style="margin:0 auto;width:680px;text-align:center;font-weight:bold">
* <div style="margin:0 auto;width:850px;text-align:center;font-weight:bold">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to keep the width small as it was?

* <div style="float:left">
* <p><img src="doc-files/GridBagLayout-1.gif"
* <p><img src="doc-files/GridBagLayout-1.png"
* alt="The preceding text describes this graphic (Figure 2)."
* style="margin: 7px 10px;">
* <p>Figure 2: Horizontal, Left-to-Right
* </div>
* <div style="float:right">
* <p><img src="doc-files/GridBagLayout-2.gif"
* <p><img src="doc-files/GridBagLayout-2.png"
* alt="The preceding text describes this graphic (Figure 3)."
* style="margin: 7px 10px;">
* <p>Figure 3: Horizontal, Right-to-Left
Expand Down Expand Up @@ -277,70 +277,64 @@
* Here is the code that implements the example shown above:
*
* <hr><blockquote><pre>
* import java.awt.*;
* import java.util.*;
* import java.applet.Applet;
* import java.awt.Button;
* import java.awt.EventQueue;
* import java.awt.Frame;
* import java.awt.GridBagConstraints;
* import java.awt.GridBagLayout;
*
* public class GridBagEx1 extends Applet {
* public class GridBagLayoutExample {
*
* protected void makebutton(String name,
* GridBagLayout gridbag,
* GridBagConstraints c) {
* Button button = new Button(name);
* gridbag.setConstraints(button, c);
* add(button);
* }
* static void addButton(String name,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* static void addButton(String name,
* private static void addButton(String name,

* GridBagLayout gridbag,
* GridBagConstraints c,
* Frame frame) {
* Button button = new Button(name);
* gridbag.setConstraints(button, c);
* frame.add(button);
* }
Comment on lines +288 to +295
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The add method should indented by 4 spaces to the right.

*
* public void init() {
* GridBagLayout gridbag = new GridBagLayout();
* GridBagConstraints c = new GridBagConstraints();
* public static void main(String[] args) throws Exception {
*
* setFont(new Font("SansSerif", Font.PLAIN, 14));
* setLayout(gridbag);
* EventQueue.invokeAndWait(() -> {
* Frame frame = new Frame("GridBagLayout");
* GridBagLayout gridbag = new GridBagLayout();
* GridBagConstraints c = new GridBagConstraints();
* frame.setLayout(gridbag);
Comment on lines +297 to +303
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main method should be indented by 4 spaces to the right, it's inside GridBagLayoutExample class.

*
* c.fill = GridBagConstraints.BOTH;
* c.weightx = 1.0;
* makebutton("Button1", gridbag, c);
* makebutton("Button2", gridbag, c);
* makebutton("Button3", gridbag, c);
* c.fill = GridBagConstraints.BOTH;
* c.weightx = 1.0;
* addButton("Button1", gridbag, c, frame);
* addButton("Button2", gridbag, c, frame);
* addButton("Button3", gridbag, c, frame);
*
* c.gridwidth = GridBagConstraints.REMAINDER; //end row
* makebutton("Button4", gridbag, c);
* c.gridwidth = GridBagConstraints.REMAINDER; //end row
* addButton("Button4", gridbag, c, frame);
*
* c.weightx = 0.0; //reset to the default
* makebutton("Button5", gridbag, c); //another row
* c.weightx = 0.0; //reset to the default
* addButton("Button5", gridbag, c, frame); //another row
*
* c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
* makebutton("Button6", gridbag, c);
* c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
* addButton("Button6", gridbag, c, frame);
*
* c.gridwidth = GridBagConstraints.REMAINDER; //end row
* makebutton("Button7", gridbag, c);
* c.gridwidth = GridBagConstraints.REMAINDER; //end row
* addButton("Button7", gridbag, c, frame);
*
* c.gridwidth = 1; //reset to the default
* c.gridheight = 2;
* c.weighty = 1.0;
* makebutton("Button8", gridbag, c);
* c.gridwidth = 1; //reset to the default
* c.gridheight = 2;
* c.weighty = 1.0;
* addButton("Button8", gridbag, c, frame);
*
* c.weighty = 0.0; //reset to the default
* c.gridwidth = GridBagConstraints.REMAINDER; //end row
* c.gridheight = 1; //reset to the default
* makebutton("Button9", gridbag, c);
* makebutton("Button10", gridbag, c);
* c.weighty = 0.0; //reset to the default
* c.gridwidth = GridBagConstraints.REMAINDER; //end row
* c.gridheight = 1; //reset to the default
* addButton("Button9", gridbag, c, frame);
* addButton("Button10", gridbag, c, frame);
*
* setSize(300, 100);
* }
*
* public static void main(String args[]) {
* Frame f = new Frame("GridBag Layout Example");
* GridBagEx1 ex1 = new GridBagEx1();
*
* ex1.init();
*
* f.add("Center", ex1);
* f.pack();
* f.setSize(f.getPreferredSize());
* f.show();
* }
* frame.pack();
* frame.setVisible(true);
* });
* }
* }
* </pre></blockquote><hr>
*
Expand Down
42 changes: 26 additions & 16 deletions src/java.desktop/share/classes/java/awt/GridLayout.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2025, 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
Expand Down Expand Up @@ -32,23 +32,33 @@
* lays out a container's components in a rectangular grid.
* The container is divided into equal-sized rectangles,
* and one component is placed in each rectangle.
* For example, the following is an applet that lays out six buttons
* For example, the following is a window that lays out six buttons
* into three rows and two columns:
*
* <hr><blockquote>
* <pre>
* import java.awt.*;
* import java.applet.Applet;
* public class ButtonGrid extends Applet {
* public void init() {
* setLayout(new GridLayout(3,2));
* add(new Button("1"));
* add(new Button("2"));
* add(new Button("3"));
* add(new Button("4"));
* add(new Button("5"));
* add(new Button("6"));
* }
* import java.awt.Button;
* import java.awt.EventQueue;
* import java.awt.Frame;
* import java.awt.GridLayout;
*
* public class GridLayoutExample {
*
* public static void main(String[] args) throws Exception {
*
* EventQueue.invokeAndWait(() -> {
* Frame frame = new Frame("GridLayout");
* frame.setLayout(new GridLayout(3, 2));
* frame.add(new Button("1"));
* frame.add(new Button("2"));
* frame.add(new Button("3"));
* frame.add(new Button("4"));
* frame.add(new Button("5"));
* frame.add(new Button("6"));
* frame.setSize(200, 200);
* frame.setVisible(true);
* });
* }
* }
* </pre></blockquote><hr>
* <p>
Expand All @@ -59,13 +69,13 @@
*
* <div style="margin:0 auto;width:600px;text-align:center;font-weight:bold">
* <div style="float:left">
* <p><img SRC="doc-files/GridLayout-1.gif"
* <p><img SRC="doc-files/GridLayout-1.png"
* alt="Shows 6 buttons in rows of 2. Row 1 shows buttons 1 then 2.
* Row 2 shows buttons 3 then 4. Row 3 shows buttons 5 then 6.">
* <p>Figure 1: Horizontal, Left-to-Right
* </div>
* <div style="float:right">
* <p><img SRC="doc-files/GridLayout-2.gif"
* <p><img SRC="doc-files/GridLayout-2.png"
* alt="Shows 6 buttons in rows of 2. Row 1 shows buttons 2 then 1.
* Row 2 shows buttons 4 then 3. Row 3 shows buttons 6 then 5.">
* <p>Figure 2: Horizontal, Right-to-Left
Expand Down
42 changes: 25 additions & 17 deletions src/java.desktop/share/classes/java/awt/MediaTracker.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2025, 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
Expand Down Expand Up @@ -68,13 +68,16 @@
* Here is an example of using {@code MediaTracker}:
*
* <hr><blockquote><pre>{@code
* import java.applet.Applet;
* import java.awt.Color;
* import java.awt.EventQueue;
* import java.awt.Frame;
* import java.awt.Image;
* import java.awt.Graphics;
* import java.awt.MediaTracker;
* import java.awt.Panel;
* import java.awt.Toolkit;
*
* public class ImageBlaster extends Applet implements Runnable {
* public class MediaTrackerExample extends Panel implements Runnable {
* MediaTracker tracker;
* Image bg;
* Image anim[] = new Image[5];
Expand All @@ -84,28 +87,33 @@
* // Get the images for the background (id == 0)
* // and the animation frames (id == 1)
* // and add them to the MediaTracker
* public void init() {
* public static void main(String[] args) throws Exception {
* MediaTrackerExample mte = new MediaTrackerExample();
* EventQueue.invokeAndWait(() -> {
* Frame frame = new Frame("MediaTrackerExample");
* frame.setSize(400, 400);
* frame.add(mte);
* frame.setVisible(true);
* });
* mte.startAnimation();
* }
*
* public MediaTrackerExample() {
* Toolkit tk = Toolkit.getDefaultToolkit();
* tracker = new MediaTracker(this);
* bg = getImage(getDocumentBase(),
* "images/background.gif");
* // Note : actual images not provided as part of this code example
* bg = tk.getImage("background.gif");
* tracker.addImage(bg, 0);
* for (int i = 0; i < 5; i++) {
* anim[i] = getImage(getDocumentBase(),
* "images/anim"+i+".gif");
* anim[i] = tk.getImage("anim"+i+".gif");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* anim[i] = tk.getImage("anim"+i+".gif");
* anim[i] = tk.getImage("anim" + i + ".gif");

* tracker.addImage(anim[i], 1);
* }
* }
*
* // Start the animation thread.
* public void start() {
* public void startAnimation() {
* animator = new Thread(this);
* animator.start();
* }
*
* // Stop the animation thread.
* public void stop() {
* animator = null;
* }
* }
*
* // Run the animation thread.
* // First wait for the background image to fully load
Expand Down Expand Up @@ -152,7 +160,7 @@
* public void paint(Graphics g) {
* if ((tracker.statusAll(false) & MediaTracker.ERRORED) != 0) {
* g.setColor(Color.red);
* g.fillRect(0, 0, size().width, size().height);
* g.fillRect(0, 0, getSize().width, getSize().height);
* return;
* }
* g.drawImage(bg, 0, 0, this);
Expand Down
Binary file not shown.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The images would look better with square borders… which are left on Windows 10 only.

If it's possible to make the pixels on the rounded corners transparent or at least white, the images would look better.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.