Manual CPCS 391
Lab 2
Introduction
One of the most important properties of a straight line is in how it angles away from the horizontal. This concept is reflected in
something called the "slope" of the line
.
Objective
We are going to write a single intelligent We are going to write a single intelligent program that will draw different types of program that will draw different types of
lines i.e.
lines i.e.
Horizontal line Horizontal line
Vertical line Vertical line
Line with positive slope
Line with positive slope
Line with negative slope
Line with negative slope . .
The difference between :
Horizontal line.
Vertical line.
Line with positive slope.
Red positive
Line with negative slope .
Blue negative
In case if you want to draw a Horizontal line the condition you might look is
When ( Y1==Yn ).
In case
In case if you want to draw a Vertical the condition if you want to draw a Vertical the condition you might look is
you might look is When ( X1==
When ( X1== Xn Xn ) )
In case
In case if X1= if X1= Xn Xn and Y1= and Y1= Yn Yn then you will draw a then you will draw a pixel.
pixel.
Overview
In case
In case if X1 not equal if X1 not equal Xn Xn and Y1 not equal and Y1 not equal Yn Yn then you have to calculate the slope of the
then you have to calculate the slope of the line by using the formula
line by using the formula
M= (Yn
M= (Yn - - Y1 )/ ( Y1 )/ ( Xn Xn – – X1) X1) where Xn
where Xn - - X1 should never be equal to zero. X1 should never be equal to zero.
Based on the M + or
Based on the M + or – – draw the line. draw the line.
Prelab Activities
htm . slope /
modules /
com . purplemath .
www ://
http
do / J2PrinterWorks /
Software /
com .
wildcrest .
www ://
http
Horizon /
j2printerworks /
wildcrest /
com / api / cumentation
html . talLine
http://www.java-examples.com/draw-line-applet- window-example
Procedure
Open the program as we have learned in the past
Then his purview the following
Example
import javax.swing.*;
import java.awt.*;
public class OnePrgLine extends JPanel{
public static void main(String[] args) { int x1,y1,xn,yn;
JFrame frame = new JFrame("MyFrame"); // Create a frame
x1= Integer.parseInt(JOptionPane.showInputDialog("Enter X1"));
y1= Integer.parseInt(JOptionPane.showInputDialog("Enter Y1"));
xn= Integer.parseInt(JOptionPane.showInputDialog("Enter Xn"));
yn= Integer.parseInt(JOptionPane.showInputDialog("Enter Yn"));
drawFig dr= new drawFig(x1,y1,xn,yn); // Creating Object of Class drawFig frame.add( dr); // Adding to the frame
frame.setSize(400, 300); // Set the frame size frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); // Display the frame }
}
class drawFig extends JPanel { int x1,y1,xn,yn;
public drawFig(int x,int y,int x1,int y1) {
this.x1=x;
this.y1=y;
this.xn=x1;
this.yn=y1;
}
@Override
public void paintComponent( Graphics g )
{ super.paintComponent( g ); // call superclass's paint method g.setColor( Color.BLUE ); // Sect the Color of the figures
// horizontal line code start here if (y1==yn && xn>x1)
{ for(int t=x1;t<=xn;t++) { g.drawLine( t, y1, t, yn ); } }
else if (y1==yn && xn<x1) {
for(int t=xn;t<=x1;t--) {
g.drawLine( t, y1, t, yn );
} }
// horizontal line code finish here // Vertical line code start here
if (x1==xn && yn>y1) {
for(int t=y1;t<=yn;t++) {
g.drawLine( x1, t, xn, yn );
} }
else if (x1==xn && yn<y1) {
for(int t=yn;t<=y1;t--) {
g.drawLine( x1, t, xn, yn );
} }
// Vertical line code finish here
// You will continue coding the remaining part of the program }
}
Why not draw a line when x1 is not equal to xn and y1 is not equal to yn ?
Exercises
import javax.swing.*;
import java.awt.*;
public class OnePrgLine extends JPanel{
public static void main(String[] args) { int x1,y1,xn,yn;
JFrame frame = new JFrame("MyFrame"); // Create a frame
x1= Integer.parseInt(JOptionPane.showInputDialog("Enter X1"));
y1= Integer.parseInt(JOptionPane.showInputDialog("Enter Y1"));
xn= Integer.parseInt(JOptionPane.showInputDialog("Enter Xn"));
yn= Integer.parseInt(JOptionPane.showInputDialog("Enter Yn"));
drawFig dr= new drawFig(x1,y1,xn,yn);
frame.add( dr);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} }
class drawFig extends JPanel { int x1,y1,xn,yn;
public drawFig(int x,int y,int x1,int y1) {
this.x1=x;
this.y1=y;
this.xn=x1;
this.yn=y1;
}
@Override
public void paintComponent( Graphics g )
{ super.paintComponent( g ); // call superclass's paint method g.setColor( Color.BLUE ); // Sect the Color of the figures
// horizontal line code start here if (………)
{ …(int t=…;t<=…;t…) {
……… ;}
}
else if (………….) {
for(int t…………..) {
………;
} }
// Vertical line code start here if (……….)
{
….(…………..) {
……….;
} }
Exercises
else if (………) {
for(int t=yn;t<=y1;t…) {
g.drawLine( x1, t, xn, yn );
} }
} }
Complete program…