You are on page 1of 2

PUF Ho Chi Minh ville PrePOA course Responsible: Tran Thai Nam

Object Oriented Programming Inheritance


Graphics We propose an Interface named GraphicObject with functions: getPosition returns its position, type of Point2D getColor returns its color, type of Color draw draws it on screen, parameter g of java.awt.Graphics. g is object used to draw 2D on panel or window. descript print text about it 1. Create new project named Graphics and new package named graphics. 2. Write GraphicObject interface. 3. One graphic object can be rectangle, circle, square, text However; all of them have same functions of position, drawing color and different drawing. So we should create one abstract class named AbstractGraphicObject implementing GraphicObject and implement same code into this abstract class. 4. Implement Text class extends AbstractGraphicObject 6. Create a class named Rectangle extends AbstractGraphicObject and has more fields: fill color, width and height. Method surface. 7. Create a class named Cirlcle extends AbstractGraphicObject and has more fields: fill color and radius. Method surface. 8. Lets review the code between Rectangle and Circle, do we make duplicated code? Make another class named AbstractGeometryObject extends AbstractGraphicObject and put duplicated code into it. Rectangle and Circle should extend from AbstractGeometryObject. 9. Create class named GraphicForm extends JFrame. It has a list of GraphicObject and these methods: //Add a graphic object into list and draw it public void addGraphicObject(GraphicObject go) //Remove a graphic object from list and redraw the window public void removeGraphicObject(GraphicObject go) //Clear the window public void clear() //Draw all graphic objects in list public void draw() Hints: How to get Graphics object for drawing on window Page 1 of 2

PUF Ho Chi Minh ville PrePOA course Responsible: Tran Thai Nam Override the method paint of JFrame with the following prototype: @Override public void paint(Graphics g){ g.drawRect(x, y, width, height); } Or in anywhere of class extending JFrame: Graphics g = getContentPane().getGraphics(); g.drawRect(x, y, width, height); 10. Create a class named Test with main method. In here, we create one instant of GraphicForm and add graphic object: Text, Rectangle, Circle. 11. Create new class named Square. 12. We can group many graphic objects into one group. A group is considered a graphic object, can contain another group. Implement Group class.

Page 2 of 2

You might also like