// Purpose: Support the manipulation of a rectangle in a window. import javax.swing.*; import java.awt.*; public class ColoredRectangle { // instance variables to describe object attributes private int width; // rectangle width private int height; // rectangle height private int x; // rectangle x-coordinate position private int y; // rectangle y-coordinate position private JFrame window; // window displaying rectangle private Color color; // color of rectangle // ColoredRectangle(): default constructor public ColoredRectangle() { window = new JFrame("Box Fun"); window.setSize(200, 200); width = 40; height = 20; x = 80; y = 90; color = Color.BLUE; window.setVisible(true); } // paint(): display the rectangle in its window public void paint() { Graphics g = window.getGraphics(); g.setColor(color); g.fillRect(x, y, width, height); } // setWidth(): width mutator public void setWidth(int w) { width = w; } // setHeight(): height mutator public void setHeight(int h) { height = h; } // setX(): x mutator public void setX(int ulx) { x = ulx; } // setY(): y mutator public void setY(int uly) { y = uly; } // setWindow(): window mutator public void setWindow(JFrame f) { window = f; } // setColor(): color mutator public void setColor(Color c) { color = c; } // getWidth(): width accessor public int getWidth() { return width; } // getHeight(): height accessor public int getHeight() { return height; } // getX(): x accessor public int getX() { return x; } // getY(): y accessor public int getY() { return y; } // getWindow(): window accessor public JFrame getWindow() { return window; } // getColor(): color accessor public Color getColor() { return color; } // erase(): erase the rectangle by displaying it // in the background color. public void erase() { Graphics g = window.getGraphics(); g.clearRect(x, y, width, height); } // ColoredRectangle(): specific constructor public ColoredRectangle(int w, int h, int ulx, int uly, JFrame f, Color c) { setWidth(w); setHeight(h); setX(ulx); setY(uly); setWindow(f); setColor(c); } }