From 486218abf0941c1408a9f20f7765eae834c61da6 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Tue, 12 Feb 2019 17:58:27 -0500 Subject: [PATCH] Added a program that draws some simple shapes --- Shapes.java | 30 ++++++++++++++++++++++++++++++ ShapesTest.java | 28 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 Shapes.java create mode 100644 ShapesTest.java diff --git a/Shapes.java b/Shapes.java new file mode 100644 index 0000000..eba6e91 --- /dev/null +++ b/Shapes.java @@ -0,0 +1,30 @@ +//Programs/Java/JavaTutorials/Shapes.java +//Matthew Ellison +// Created: 02-12-19 +//Modified: 002-12-19 +//Demonstration of drawing rectangles and ovals +//Ovals work very similarly to rectanges. In the program it draws a bounding rectangle then draws the oval within it + + +import java.awt.Graphics; +import javax.swing.JPanel; + + +public class Shapes extends JPanel{ + private int choice; //The choice of what shape to draw + //Constructor + public Shapes(int userChoice){ + choice = userChoice; + } + //Draws the shapes + public void paintComponent(Graphics g){ + super.paintComponent(g); + for(int cnt = 0;cnt < 10;++cnt){ + //Pick a shape based on what the user chose + switch(choice){ + case 1: g.drawRect(10 + (cnt * 10), 10 + (cnt * 10), 50 + (cnt * 10), 50 + (cnt * 10)); break; + case 2: g.drawOval(10 + (cnt * 10), 10 + (cnt * 10), 50 + (cnt * 10), 50 + (cnt * 10)); break; + } + } + } +} \ No newline at end of file diff --git a/ShapesTest.java b/ShapesTest.java new file mode 100644 index 0000000..7d314d6 --- /dev/null +++ b/ShapesTest.java @@ -0,0 +1,28 @@ +//Programs/Java/JavaTutorials/Shapes.java +//Matthew Ellison +// Created: 02-12-19 +//Modified: 002-12-19 +//Demonstration of drawing rectangles and ovals + + +import javax.swing.JFrame; +import javax.swing.JOptionPane; + + +public class ShapesTest{ + public static void main(String[] argv){ + //Get the user's choice + String input = JOptionPane.showInputDialog("Enter 1 to draw rectangles\nEnter2 to draw ovals"); + //Convert the input to an int + int choice = Integer.parseInt(input); + //Create the panel with the shapes drawn on it + Shapes panel = new Shapes(choice); + //Create a new frame + JFrame application = new JFrame(); + + application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + application.add(panel); + application.setSize(300, 300); + application.setVisible(true); + } +} \ No newline at end of file