Please, explain every line of code of this program. import java.awt.*; import ja
ID: 3544116 • Letter: P
Question
Please, explain every line of code of this program.
import java.awt.*;
import java.awt.geom.*;
import javax.swing.JApplet;
public class geomp extends JApplet
{
Container c;
public void init()
{
c = getContentPane();
c.setBackground(Color.cyan);
}
public void paint (Graphics g)
{
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
Rectangle2D.Float r1 = new Rectangle2D.Float(10f, 10f, 40f, 80f);
Rectangle2D.Float r2 = new Rectangle2D.Float(100f, 10f, 40f, 80f);
BasicStroke b1 = new BasicStroke(7f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
BasicStroke b2 = new BasicStroke (3f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER);
GradientPaint gp1 = new GradientPaint (100f, 10f, Color.green, 140f, 90f, Color.red, false);
g2.setColor(Color.blue);
g2.setStroke(b1);
g2.draw(r1);
g2.setPaint(gp1);
g2.setStroke(b2);
g2.fill(r2);
g2.setColor(Color.green);
GeneralPath pg1 = new GeneralPath();
pg1.moveTo(30f, 140f);
pg1.lineTo(120f, 120f);
pg1.lineTo(20f, 190f);
pg1.closePath();
g2.setStroke(b1);
g2.draw(pg1);
}
}
Output:
Explanation / Answer
Well check below
In the Rectangle2D.Float constructor, the first two parameters are the x and y coordinates of the top left hand corner of the rectangle. The last two are its width and height.
Having set the color and stroke attributes, the draw method then draws the outline of the first rectangle (r1) at those coordinates.
In contrast, having set the paint (using a GradientPaint object) and stroke attributes, the fill method draws and fills the interior of the second rectangle (r2) at the x and y coordinates given in its constructor.