• Tidak ada hasil yang ditemukan

Manual CPCS 391

N/A
N/A
Protected

Academic year: 2024

Membagikan "Manual CPCS 391"

Copied!
16
0
0

Teks penuh

(1)

Manual CPCS 391

Lab 2

(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

.

(3)

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 . .

(4)

The difference between :

Horizontal line.

Vertical line.

Line with positive slope.

Red positive

Line with negative slope .

Blue negative

(5)

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

(6)

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.

(7)

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

(8)

Procedure

Open the program as we have learned in the past

Then his purview the following

(9)

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 }

}

(10)

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

(11)

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

} }

(12)

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 ?

(13)

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

} }

(14)

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…) {

……… ;}

}

(15)

else if (………….) {

for(int t…………..) {

………;

} }

// Vertical line code start here if (……….)

{

….(…………..) {

……….;

} }

(16)

Exercises

else if (………) {

for(int t=yn;t<=y1;t…) {

g.drawLine( x1, t, xn, yn );

} }

} }

Complete program…

Referensi

Dokumen terkait

public static void main(String[] args) { System.out.println(“Welcome to Java!”); } } Method Block Class Block..

public static void main (String[] args) throws IOException{. System.out.println(&#34;masukkan nama file

3 package A; public class MainA { public static void mainString args[]{ } } package A; public class TestA { private int varPrivateA ; int varDefaultA ; protected int

// // class TestPC.java // import person.*; class TestPC { public static void mainString args[] { // Initialize the eyedb package and parse the default eyedb options // on the

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class SDA13141LV2 { public static void main String[] args throws IOException {

Contoh Penggunaan Object 2 public class TesterPenggunaanObjek { public static void mainString[] args { Objek x=new Objek; Integer y=10; x.setDatay;

Statement WHILE Contoh : 10 import java.util.Scanner; public class PenghitungBeratBadan{ public static void mainString args[]{ Scanner data= new ScannerSystem.in; int urutan;

Java Application Menampilkan Tulisan //Welcome1.java Public class Welcome1 { //main method memulai eksekusi aplikasi java public static void main String args[] {