Saturday, October 24, 2015

// // 2 comments

Java GUI And Graphics Case Study Exercise Using DrawLine

The program below is the answer to Deitel's Java How to Program (9th Edition) Chapter 4 Exercise 4.1.

Question: Using loops and control statements to draw lines can lead to many interesting designs.
  1. Create the design in the left screen capture of Fig. 4.20. This design draws lines from the top-left corner, fanning them out 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 endpoint. The figure should scale accordingly as you resize the window.

  2. Modify part (a) to have lines fan out from all four corners, as shown in the right screen capture of Fig. 4.20. Lines from opposite corners should intersect along the middle.


To compile all four classes at the same time using the command prompt, use the command
javac Lines.java LinesTest.java LinesB.java LinesBTest.java  

To run LinesTest and LinesBTest, use the command
java LinesTest LinesBTest

/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 *
 * Exercise 4.1a - Lines fanning From A Corner Class
 * This Program Draws Lines fanning From A Corner of A Jframe
 *
 */ 

import java.awt.Graphics;
import javax.swing.JPanel;

public class Lines {
    public void paintComponent(Graphics g) {

        int width = getWidth(); // total width
        int height = getHeight(); // total height
        int x, y;
        int loopCount = 1;
        int xCount = 1;
        int yCount = 1;

        super.paintComponent(g); // call paintComponent to ensure the panel displays correctly
        
        x = width / 15;
        y = height / 15;

        //fan out from top left corner

        g.drawLine(0, 0, 0, height);

        while (loopCount < 15) {
            g.drawLine(0, 0, x * xCount, height - y * yCount);
            ++loopCount;
            ++xCount;
            ++yCount;
        }

    } // end method paintComponent
}//end class

Below is Class LinesTest.java to test Class Lines.java

//Exercise 4.1a - Lines fanning From A Corner Class Test
//This Program Tests Class Lines

import javax.swing.JFrame;

public class LinesTest {
    public static void main (String [] args) {
        Lines panel = new Lines(); // create a panel that contains our drawing
        JFrame application = new JFrame(); // create a new frame to hold the panel
        application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); // exit frame when closed
        application.add( panel ); // add the panel to the frame
        application.setSize( 300, 300 ); // set the size of the frame
        application.setVisible( true ); // make the frame visible
    }
}

Java GUI And Graphics Case Study program
4. 1(a) Fullscreen
Java GUI And Graphics Case Study program resizable
4.1(a) Resized
Java GUI And Graphics Case Study program resizable
4.1(a)




Below is the code for the (b) part of the question - Lines fanning out from all corners of the frame

//Exercise 4.1b - Lines fanning From All Corners
//This Program Draws Lines fanning From All Corners of a JFrame

import java.awt.Graphics;
import javax.swing.JPanel;

public class LinesB extends JPanel {
    public void paintComponent(Graphics g) {

        int width = getWidth();
        int height = getHeight();
        int x, y;
        int loopCount = 1;
        int xCount = 1;
        int yCount = 1;
     
        super.paintComponent(g);
     
        x = width / 15;
        y = height / 15;

        //fan out from top left corner

        g.drawLine(0, 0, 0, height);

        while (loopCount < 15) {
            g.drawLine(0, 0, x * xCount, height - y * yCount);
            ++loopCount;
            ++xCount;
            ++yCount;
        }

        //fan out from top right corner

        loopCount = xCount = yCount = 1;

        g.drawLine(width, 0, width, height);

        while (loopCount < 15) {
            g.drawLine(width, 0, width - x * xCount, height - y * yCount);
            ++loopCount;
            ++xCount;
            ++yCount;
        }

        //fan out from bottom left corner

        loopCount = xCount = yCount = 1;

        g.drawLine(0, height, width, height);

        while (loopCount < 15) {
            g.drawLine(0, height, width - x * xCount, height - y * yCount);
            ++loopCount;
            ++xCount;
            ++yCount;
        }

        //fan out from bottom right corner

        loopCount = xCount = yCount = 1;

        g.drawLine(width, height, 0, height);
     
        while (loopCount < 15) {
            g.drawLine(width, height, x * xCount, height - y * yCount);
            ++loopCount;
            ++xCount;
            ++yCount;
        }

    } // end method paintComponent
} //end class

Below is Class LinesBTest.java to test Class LinesB.java

//Exercise 4.1b - Lines fanning From All Corners Class Test
//This Program Tests Class LinesB

import javax.swing.JFrame;

public class LinesBTest {
    public static void main (String [] args) {
        LinesB panel = new LinesB ();
        JFrame application = new JFrame();
        application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        application.add( panel );
        application.setSize( 300, 300 );
        application.setVisible( true );
    }
}

Java GUI graphics program
4. 1(b) Fullscreen
Java GUI graphics program resizable
4.1(b) Resized
Java GUI graphics program using drawline
4.1(b)



Click here to see other answers to Java How to Program.

2 comments:

  1. The class with main in it(first program) can't be the one to "extend Jpanel". This has to be the external .java class. Also, second program creates an object LineB instead of LinesB. Other than those minor errors you have the solution. Also, than you, I wanted to see the code to study it without taking the time to write it myself. Thank you.

    ReplyDelete
    Replies
    1. Thank you very much for the corrections. I have already updated the code - works correctly now.

      Delete


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately.

Thanks.