nguyent063

ICS4U2 Graphics Assignments

Feb 10th, 2021
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.70 KB | None | 0 0
  1. /*
  2.  * Name: T. Nguyen
  3.  * Date: Jan. 26th, 2021
  4.  * Last updated: Feb. 3rd, 2021
  5.  * Description: A book cover made using JFrame and its associated methods.
  6.  */
  7. import java.awt.*;
  8. import javax.swing.JFrame;
  9. @SuppressWarnings("serial")
  10. public class BookCover extends JFrame
  11. {
  12.     public void init()
  13.     {
  14.         Color background = new Color(165, 165, 141);
  15.         getContentPane().setBackground(background);
  16.     }//End init()
  17.    
  18.     public void paint(Graphics g)
  19.     {
  20.         super.paint(g);
  21.  
  22.         //Declarations & Initializations
  23.         //Colors
  24.         Color titleText = new Color(107, 112, 92);
  25.         Color authorText = new Color(203, 153, 126);
  26.         Color titleRect = new Color(255, 232, 214);
  27.         Color border = new Color(221, 190, 169);
  28.         Color lightHill = new Color(183, 183, 164);
  29.         Color medHill = new Color(137, 140, 116);
  30.        
  31.         //Fonts
  32.         Font titleFont = new Font("TimesRoman", Font.ITALIC+Font.BOLD, 25);
  33.         Font authorFont = new Font("TimesRoman", Font.BOLD, 14);
  34.         Font subFont = new Font("TimesRoman", Font.ITALIC, 14);
  35.        
  36.         //FontMetrics
  37.         FontMetrics titleFM = getFontMetrics(titleFont);
  38.         FontMetrics authorFM = getFontMetrics(authorFont);
  39.        
  40.         //House coordinate arrays and ints
  41.         int[] xCoordinate = {177, 177, 171, 221, 266, 260, 260};
  42.         int[] yCoordinate = {285, 229, 229, 193, 229, 229, 309};
  43.         Polygon house = new Polygon(xCoordinate, yCoordinate, xCoordinate.length);
  44.         int x0, x1, x2, x3;
  45.        
  46.         //Strings
  47.         String title = "Java For Home", author = "By Theresa Nguyen", subtitle = "'Automate Your Life'";
  48.        
  49.         //Coordinates
  50.         x0 = (getContentPane().getWidth() - (titleFM.stringWidth(title))) / 2;
  51.         x1 = (getContentPane().getWidth() - 260) / 2;
  52.         x2 = (getContentPane().getWidth() - authorFM.stringWidth(author)) / 2;
  53.         x3 = (getContentPane().getWidth() - authorFM.stringWidth(subtitle)) / 2;
  54.        
  55.         //Cover Shapes and Strings
  56.         g.setColor(titleRect);
  57.         g.fill3DRect(x0 - 10, 80, titleFM.stringWidth(title) + 20, titleFM.getHeight() + 20, true);
  58.         g.setColor(titleText);
  59.         g.setFont(titleFont);
  60.         g.drawString(title, x0, 115);
  61.         g.setColor(titleText);
  62.         g.fill3DRect(x2 - 20, 420, 160, 50, true);
  63.         g.setFont(authorFont);
  64.         g.setColor(authorText);
  65.         g.drawString(author, x2, 450);
  66.         g.setFont(subFont);
  67.         g.setColor(titleText);
  68.         g.drawString(subtitle, x3, 495);
  69.         g.setColor(border);
  70.         g.drawLine(100, 390, 290, 390);
  71.         g.drawRect(x1, 70, 270, 460);
  72.         g.fillPolygon(house);
  73.         g.setColor(titleText);
  74.         g.fillArc(130, 280, 110, 110, 0, 180);
  75.         g.setColor(medHill);
  76.         g.fillArc(100, 300, 100, 90, 0, 180);
  77.         g.setColor(lightHill);
  78.         g.fillArc(180, 300, 110, 90, 0, 180);
  79.     }//End paint()
  80.    
  81.     public BookCover(String title)
  82.     {
  83.         super(title);
  84.        
  85.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  86.         this.setSize(400, 600);
  87.         this.setResizable(false);
  88.         this.setVisible(true);
  89.     }//End BookCover constructor
  90.    
  91.     public static void main(String[] args)
  92.     {
  93.         BookCover JavaBook = new BookCover("Book Cover");
  94.         JavaBook.init();
  95.     }//End main
  96. }//End class
  97.  
  98. /*
  99.  * Name: T. Nguyen
  100.  * Date: Feb. 2nd, 2021
  101.  * Last updated: Feb. 5th, 2021
  102.  * 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.
  103.  */
  104. import java.awt.*;
  105. import javax.swing.JFrame;
  106. @SuppressWarnings("serial")
  107. public class RandomCircles extends JFrame
  108. {
  109.     public void init()
  110.     {
  111.         getContentPane().setBackground(Color.WHITE);
  112.     }//End init()
  113.  
  114.     public void paint(Graphics g)
  115.     {
  116.         super.paint(g);
  117.         g.setColor(Color.RED);
  118.         //For loop that draws 50 circles between pauses of 100 milliseconds
  119.         for (int j = 0; j < 50; j++)
  120.         {
  121.             g.fillArc((int) (Math.random() * 600), (int) (Math.random() * 600), 20, 20, 0, 360);
  122.             try
  123.             {
  124.                 Thread.sleep(100);
  125.             }
  126.             catch (InterruptedException ex)
  127.             {
  128.                 Thread.currentThread().interrupt();
  129.             }//Pauses for 100 milliseconds, then continues to draw the next circle
  130.         }//End for loop
  131.  
  132.         //Pauses for 800 milliseconds before continuing to the JFrame clear
  133.         try
  134.         {
  135.             Thread.sleep(800);
  136.         }
  137.         catch (InterruptedException ex)
  138.         {
  139.             Thread.currentThread().interrupt();
  140.         }//End try/catch
  141.        
  142.         //Clears the JFrame and redraws the circles
  143.         getContentPane().removeAll();
  144.         repaint();
  145.     }//End paint()
  146.  
  147.     public RandomCircles (String title)
  148.     {
  149.         super(title);
  150.  
  151.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  152.         this.setSize(600, 600);
  153.         this.setResizable(false);
  154.         this.setVisible(true);
  155.     }//End RandomCircles constructor
  156.  
  157.     public static void main(String[] args)
  158.     {
  159.         RandomCircles FiftyCircles = new RandomCircles ("Random Circles");
  160.         FiftyCircles.init(); //This method is called to initialize some things.
  161.     }//end main
  162. }//end class
  163.  
  164. /*
  165.  * Name: T. Nguyen
  166.  * Date: Feb. 2nd, 2021
  167.  * Last updated: Feb. 7th, 2021
  168.  * Description: A pie graph that follows the ratios 1:2:3:4 made with g.fillArc().
  169.  */
  170. import java.awt.*; // Graphics, Color, Font
  171. import javax.swing.JFrame;
  172. @SuppressWarnings("serial")
  173. public class PieGraph extends JFrame
  174. {
  175.     //Start of original code block.
  176.     public void init()
  177.     {
  178.         getContentPane().setBackground(Color.WHITE);
  179.        
  180.     }//End init()
  181.    
  182.     public void paint(Graphics g)
  183.     {
  184.         super.paint(g);
  185.         //Declarations & Initializations
  186.         String title = "What Clothes Do I Own?", labelPink = "Shirts", labelYellow = "Pants", labelMagenta = "Sweaters", labelOrange = "Other";
  187.         Font titleFont = new Font("Arial Bold", Font.PLAIN, 25);
  188.         Font labelFont = new Font("Arial", Font.PLAIN, 16);
  189.         FontMetrics titleFM = getFontMetrics(titleFont);
  190.         int xCoordinate = 0;
  191.        
  192.         //Aligning the x-coordinate of the title to the middle of the window
  193.         xCoordinate = (getContentPane().getWidth() - titleFM.stringWidth(title)) / 2;
  194.        
  195.         //Title
  196.         g.setFont(titleFont);
  197.         g.drawString(title, xCoordinate, 80);
  198.        
  199.         //Pie Graph Arcs
  200.         g.setColor(Color.PINK);
  201.         g.fillArc(getContentPane().getWidth() / 2 - 150, getContentPane().getHeight() / 2 - 150, 300, 300, 0, 360);
  202.         g.setColor(Color.YELLOW);
  203.         g.fillArc(getContentPane().getWidth() / 2 - 150, getContentPane().getHeight() / 2 - 150, 300, 300, 0, 216);
  204.         g.setColor(Color.MAGENTA);
  205.         g.fillArc(getContentPane().getWidth() / 2 - 150, getContentPane().getHeight() / 2 - 150, 300, 300, 0, 108);
  206.         g.setColor(Color.ORANGE);
  207.         g.fillArc(getContentPane().getWidth() / 2 - 150, getContentPane().getHeight() / 2 - 150, 300, 300, 0, 36);
  208.        
  209.         //Label Lines
  210.         g.setColor(Color.BLACK);
  211.         g.drawLine(433, 280, 470, 280);
  212.         g.drawLine(291, 480, 291, 511);
  213.         g.drawLine(291, 511, 321, 511);
  214.         g.drawLine(318, 185, 318, 160);
  215.         g.drawLine(318, 160, 348, 160);
  216.         g.drawLine(153, 280, 115, 280);
  217.        
  218.         //Label Strings
  219.         g.setFont(labelFont);
  220.         g.drawString(labelPink, 326, 520);
  221.         g.drawString(labelOrange, 478, 285);
  222.         g.drawString(labelMagenta, 353, 165);
  223.         g.drawString(labelYellow, 70, 285);
  224.     }//End paint()
  225.    
  226.     public PieGraph (String title) //Application Constructor
  227.     {
  228.         super(title);
  229.         this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  230.         this.setSize(600, 700);
  231.         this.setResizable(false);
  232.         this.setVisible(true);
  233.     }//End PieGraph constructor
  234.    
  235.     public static void main(String[] args)
  236.     {
  237.         PieGraph MyClothes = new PieGraph ("Clothes I Own");
  238.         MyClothes.init();
  239.     }//end main
  240. }//end class
  241.  
  242. /*
  243.  * Name: T. Nguyen
  244.  * Date: Feb. 3rd, 2021
  245.  * Last updated: Feb. 7th, 2021
  246.  * Description: A bar graph that follows the ratios 1:2.5:3.5:3 using g.fill3DRect.
  247.  */
  248. import java.awt.*;
  249. import javax.swing.JFrame;
  250. @SuppressWarnings("serial")
  251. public class BarGraph extends JFrame
  252. {
  253.     public void init()
  254.     {
  255.         getContentPane().setBackground(Color.LIGHT_GRAY);
  256.     }//End init()
  257.    
  258.     public void paint(Graphics g)
  259.     {
  260.         super.paint(g);
  261.         //Declarations & Initializations
  262.         String title = "Election Results", label1 = "Candidate 1", label2 = "Candidate 2", label3 = "Candidate 3", label4 = "Candidate 4";
  263.         Font titleFont = new Font("Arial Bold", Font.PLAIN, 25);
  264.         Font labelFont = new Font("Arial Bold", Font.PLAIN, 12);
  265.         FontMetrics titleFM = getFontMetrics(titleFont);
  266.         int x1 = 0, x2 = 0;
  267.        
  268.         x1 = (getContentPane().getWidth() - titleFM.stringWidth(title)) / 2;
  269.         x2 = (getContentPane().getWidth() - 450) / 2;
  270.        
  271.         //Title
  272.         g.setFont(titleFont);
  273.         g.drawString(title, x1, 70);
  274.        
  275.         //Graph Axes
  276.         g.setFont(labelFont);
  277.         g.drawLine(x2 + 20, 350, x2 + 470, 350);
  278.         g.drawLine(x2 + 20, 350, x2 + 20, 100);
  279.         g.drawString("50", 15, 300);
  280.         g.drawString("100", 15, 250);
  281.         g.drawString("150", 15, 200);
  282.         g.drawString("200", 15, 150);
  283.        
  284.         //Graph Bars
  285.         g.setColor(Color.BLUE);
  286.         g.fill3DRect(x2 + 50, 300, 50, 50, true);
  287.         g.setColor(Color.GREEN);
  288.         g.fill3DRect(x2 + 150, 275, 50, 75, true);
  289.         g.setColor(Color.CYAN);
  290.         g.fill3DRect(x2 + 250, 175, 50, 175, true);
  291.         g.setColor(Color.WHITE);
  292.         g.fill3DRect(x2 + 350, 200, 50, 150, true);
  293.        
  294.         //Graph Labels
  295.         g.setFont(labelFont);
  296.         g.setColor(Color.BLACK);
  297.         g.drawString(label1, x2 + 50, 370);
  298.         g.drawString(label2, x2 + 150, 370);
  299.         g.drawString(label3, x2 + 250, 370);
  300.         g.drawString(label4, x2 + 350, 370);
  301.     }
  302.    
  303.     public BarGraph (String title)
  304.     {
  305.         super(title);
  306.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  307.         this.setSize(500, 500);
  308.         this.setResizable(false);
  309.         this.setVisible(true);
  310.     }
  311.    
  312.     public static void main(String[] args)
  313.     {
  314.         BarGraph ElectionGraph = new BarGraph ("Election Results");
  315.         ElectionGraph.init();
  316.     }//end main
  317. }//end class
  318.  
  319. /*
  320.  * Name: T. Nguyen
  321.  * Date: Feb. 4th, 2021
  322.  * Last updated: Feb. 8th, 2021
  323.  * Description: Modified code that draws stars at random different locations.
  324.  */
  325. import java.awt.*;
  326. import javax.swing.JFrame;
  327. @SuppressWarnings("serial")
  328. public class Stars extends JFrame
  329. {
  330.     final int radius = 10;
  331.  
  332.     public void init()
  333.     {
  334.         getContentPane().setBackground(Color.WHITE);
  335.     }
  336.  
  337.     public void paint(Graphics g)
  338.     {
  339.         super.paint(g);
  340.         //Declarations & Initializations
  341.         int x = 0, y = 0, red = 0, green = 0, blue = 0;
  342.  
  343.         for (int i = 0; i < 10; i++)
  344.         {
  345.             x = (int) (Math.random() * (getContentPane().getWidth() - 2 * radius)) + radius;
  346.             y = (int) (Math.random() * (getContentPane().getHeight() - 2 * radius)) + radius;
  347.  
  348.             red = (int) (Math.random() * 256);
  349.             green = (int) (Math.random() * 256);
  350.             blue = (int) (Math.random() * 256);
  351.  
  352.             int[] xPt = {x, x + 35, x + 70, x + 70, x + 35, x}, yPt = {y, y + 20, y, y + 50, y + 30, y + 50};
  353.            
  354.             g.setColor(new Color(red, green, blue));
  355.             g.drawPolygon(xPt, yPt, xPt.length);
  356.             g.fillPolygon(xPt, yPt, xPt.length);
  357.            
  358.             try
  359.             {
  360.                 Thread.sleep(100);
  361.             }
  362.             catch (InterruptedException ex)
  363.             {
  364.                 Thread.currentThread().interrupt();
  365.             }//Pauses for 100 milliseconds, then continues to draw the next shape
  366.         }
  367.     }//End paint()
  368.    
  369.     public Stars (String title)
  370.     {
  371.         super(title);
  372.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  373.         this.setSize(500, 500);
  374.         this.setResizable(false);
  375.         this.setVisible(true);
  376.     }
  377.    
  378.     public static void main(String[] args)
  379.     {
  380.         Stars bowsRandom = new Stars("Random Bows");
  381.         bowsRandom.init();
  382.     }//End main
  383. }//end class
  384.  
  385. /*
  386.  * Name: T. Nguyen
  387.  * Date: Feb. 4th, 2021
  388.  * Last updated: Feb. 8th, 2021
  389.  * Description: Draws a JFrame with different drawing methods to create a face with the title "MY FUNNY FACE".
  390.  */
  391. import java.awt.*; // Graphics, Color, Font
  392. import javax.swing.JFrame;
  393. @SuppressWarnings("serial")
  394. public class FunnyFace extends JFrame
  395. {
  396.     public void init()
  397.     {
  398.         Color background = new Color(250,138,98);
  399.         getContentPane().setBackground(background);
  400.     }//End init()
  401.    
  402.     public void paint(Graphics g)
  403.     {
  404.         super.paint(g);
  405.         //Declarations & Initializations
  406.         Color faceColour = new Color(244,233,222);
  407.         Color outlineColour = new Color(125,75,79);
  408.         Color shadeColour = new Color(244,200,175);
  409.         int xCenter = 0, titleCenter;
  410.         int[] xPts = {80, 140, 200}, yPts = {180, 140, 180};
  411.         Font titleFont = new Font("Comic Sans MS", Font.PLAIN, 16);
  412.         FontMetrics titleFM = getFontMetrics(titleFont);
  413.         String title = "MY FUNNY FACE";
  414.        
  415.         //Centering the face and title with FontMetrics
  416.         xCenter = (getContentPane().getWidth() - 200) / 2;
  417.         titleCenter = (getContentPane().getWidth() - titleFM.stringWidth(title)) / 2;
  418.        
  419.         //Title
  420.         g.setColor(Color.WHITE);
  421.         g.setFont(titleFont);
  422.         g.drawString(title, titleCenter, 270);
  423.        
  424.         //Face
  425.         g.setColor(faceColour);
  426.         g.fillArc(xCenter, 75, 200, 160, 0, 360);
  427.        
  428.         //Shading on Face
  429.         g.setColor(shadeColour);
  430.         g.fillArc(xCenter + 29, 75, 145, 50, 0, 180);
  431.         g.fillArc(115, 180, 50, 20, 0, -180);
  432.         g.fillArc(130, 120, 20, 10, 0, 360);
  433.        
  434.         //Eyes and Teeth Whites
  435.         g.setColor(Color.WHITE);
  436.         g.fillArc(75, 120, 30, 20, 0, 360);
  437.         g.fillArc(180, 120, 30, 20, 0, 360);
  438.         g.fillPolygon(xPts, yPts, xPts.length);
  439.        
  440.         //Blush
  441.         g.setColor(Color.PINK);
  442.         g.fillArc(60, 145, 30, 20, 0, 360);
  443.         g.fillArc(190, 145, 30, 20, 0, 360);
  444.        
  445.         //Teeth Lines
  446.         g.setColor(outlineColour);
  447.         g.drawLine(90, 110, 110, 120);
  448.         g.drawLine(175, 120, 195, 110);
  449.         g.drawLine(140, 180, 140, 140);
  450.         g.drawLine(120, 180, 120, 154);
  451.         g.drawLine(100, 180, 100, 167);
  452.         g.drawLine(160, 180, 160, 154);
  453.         g.drawLine(180, 180, 180, 167);
  454.        
  455.         //Triangle Lines, Eye Pupils, and Eye Outlines
  456.         g.drawPolygon(xPts, yPts, xPts.length);
  457.         g.drawArc(75, 120, 30, 20, 0, 360);
  458.         g.drawArc(180, 120, 30, 20, 0, 360);
  459.         g.fillArc(85, 120, 10, 20, 0, 360);
  460.         g.fillArc(190, 120, 10, 20, 0, 360);
  461.     }//End paint()
  462.    
  463.     public FunnyFace (String title) //Application Constructor
  464.     {
  465.         super(title); //Call JFrame Constructor
  466.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  467.         this.setSize(300, 300);
  468.         this.setResizable(false);
  469.         this.setVisible(true);
  470.     }//End FunnyFace()
  471.    
  472.     public static void main(String[] args)
  473.     {
  474.         FunnyFace MyFace = new FunnyFace ("My Funny Face!");
  475.         MyFace.init();
  476.     }//end main
  477. }//end class
Advertisement
Add Comment
Please, Sign In to add comment