Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name: T. Nguyen
- * Date: Jan. 26th, 2021
- * Last updated: Feb. 3rd, 2021
- * Description: A book cover made using JFrame and its associated methods.
- */
- import java.awt.*;
- import javax.swing.JFrame;
- @SuppressWarnings("serial")
- public class BookCover extends JFrame
- {
- public void init()
- {
- Color background = new Color(165, 165, 141);
- getContentPane().setBackground(background);
- }//End init()
- public void paint(Graphics g)
- {
- super.paint(g);
- //Declarations & Initializations
- //Colors
- Color titleText = new Color(107, 112, 92);
- Color authorText = new Color(203, 153, 126);
- Color titleRect = new Color(255, 232, 214);
- Color border = new Color(221, 190, 169);
- Color lightHill = new Color(183, 183, 164);
- Color medHill = new Color(137, 140, 116);
- //Fonts
- Font titleFont = new Font("TimesRoman", Font.ITALIC+Font.BOLD, 25);
- Font authorFont = new Font("TimesRoman", Font.BOLD, 14);
- Font subFont = new Font("TimesRoman", Font.ITALIC, 14);
- //FontMetrics
- FontMetrics titleFM = getFontMetrics(titleFont);
- FontMetrics authorFM = getFontMetrics(authorFont);
- //House coordinate arrays and ints
- int[] xCoordinate = {177, 177, 171, 221, 266, 260, 260};
- int[] yCoordinate = {285, 229, 229, 193, 229, 229, 309};
- Polygon house = new Polygon(xCoordinate, yCoordinate, xCoordinate.length);
- int x0, x1, x2, x3;
- //Strings
- String title = "Java For Home", author = "By Theresa Nguyen", subtitle = "'Automate Your Life'";
- //Coordinates
- x0 = (getContentPane().getWidth() - (titleFM.stringWidth(title))) / 2;
- x1 = (getContentPane().getWidth() - 260) / 2;
- x2 = (getContentPane().getWidth() - authorFM.stringWidth(author)) / 2;
- x3 = (getContentPane().getWidth() - authorFM.stringWidth(subtitle)) / 2;
- //Cover Shapes and Strings
- g.setColor(titleRect);
- g.fill3DRect(x0 - 10, 80, titleFM.stringWidth(title) + 20, titleFM.getHeight() + 20, true);
- g.setColor(titleText);
- g.setFont(titleFont);
- g.drawString(title, x0, 115);
- g.setColor(titleText);
- g.fill3DRect(x2 - 20, 420, 160, 50, true);
- g.setFont(authorFont);
- g.setColor(authorText);
- g.drawString(author, x2, 450);
- g.setFont(subFont);
- g.setColor(titleText);
- g.drawString(subtitle, x3, 495);
- g.setColor(border);
- g.drawLine(100, 390, 290, 390);
- g.drawRect(x1, 70, 270, 460);
- g.fillPolygon(house);
- g.setColor(titleText);
- g.fillArc(130, 280, 110, 110, 0, 180);
- g.setColor(medHill);
- g.fillArc(100, 300, 100, 90, 0, 180);
- g.setColor(lightHill);
- g.fillArc(180, 300, 110, 90, 0, 180);
- }//End paint()
- public BookCover(String title)
- {
- super(title);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(400, 600);
- this.setResizable(false);
- this.setVisible(true);
- }//End BookCover constructor
- public static void main(String[] args)
- {
- BookCover JavaBook = new BookCover("Book Cover");
- JavaBook.init();
- }//End main
- }//End class
- /*
- * Name: T. Nguyen
- * Date: Feb. 2nd, 2021
- * Last updated: Feb. 5th, 2021
- * Description: A JFrame window where 50 circles are drawn in random positions all over the window. After all 50 are drawn, the window is wiped and the circles are redrawn.
- */
- import java.awt.*;
- import javax.swing.JFrame;
- @SuppressWarnings("serial")
- public class RandomCircles extends JFrame
- {
- public void init()
- {
- getContentPane().setBackground(Color.WHITE);
- }//End init()
- public void paint(Graphics g)
- {
- super.paint(g);
- g.setColor(Color.RED);
- //For loop that draws 50 circles between pauses of 100 milliseconds
- for (int j = 0; j < 50; j++)
- {
- g.fillArc((int) (Math.random() * 600), (int) (Math.random() * 600), 20, 20, 0, 360);
- try
- {
- Thread.sleep(100);
- }
- catch (InterruptedException ex)
- {
- Thread.currentThread().interrupt();
- }//Pauses for 100 milliseconds, then continues to draw the next circle
- }//End for loop
- //Pauses for 800 milliseconds before continuing to the JFrame clear
- try
- {
- Thread.sleep(800);
- }
- catch (InterruptedException ex)
- {
- Thread.currentThread().interrupt();
- }//End try/catch
- //Clears the JFrame and redraws the circles
- getContentPane().removeAll();
- repaint();
- }//End paint()
- public RandomCircles (String title)
- {
- super(title);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(600, 600);
- this.setResizable(false);
- this.setVisible(true);
- }//End RandomCircles constructor
- public static void main(String[] args)
- {
- RandomCircles FiftyCircles = new RandomCircles ("Random Circles");
- FiftyCircles.init(); //This method is called to initialize some things.
- }//end main
- }//end class
- /*
- * Name: T. Nguyen
- * Date: Feb. 2nd, 2021
- * Last updated: Feb. 7th, 2021
- * Description: A pie graph that follows the ratios 1:2:3:4 made with g.fillArc().
- */
- import java.awt.*; // Graphics, Color, Font
- import javax.swing.JFrame;
- @SuppressWarnings("serial")
- public class PieGraph extends JFrame
- {
- //Start of original code block.
- public void init()
- {
- getContentPane().setBackground(Color.WHITE);
- }//End init()
- public void paint(Graphics g)
- {
- super.paint(g);
- //Declarations & Initializations
- String title = "What Clothes Do I Own?", labelPink = "Shirts", labelYellow = "Pants", labelMagenta = "Sweaters", labelOrange = "Other";
- Font titleFont = new Font("Arial Bold", Font.PLAIN, 25);
- Font labelFont = new Font("Arial", Font.PLAIN, 16);
- FontMetrics titleFM = getFontMetrics(titleFont);
- int xCoordinate = 0;
- //Aligning the x-coordinate of the title to the middle of the window
- xCoordinate = (getContentPane().getWidth() - titleFM.stringWidth(title)) / 2;
- //Title
- g.setFont(titleFont);
- g.drawString(title, xCoordinate, 80);
- //Pie Graph Arcs
- g.setColor(Color.PINK);
- g.fillArc(getContentPane().getWidth() / 2 - 150, getContentPane().getHeight() / 2 - 150, 300, 300, 0, 360);
- g.setColor(Color.YELLOW);
- g.fillArc(getContentPane().getWidth() / 2 - 150, getContentPane().getHeight() / 2 - 150, 300, 300, 0, 216);
- g.setColor(Color.MAGENTA);
- g.fillArc(getContentPane().getWidth() / 2 - 150, getContentPane().getHeight() / 2 - 150, 300, 300, 0, 108);
- g.setColor(Color.ORANGE);
- g.fillArc(getContentPane().getWidth() / 2 - 150, getContentPane().getHeight() / 2 - 150, 300, 300, 0, 36);
- //Label Lines
- g.setColor(Color.BLACK);
- g.drawLine(433, 280, 470, 280);
- g.drawLine(291, 480, 291, 511);
- g.drawLine(291, 511, 321, 511);
- g.drawLine(318, 185, 318, 160);
- g.drawLine(318, 160, 348, 160);
- g.drawLine(153, 280, 115, 280);
- //Label Strings
- g.setFont(labelFont);
- g.drawString(labelPink, 326, 520);
- g.drawString(labelOrange, 478, 285);
- g.drawString(labelMagenta, 353, 165);
- g.drawString(labelYellow, 70, 285);
- }//End paint()
- public PieGraph (String title) //Application Constructor
- {
- super(title);
- this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
- this.setSize(600, 700);
- this.setResizable(false);
- this.setVisible(true);
- }//End PieGraph constructor
- public static void main(String[] args)
- {
- PieGraph MyClothes = new PieGraph ("Clothes I Own");
- MyClothes.init();
- }//end main
- }//end class
- /*
- * Name: T. Nguyen
- * Date: Feb. 3rd, 2021
- * Last updated: Feb. 7th, 2021
- * Description: A bar graph that follows the ratios 1:2.5:3.5:3 using g.fill3DRect.
- */
- import java.awt.*;
- import javax.swing.JFrame;
- @SuppressWarnings("serial")
- public class BarGraph extends JFrame
- {
- public void init()
- {
- getContentPane().setBackground(Color.LIGHT_GRAY);
- }//End init()
- public void paint(Graphics g)
- {
- super.paint(g);
- //Declarations & Initializations
- String title = "Election Results", label1 = "Candidate 1", label2 = "Candidate 2", label3 = "Candidate 3", label4 = "Candidate 4";
- Font titleFont = new Font("Arial Bold", Font.PLAIN, 25);
- Font labelFont = new Font("Arial Bold", Font.PLAIN, 12);
- FontMetrics titleFM = getFontMetrics(titleFont);
- int x1 = 0, x2 = 0;
- x1 = (getContentPane().getWidth() - titleFM.stringWidth(title)) / 2;
- x2 = (getContentPane().getWidth() - 450) / 2;
- //Title
- g.setFont(titleFont);
- g.drawString(title, x1, 70);
- //Graph Axes
- g.setFont(labelFont);
- g.drawLine(x2 + 20, 350, x2 + 470, 350);
- g.drawLine(x2 + 20, 350, x2 + 20, 100);
- g.drawString("50", 15, 300);
- g.drawString("100", 15, 250);
- g.drawString("150", 15, 200);
- g.drawString("200", 15, 150);
- //Graph Bars
- g.setColor(Color.BLUE);
- g.fill3DRect(x2 + 50, 300, 50, 50, true);
- g.setColor(Color.GREEN);
- g.fill3DRect(x2 + 150, 275, 50, 75, true);
- g.setColor(Color.CYAN);
- g.fill3DRect(x2 + 250, 175, 50, 175, true);
- g.setColor(Color.WHITE);
- g.fill3DRect(x2 + 350, 200, 50, 150, true);
- //Graph Labels
- g.setFont(labelFont);
- g.setColor(Color.BLACK);
- g.drawString(label1, x2 + 50, 370);
- g.drawString(label2, x2 + 150, 370);
- g.drawString(label3, x2 + 250, 370);
- g.drawString(label4, x2 + 350, 370);
- }
- public BarGraph (String title)
- {
- super(title);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(500, 500);
- this.setResizable(false);
- this.setVisible(true);
- }
- public static void main(String[] args)
- {
- BarGraph ElectionGraph = new BarGraph ("Election Results");
- ElectionGraph.init();
- }//end main
- }//end class
- /*
- * Name: T. Nguyen
- * Date: Feb. 4th, 2021
- * Last updated: Feb. 8th, 2021
- * Description: Modified code that draws stars at random different locations.
- */
- import java.awt.*;
- import javax.swing.JFrame;
- @SuppressWarnings("serial")
- public class Stars extends JFrame
- {
- final int radius = 10;
- public void init()
- {
- getContentPane().setBackground(Color.WHITE);
- }
- public void paint(Graphics g)
- {
- super.paint(g);
- //Declarations & Initializations
- int x = 0, y = 0, red = 0, green = 0, blue = 0;
- for (int i = 0; i < 10; i++)
- {
- x = (int) (Math.random() * (getContentPane().getWidth() - 2 * radius)) + radius;
- y = (int) (Math.random() * (getContentPane().getHeight() - 2 * radius)) + radius;
- red = (int) (Math.random() * 256);
- green = (int) (Math.random() * 256);
- blue = (int) (Math.random() * 256);
- int[] xPt = {x, x + 35, x + 70, x + 70, x + 35, x}, yPt = {y, y + 20, y, y + 50, y + 30, y + 50};
- g.setColor(new Color(red, green, blue));
- g.drawPolygon(xPt, yPt, xPt.length);
- g.fillPolygon(xPt, yPt, xPt.length);
- try
- {
- Thread.sleep(100);
- }
- catch (InterruptedException ex)
- {
- Thread.currentThread().interrupt();
- }//Pauses for 100 milliseconds, then continues to draw the next shape
- }
- }//End paint()
- public Stars (String title)
- {
- super(title);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(500, 500);
- this.setResizable(false);
- this.setVisible(true);
- }
- public static void main(String[] args)
- {
- Stars bowsRandom = new Stars("Random Bows");
- bowsRandom.init();
- }//End main
- }//end class
- /*
- * Name: T. Nguyen
- * Date: Feb. 4th, 2021
- * Last updated: Feb. 8th, 2021
- * Description: Draws a JFrame with different drawing methods to create a face with the title "MY FUNNY FACE".
- */
- import java.awt.*; // Graphics, Color, Font
- import javax.swing.JFrame;
- @SuppressWarnings("serial")
- public class FunnyFace extends JFrame
- {
- public void init()
- {
- Color background = new Color(250,138,98);
- getContentPane().setBackground(background);
- }//End init()
- public void paint(Graphics g)
- {
- super.paint(g);
- //Declarations & Initializations
- Color faceColour = new Color(244,233,222);
- Color outlineColour = new Color(125,75,79);
- Color shadeColour = new Color(244,200,175);
- int xCenter = 0, titleCenter;
- int[] xPts = {80, 140, 200}, yPts = {180, 140, 180};
- Font titleFont = new Font("Comic Sans MS", Font.PLAIN, 16);
- FontMetrics titleFM = getFontMetrics(titleFont);
- String title = "MY FUNNY FACE";
- //Centering the face and title with FontMetrics
- xCenter = (getContentPane().getWidth() - 200) / 2;
- titleCenter = (getContentPane().getWidth() - titleFM.stringWidth(title)) / 2;
- //Title
- g.setColor(Color.WHITE);
- g.setFont(titleFont);
- g.drawString(title, titleCenter, 270);
- //Face
- g.setColor(faceColour);
- g.fillArc(xCenter, 75, 200, 160, 0, 360);
- //Shading on Face
- g.setColor(shadeColour);
- g.fillArc(xCenter + 29, 75, 145, 50, 0, 180);
- g.fillArc(115, 180, 50, 20, 0, -180);
- g.fillArc(130, 120, 20, 10, 0, 360);
- //Eyes and Teeth Whites
- g.setColor(Color.WHITE);
- g.fillArc(75, 120, 30, 20, 0, 360);
- g.fillArc(180, 120, 30, 20, 0, 360);
- g.fillPolygon(xPts, yPts, xPts.length);
- //Blush
- g.setColor(Color.PINK);
- g.fillArc(60, 145, 30, 20, 0, 360);
- g.fillArc(190, 145, 30, 20, 0, 360);
- //Teeth Lines
- g.setColor(outlineColour);
- g.drawLine(90, 110, 110, 120);
- g.drawLine(175, 120, 195, 110);
- g.drawLine(140, 180, 140, 140);
- g.drawLine(120, 180, 120, 154);
- g.drawLine(100, 180, 100, 167);
- g.drawLine(160, 180, 160, 154);
- g.drawLine(180, 180, 180, 167);
- //Triangle Lines, Eye Pupils, and Eye Outlines
- g.drawPolygon(xPts, yPts, xPts.length);
- g.drawArc(75, 120, 30, 20, 0, 360);
- g.drawArc(180, 120, 30, 20, 0, 360);
- g.fillArc(85, 120, 10, 20, 0, 360);
- g.fillArc(190, 120, 10, 20, 0, 360);
- }//End paint()
- public FunnyFace (String title) //Application Constructor
- {
- super(title); //Call JFrame Constructor
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(300, 300);
- this.setResizable(false);
- this.setVisible(true);
- }//End FunnyFace()
- public static void main(String[] args)
- {
- FunnyFace MyFace = new FunnyFace ("My Funny Face!");
- MyFace.init();
- }//end main
- }//end class
Advertisement
Add Comment
Please, Sign In to add comment