//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; /** * Displays 5 Yukidarumas - Japanese snow person * Need To Add "extends JPanel", So That Can Use The Methods Of JPanel In Our Program. * This Is Called "Inheritance". * * @author William McDaniel Albritton */ public class Yukidarumas extends JPanel { /** * The main() method starts the program. * This method Displays A Yukidaruma, Or "Japanese Snow Person". * * @param args The commandline arguments are not used. */ public static void main(String[] args) { //create a frame with a title JFrame window = new JFrame("Five Yukidarumas (snow people)"); /* Create a panel to draw the text upon Yukidaruma is a class, which is defined below this class, in the same file */ Yukidarumas panel = new Yukidarumas(); //add the panel to the window window.setContentPane(panel); /* The default is to hide the JFrame when the user closes the window. However, 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, 500 is on the left, towards the bottom. */ window.setLocation(100, 200); //sets the size of the window: width and height window.setSize(1100,400); //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.PINK); //draw five Yukidarumas Yukidarumas.drawYukidaruma(page, 0); Yukidarumas.drawYukidaruma(page, 200); Yukidarumas.drawYukidaruma(page, 400); Yukidarumas.drawYukidaruma(page, 600); Yukidarumas.drawYukidaruma(page, 800); }//end of paintComponent() method /** * Draws a Yukidaruma * Note: Just add "+x" to the x-coordinate * to draw it anywhere vertically across the panel * Similarly, add "+y" to the y-coordinate to draw anywhere hortizonally * * @param window is the panel window * @param x is the x-coordinate of the critter */ public static void drawYukidaruma(Graphics window, Integer x) { //This Is The Snowperson's Head and Body.. window.setColor(Color.white); /*METHOD "fillOval()" Draws An Oval With Width, Height, X & Y Coordinate. X & Y Coordinates Start At The TOP LEFT Part Of The Oval. It Draws An Oval Withing The Specified Rectangual Space. Parameters: X-Coordinate, Y-Coordinate, Width, Height.*/ window.fillOval(90+x,40,120,120); window.fillOval(50+x,140,200,200); //This Is The Snowperson's Eyes window.setColor(Color.black); /*Method "fillRect()" Creates A Rectange With Width, Height, X & Y Coordinate. X & Y Coordinates Start At The TOP LEFT Part Of The Rectangel. Parameters: X-Coordinate, Y-Coordinate, Width, Height.*/ window.fillRect(120+x,80,10,10); window.fillRect(140+x,80,10,10); //This Is The Snowperson's Mouth.. window.fillRect(110+x,100,50,10); /*This Is The Snowperson's Arms.. Method "drawLine()" Draws A Line From Point (X, Y) To Point (X2, Y2). Parameters: X-Coordinate, Y-Coordinate, X2-Coordinate, Y2-Coordinate.*/ window.drawLine(60+x,200,40+x,150); window.drawLine(230+x,200,250+x,150); }//end of drawYukidaruma() method }//end of class