To begin, define a base class Shape that stores the shape's length, position, and (randomly generated) color. From there, you can inherit these basic properties and further specify additional shapes. At a minimum, your program must support squares, rectangles, triangles, circles, and ellipses. Each shape should define its own class in an inheritance hierarchy based on Shape. Recall that inheritance often spans more than one level, so consider and implement an appropriate inheritance scheme. The classes that define the various shapes should further specify the Shape class by adding additional needed attributes, appropriately invoking parent constructors during initialization, and including a method to draw the shape using a QPainter instance provided as a parameter. Notice that the shape classes fully encapsulate the shape concepts, including the graphical drawing code. By providing an already-in- use QPainter instance to the shapes' draw methods,<-The code I have provided is supposed to do what the question above is asking for but I am not sure that I am doing it correctly and want to verify if my current code does this.
When the user clicks somewhere in the window (indicated by a call to mousePressEvent), create a new Shape object at random to be located at the click coordinates and add that Shape to the list of items to draw, which should be an attribute of your class. The Shape object's constructor (in cooperation with its parent constructors) should set all its attributes, including coordinates, color, and size. Feel free to apply reasonable restrictions to shape sizes. Every time the window is drawn (because something triggered a call to paintEvent), each of the shapes created so far should be drawn. Because they are in a list (see below), you can iterate over them using a loop and draw each shape. The beauty of polymorphism is that you can treat them all as basic Shapes and ask them to draw themselves, because they each implement a draw method that takes a QPainter instance (perhaps the one from paintEvent in ShapeDrawer.py) as a parameter. Note that you must already have activated the painter object by calling its begin method before passing it to a shape's draw method.<-I have no clue how to complete number 2 with my current code and would really appreciate if some one could help me with number two because I am confused with the mouse clicks part and two overall.
Overall, I've done number one with the code provided and want to verify if it is correct and have no clue how to do number two and need help.
The code I have provided is what I have tried.
import sys, random
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QPainter, QPainterPath, QBrush, QPen
from PyQt5.QtCore import Qt
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.title = "Main Window"
self.right= 200
self.left= 200
self.width = 500
self.height = 500
self.Window()
def Window(self):
self.setWindowTitle(self.title)
self.setGeometry(self.right, self.left, self.width, self.height)
self.show()
class shape():
def __init__(self,length,position,color):
self.length=length
self.position=position
self.color= random.choice(colors)
class circle(shape):
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(QPen(Qt.red, 4 , Qt.SolidLine))
painter.drawEllipse(20, 20, 200, 200)
class rectangle(shape):
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(QPen(Qt.yellow, 7, Qt.DotLine))
painter.drawRect(10, 20, 100, 200)
class ellipse(shape):
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
painter.setBrush(QBrush(Qt.black, Qt.SolidPattern))
painter.drawEllipse(20, 20, 200, 100)
class triangle(shape):
def paintEvent(self, event):
painter = QPainter()
path = QPainterPath()
painter.begin(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(QtCore.Qt.red)
path.lineTo(180, 300)
path.lineTo(200, 100)
path.lineTo(10, 50)
painter.drawPath(path)
class square(shape):
def paintEvent(self, event):
painter = QPainter()
path = QPainterPath()
painter.begin(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(QtCore.Qt.red)
path.lineTo(20, 12)
path.lineTo(20, 28)
path.lineTo(36, 28)
path.lineTo(36,12)
path.lineTo(20,12)
painter.drawPath(path)
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())