In: Computer Science
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
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