Question

In: Computer Science

Consider the program which you can find on Moodle that draws random lines. In this assignment,...

Consider the program which you can find on Moodle that draws random lines. In this assignment, you will extend that program to draw triangles and quadrilaterals. Create classes Traingle and Quadrilaterals. Declare a constructor in each class as follows:

public Triangle (int x1, int y1, int x2, int y2, int x3, int y3, Color color)

public Quadrilateral (int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, Color color)

Each class of these two should include an implementation of the method:

public void draw(Graphics g)

Edit the constructor of DrawPanel to generate random objects of class Triangle and Quadrilateral. When your program is run, these objects should be drawn in the panel. The color of the lines of a shape should be the same. To test your program, draw three triangles and three quadrilaterals. Use a different color for each shape.

-----------------------------------------------------------------------------------------------------------------------------------

package GraphicsExample;

// Fig. 8.19: TestDraw.java
// Creating a JFrame to display a DrawPanel.
import javax.swing.JFrame;

public class TestDraw
{
public static void main(String[] args)
{
DrawPanel panel = new DrawPanel();
JFrame app = new JFrame();
  
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.add(panel);
app.setSize(600, 600);
app.setVisible(true);
}
} // end class TestDraw
-----------------------------------------------------------------------------------------------

package GraphicsExample;

// Fig. 8.17: MyLine.java
// MyLine class represents a line.
import java.awt.Color;
import java.awt.Graphics;

public class MyLine
{
private int x1; // x-coordinate of first endpoint
private int y1; // y-coordinate of first endpoint
private int x2; // x-coordinate of second endpoint
private int y2; // y-coordinate of second endpoint
private Color color; // color of this line

// constructor with input values
public MyLine(int x1, int y1, int x2, int y2, Color color)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
}

// Actually draws the line
public void draw(Graphics g)
{
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
} // end class MyLine
--------------------------------------------------------------------------------------------------------------------------------

package GraphicsExample;

// Fig. 8.18: DrawPanel.java
// Program that uses class MyLine
// to draw random lines.
import java.awt.Color;
import java.awt.Graphics;
import java.security.SecureRandom;
import javax.swing.JPanel;

public class DrawPanel extends JPanel
{
private SecureRandom randomNumbers = new SecureRandom();   
private MyLine[] lines; // array on lines

// constructor, creates a panel with random shapes
public DrawPanel()
{
setBackground(Color.WHITE);
  
lines = new MyLine[5 + randomNumbers.nextInt(5)];

// create lines
for (int count = 0; count < lines.length; count++)
{
// generate random coordinates
int x1 = randomNumbers.nextInt(300);
int y1 = randomNumbers.nextInt(300);
int x2 = randomNumbers.nextInt(300);
int y2 = randomNumbers.nextInt(300);

// generate a random color
Color color = new Color(randomNumbers.nextInt(256),
randomNumbers.nextInt(256), randomNumbers.nextInt(256));

// add the line to the list of lines to be displayed
lines[count] = new MyLine(x1, y1, x2, y2, color);
}
}

// for each shape array, draw the individual shapes
public void paintComponent(Graphics g)
{
super.paintComponent(g);

// draw the lines
for (MyLine line : lines)
line.draw(g);
}
} // end class DrawPanel

Solutions

Expert Solution

The code written in bold is edited code ....

package GraphicsExample;

// Fig. 8.19: TestDraw.java
// Creating a JFrame to display a DrawPanel.
import javax.swing.JFrame;

public class TestDraw
{
public static void main(String[] args)
{
DrawPanel panel = new DrawPanel();
JFrame app = new JFrame();
  
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.add(panel);
app.setSize(600, 600);
app.setVisible(true);
}
} // end class TestDraw
-----------------------------------------------------------------------------------------------

package GraphicsExample;

// Fig. 8.17: MyLine.java
// MyLine class represents a line.
import java.awt.Color;
import java.awt.Graphics;

public class MyLine
{
private int x1; // x-coordinate of first endpoint
private int y1; // y-coordinate of first endpoint
private int x2; // x-coordinate of second endpoint
private int y2; // y-coordinate of second endpoint
private Color color; // color of this line

// constructor with input values
public MyLine(int x1, int y1, int x2, int y2, Color color)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
}

// Actually draws the line
public void draw(Graphics g)
{
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
} // end class MyLine

