Java Programming Question

Q.  Using loops and control statements to draw lines can lead to many interesting designs. Create the
design in the left screen capture of Figure. This design draws lines from the top-left corner, fanning out
the lines until they cover the upper-left half of the panel. One approach is to divide the width and height
into an equal number of steps (we found 15 steps worked well). The first endpoint of a line will always be
in the top-left corner (0, 0). The second endpoint can be found by starting at the bottom-left corner and
moving up one vertical step and right one horizontal step. Draw a line between the two endpoints.
Continue moving up and to the right one step to find each successive end-point. The figure should scale
accordingly as you resize the window.
Java Code:
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Graphics;
public class q2ans extends JPanel
public static void main(String args[])
q2ans panel = new q2ans(); //creates a panel
JFrame frameapp = new JFrame(); // create a new frame
frameapp.setSize(500, 500); //size of frame
public void paintComponent(Graphics g)
int x, y;
int total_lines = 15;
int width = getWidth();
int height = getHeight();
int change_ratio_x = width / total_lines;
int change_ratio_y = height / total_lines;
x = width;
y = 0;
for(int i=1; i<=15; i++)
x = width – i*change_ratio_x;
y = i*change_ratio_y;


Popular posts from this blog

MATLAB code for Circular Convolution using Matrix method

Positive number pipe in angular 2+