From c051ad6487ecfd6d23f484a221519f8949e8e165 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Tue, 12 Feb 2019 16:21:57 -0500 Subject: [PATCH] Added classes to draw lines in a window --- Draw.java | 38 ++++++++++++++++++++++++++++++++++++++ DrawTest.java | 25 +++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 Draw.java create mode 100644 DrawTest.java diff --git a/Draw.java b/Draw.java new file mode 100644 index 0000000..a458e8a --- /dev/null +++ b/Draw.java @@ -0,0 +1,38 @@ +//Programs/Java/JavaTutorials/Draw.java +//Matthew Ellison +// Created: 02-12-19 +//Modified: 02-12-19 +//This is a program that simply draws a line from corner to corner in a window + + +import java.awt.Graphics; +import javax.swing.JPanel; + + +public class Draw extends JPanel{ + public void paintComponent(Graphics g){ + super.paintComponent(g); //Ensures the panel displays correctly + + int width = getWidth(); //The width of the panel + int height = getHeight(); //Get the height of the panel + int stepLength = 15; //Set the length each line will be separated by + + //Draw lines all the way around the panel, one step appart each time + //Covers the bottom-left corner + for(int cnt = 0;(cnt * stepLength) < width;++cnt){ + g.drawLine(0, (stepLength * cnt), stepLength * (cnt + 1), height); + } + //Covers the bottom-right corner + for(int cnt = 0;(cnt * stepLength) < width;++cnt){ + g.drawLine(cnt * stepLength, height, width, height - (stepLength * (cnt + 1))); + } + //Covers the top-right corner + for(int cnt = 0;(cnt * stepLength) < width;++cnt){ + g.drawLine(width, height - (stepLength * cnt), width - (stepLength * (cnt + 1)), 0); + } + //Covers the top-left corner + for(int cnt = 0;(cnt * stepLength) < width;++cnt){ + g.drawLine(width - (stepLength * cnt), 0, 0, stepLength * cnt); + } + } +} \ No newline at end of file diff --git a/DrawTest.java b/DrawTest.java new file mode 100644 index 0000000..e7530c0 --- /dev/null +++ b/DrawTest.java @@ -0,0 +1,25 @@ +//Programs/Java/JavaTutorials/Draw.java +//Matthew Ellison +// Created: 02-12-19 +//Modified: 02-12-19 +//This is a program that simply draws a line from corner to corner in a window + + +import javax.swing.JFrame; + + +public class DrawTest{ + public static void main(String[] argv){ + Draw panel = new Draw(); + + //Create a new frame to hold the panel + JFrame application = new JFrame(); + + //Set the frame to exit when it is closed + application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + application.add(panel); //Add the panel to the frame + application.setSize(500, 500); //Set the size of the frame + application.setVisible(true); //Make the frame visible + } +} \ No newline at end of file