Added classes to draw lines in a window

This commit is contained in:
2019-02-12 16:21:57 -05:00
parent b6400bb145
commit c051ad6487
2 changed files with 63 additions and 0 deletions

38
Draw.java Normal file
View File

@@ -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);
}
}
}

25
DrawTest.java Normal file
View File

@@ -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
}
}