//used to make a frame, which is a window with a title and a border import javax.swing.JFrame; //used to make a panel, which is a place to draw text and shapes import javax.swing.JPanel; //has methods to set fonts, draw text, set colors, and draw shapes import java.awt.Graphics; //has the colors that we can use import java.awt.Color; //used to create fonts import java.awt.Font; /** * This panel displays Greetings In Different Languages & Colors.. * Need To Add "extends JPanel", So That Can Use The Methods Of JPanel In Our Program. * This Is Called "Inheritance", which is a way of re-using software (code) * We will learn more about inheritance later in this class! * * @author William McDaniel Albritton */ public class HelloWorldPanel extends JPanel { /** * The main() method starts the program. * This method Displays Greetings In Different Languages and Colors. * * @param args The commandline arguments are not used. */ public static void main(String[] args) { //create a frame with a title JFrame window = new JFrame("This is the title to the Hello World frame."); /* Create a panel to draw the text upon HelloWorldPanel is a class, which is used to create objects HelloWorldPanel is also the name of this file*/ HelloWorldPanel panel = new HelloWorldPanel(); //add the panel to the window window.setContentPane(panel); /* The default is to hide the JFrame when the user closes the window. owever, we want the window to stop running, when we close the window!*/ window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /*Sets the location of the window, where x is the first parameter, and y is the second parameter. 0,0 is on the left (x) and at the top (y) of the screen. 500, 0 is towards the middle of the screen at the top. 0, 200 is on the left, towards the bottom. 500, 200 is roughly in the middle of the screen*/ window.setLocation(500, 200); //sets the size of the window: width and height window.setSize(600,300); //if you do not set this parameter to "true", the window will be invisible! window.setVisible(true); }//end of main() method /** * The paintComponent() method draws the content of the panel. * It draws the Greetings In Different Languages & Colors.. * * @param page is the object on which we use the drawing methods */ public void paintComponent(Graphics page) { /* Calls the paintComponent method from the superclass JPanel. Initializes variables in superclass JPanel to default values. If not included, the setBackground() method will not work. */ super.paintComponent(page); /* "this" Is An Object That Represents The JPanel Itself. Method "setBackground()" Sets The Background Color Of The JPanel. Use Object "Color.colorName" To Set The Colors. See The Java API For A List Of Colors.*/ this.setBackground(Color.CYAN); /* "page" Is aN Object That Represents The JPanel's Display Area. "setColor()" Is A Method That Sets The Color Of Any Object Drawn On The Page..*/ page.setColor(Color.BLUE); /* Method "drawString()" Draws A String At A Specified X and Y Coordinate.. Parameters: String, X-Coordinate, Y-Coordinate. X and Y Start At (0,0) ON The Top Left Corner Of The JPanel.. */ page.drawString ("Hello, World!", 100, 50); //make a larger font Font largerFont = new Font("SansSerif", Font.BOLD, 24); //set the font, color, and draw a string page.setFont(largerFont); page.setColor(Color.RED); page.drawString ("Ohaiyou gozaimasu!", 100, 120); //make a computer font Font computerFont = new Font("Monospaced", Font.PLAIN, 48); //set the font, color, and draw a string page.setFont(computerFont); page.setColor(Color.ORANGE); page.drawString ("Salamat Pagi!", 100, 200); }//end of paintComponent() method }//end of HelloWorldPanel class