public class Triangle{

private int x1; // x-coordinate of first point
private int y1; // y-coordinate of first point
private int x2; // x-coordinate of second point
private int y2; // y-coordinate of second point

private int x3 // x-coordinate of third point
private int y3; // y-coordinate of third point
private Color color; // color of this traingle

// constructor with input values
public Triangle(int x1, int y1, int x2, int y2,int x3,int y3, Color color)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;

this.x3 = x3;
this.y3 = y3;


this.color = color;
}

// Actually draws the Triangle
public void draw(Graphics g)
{
g.setColor(color);

MyLine side1=new MyLine(x1,y1,x2,y2,color);

side1.draw(g);

MyLine side2=new MyLine(x2,y2,x3,y3,color);

side2.draw(g);

MyLine side3=new MyLine(x3,y3,x1,y1,color);

side3.draw(g);


}

}

//---------------------------------------------------------------

public class Quadrilateral{

private int x1; // x-coordinate of first point
private int y1; // y-coordinate of first point
private int x2; // x-coordinate of second point
private int y2; // y-coordinate of second point

private int x3 // x-coordinate of third point
private int y3; // y-coordinate of third point
private int x4 // x-coordinate of fourth point
private int y4; // y-coordinate of fourth point

private Color color; // color of this traingle

// constructor with input values
public Quadrilateral(int x1, int y1, int x2, int y2,int x3,int y3,int x4,int y4 Color color)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;

this.x3 = x3;
this.y3 = y3;

this.x4=x4;

this.y4=y4;


this.color = color;
}

// Actually draws the Triangle
public void draw(Graphics g)
{
g.setColor(color);

MyLine side1=new MyLine(x1,y1,x2,y2,color);

side1.draw(g);

MyLine side2=new MyLine(x2,y2,x3,y3,color);

side2.draw(g);

MyLine side3=new MyLine(x3,y3,x4,y4,color);

side3.draw(g);

MyLine side4=new MyLine(x4,y4,x1,y1,color);

side4.draw(g);


}

}


--------------------------------------------------------------------------------------------------------------------------------

package GraphicsExample;

// Fig. 8.18: DrawPanel.java
// Program that uses class MyLine
// to draw random lines.
import java.awt.Color;
import java.awt.Graphics;
import java.security.SecureRandom;
import javax.swing.JPanel;

public class DrawPanel extends JPanel
{
private SecureRandom randomNumbers = new SecureRandom();   
//private MyLine[] lines; // array on lines

private Triangle[] triangle; //array of triangles

private Quadrilateral[] quadrilateral; //array of quadrilateral

public DrawPanel()
{
setBackground(Color.WHITE);

//constructing triangle array

triangle = new Triangle[5 + randomNumbers.nextInt(5)]; //for testing make size as 3

// create triangles
for (int count = 0; count < triangle.length; count++)
{
// generate random coordinates
int x1 = randomNumbers.nextInt(300);
int y1 = randomNumbers.nextInt(300);
int x2 = randomNumbers.nextInt(300);
int y2 = randomNumbers.nextInt(300);

int x3=randomNumbers.nextInt(300);

int y3=randomNumbers.nextInt(300);

// generate a random color
Color color = new Color(randomNumbers.nextInt(256),
randomNumbers.nextInt(256), randomNumbers.nextInt(256));

// add the triangle to the list of triangles to be displayed
triangle[count] = new Triangle(x1, y1, x2, y2,x3,y3 ,color);

}

//constructing quadrilateral  array

quadrilateral = new Quadrilateral[5 + randomNumbers.nextInt(5)]; //for testing make size 3

// create quadrilateral
for (int count = 0; count < triangle.length; count++)
{
// generate random coordinates
int x1 = randomNumbers.nextInt(300);
int y1 = randomNumbers.nextInt(300);
int x2 = randomNumbers.nextInt(300);
int y2 = randomNumbers.nextInt(300);

int x3=randomNumbers.nextInt(300);

int y3=randomNumbers.nextInt(300);

int x4=randomNumbers.nextInt(300);

int y4=randomNumbers.nextInt(300);

// generate a random color
Color color = new Color(randomNumbers.nextInt(256),
randomNumbers.nextInt(256), randomNumbers.nextInt(256));

// add the quadrilateral to the list of quadrilaterals to be displayed
quadrilateral[count] = new Quadrilateral(x1, y1, x2, y2,x3,y3 ,x4,y4,color);

}

// constructor, creates a panel with random shapes


}

// for each shape array, draw the individual shapes
public void paintComponent(Graphics g)
{
super.paintComponent(g);

// draw the triangles
for (Triangle t : triangle)
t.draw(g);

// draw the quadrilaterals
for (Quadrilateral q : quadrilateral)
q.draw(g);


}
} // end class DrawPanel


