Complete the class ProcessRectangle given below. 1.Create a rectangle with a wid
ID: 3786280 • Letter: C
Question
Complete the class ProcessRectangle given below.
1.Create a rectangle with a width of 60 and a height of 50 at x = 100, y = 200.
2.Call the translate method to move the rectangle 20 pixels in the x direction and 30 pixels in the y direction.
3.Print the new x and y in this format: x, y (Rectangle has methods to get x and y)
4.Call the grow method the rectangle to make the rectangle 10 units larger on both the left and right side leaving the height unchanged.
----------------------------------------------------------------------------
import java.awt.Rectangle;
public class ProcessRectangle
{
public static void main(String[] args)
{
Rectangle rec;
System.out.println(rec);
}
}
Explanation / Answer
Rectangle1.java
import java.awt.*;
import javax.swing.JPanel;
public class Rectangle1 extends JPanel {
public Rectangle1(){
repaint();
}
private int length1 = 50;
private int width1 = 60;
private int x = 100;
private int y = 100;
public int getX(){
return x;
}
public int getY(){
return y;
}
public void grow(Graphics g){
length1 += 20;
x -= 10;
}
public void translate(){
x += 20;
y += 30;
}
public int getLength1(){
return length1;
}
public int getWidth1(){
return width1;
}
}
ProcessRectangle.java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class ProcessRectangle extends JFrame{
Rectangle1 r;
public ProcessRectangle(){
r = new Rectangle1();
}
public void paint(Graphics g){
g.setColor(Color.RED);
g.drawRect(r.getLength1(), r.getWidth(), r.getX(), r.getY());
g.setColor(Color.BLACK);
r.grow(g);
g.drawRect(r.getLength1(), r.getWidth(), r.getX(), r.getY());
g.setColor(Color.green);
r.translate();
g.drawRect(r.getLength1(), r.getWidth(), r.getX(), r.getY());
}
public static void main(String[] args){
JFrame pr = new ProcessRectangle();
pr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pr.setSize(600,600);
pr.setResizable(false);
pr.setTitle("Rectangle");
pr.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.