Related Solutions

On Moodle, you will find a file labelled “Data for Question 4 Assignment 1”. It contains...
On Moodle, you will find a file labelled “Data for Question 4 Assignment 1”. It contains data on past students in this course. Under Midterm is information on whether past studentsgot an A grade (A−, A, A+) an F or D grade (D is a passing grade but most students need aC− for it to count towards their program) or Other (any grade in between). Under FinalExam is information on whether students got a D or F grade or anything...
Consider a sealed-bid auction in which the seller draws one of the N bids at random....
Consider a sealed-bid auction in which the seller draws one of the N bids at random. The buyer whose bid was drawn wins the auction and pays the amount bid. Assume that buyer valuations follow a uniform(0,1) distribution. 1. What is the symmetric equilibrium bidding strategy b(v)? 2.What is the seller’s expected revenue? 3.Why doesn’t this auction pay the seller the same revenue as the four standard auctions? That is, why doesn’t the revenue equivalence theorem apply here? Be specific.
On Moodle, you will find a file “Data for Q2 Ass3” which has data on grades...
On Moodle, you will find a file “Data for Q2 Ass3” which has data on grades in past sections of this course. The variables are Assignment1 = Grade on Assignment 1 as a percentage Assignment2 = Grade on Assignment 2 as a percentage Assignment3 = Grade on Assignment 3 as a percentage Midterm = Midterm Grade as a percentage Project = Project Grade as a percentage Participation = Participation Grade as a number out of 5. AssignmentDum = A dummy...
C++ This assignment will have you designing a program which can test multiple combinations of the...
C++ This assignment will have you designing a program which can test multiple combinations of the Boolean variables A, B, and C, and determine which combinations of them will yield true values. However this time the statement is designed in the style of a Logical Circuit. You are asked to create three truth tables for the three problems. To achieve this, you should create SEVEN gate functions for your checkpoint. For each of the problem: First, convert the diagram to...
C++ This assignment will have you designing a program which can test multiple combinations of the...
C++ This assignment will have you designing a program which can test multiple combinations of the Boolean variables A, B, and C, and determine which combinations of them will yield true values. However the statement is designed in the style of a Logical Circuit.You are asked to create three truth tables for the three problems.First, convert the diagram to boolean algebra equations.Then evaluate their values step by step based on the values of A, B and C. Finally, print the...
You will design and implement a graphics program that draws four concentric rectangles in a window,...
You will design and implement a graphics program that draws four concentric rectangles in a window, using four different colors of your choice, meeting the following specifications. • The size of the window is determined by the user of the program. To be specific, you should prompt the user for both the width and height of the window (in pixels), and then you should set the size of the window using these values. (Hint: use the setup() method of the...
Write a program that initializes an array of 6 random integers and then prints 4 lines...
Write a program that initializes an array of 6 random integers and then prints 4 lines of output, containing the following: 1. Only the first and last element 2. Every element at an odd index 3. Every odd element 4. All elements in reverse order
For this assignment you will either review an organization for which you currently work, or find...
For this assignment you will either review an organization for which you currently work, or find a case study to review. Write an essay, 1100-1400 words using proper APA formatting on the following How are each of the following issues addressed in the organization? Do you have any suggestions for improvements? Restrictive covenants such as non-competition, non-solicitation provision Access and use of technology resources Access and use of confidential and proprietary information Transfer of intellectual property work product Performance evaluation...
For this assignment, create a complete UML class diagram of this program. You can use Lucidchart...
For this assignment, create a complete UML class diagram of this program. You can use Lucidchart or another diagramming tool. If you can’t find a diagramming tool, you can hand draw the diagram but make sure it is legible. Points to remember: • Include all classes on your diagram. There are nine of them. • Include all the properties and methods on your diagram. • Include the access modifiers on the diagram. + for public, - for private, ~ for...
a) Which lines of code contain operations that change the contents of memory in this program?...
a) Which lines of code contain operations that change the contents of memory in this program? b) What are those operations? int main ( void ){             double dScore1;             double dScore2;             double dScore3;             double dAverage;             printf("Enter score 1: ");                                             //line 1             scanf("%lg", &dScore1);                                           //line 2             printf("Enter score 2: ");                                             //line 3             scanf("%lg", &dScore2);                                           //line 4             printf("Enter score 3: ");                                             //line 5             scanf("%lg", &dScore3);                                           //line 6             dAverage = (dScore1 + dScore2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT