The Complete Shapes and Draw Shapes Modules

We've covered all of Ha dames. At this p oint, you may wed( to om ohe Soapes and DrawShapes moduoes to see the drawing package in acaion. Heree the Shapes wwPule.

class Shape:

def _init_(self, x, y, width=0, height=0, color=None, fill=0)

self.x=x self.y=y self.width=width self.height=height self.color=color self.fill=fill def paint(self, graphics):

if not self.color is None:

oldcolor = graphics.foregroundColor graphics.color = self.color if self.fill:

self.draw_filled(graphics)

else:

self.draw_outline(graphics)

if not self.color is None:

graphics.color = oldcolor def draw_filled(self, graphics): pass def draw_outline(self, graphics): pass def getRect(self):

return self.x, self.y, self.width, self.height class Rectangle(Shape):

def _init_(self, x, y, width, height, color=None, fill=0):

Shape.__init_(self, x, y, width, height, color, fill)

def draw_filled(self,graphics):

graphics.fillRect(self.x, self.y, self.width, self.height def draw_outline(self,graphics):

graphics.drawRect(self.x, self.y, self.width, self.height class Oval(Shape):

def _init_(self, x, y, width, height, color=None, fill=0):

Shape.__init_(self, x, y, width, height, color, fill)

def draw_filled(self,graphics):

graphics.fillOval(self.x, self.y, self.width, self.height def draw_outline(self,graphics):

graphics.drawOval(self.x, self.y, self.width, self.height class Shapes (Shape):

def addShape(self, shape):

self._shapes.append(shape)

def paint(self, graphics):

for shape in self._shapes:

shape.paint()(graphics)

def getRect(self):

# Lists to hold x,y,height and width from shape in xl,yl,wl, hl = [],[],[],[]

# Iterate through the list gathering each shapes

# bounding rectangle for shape in shapes:

x,y,width, height = shape.getRect() xl.append(x) yl.append(y) wl.append(width) hl.append(height)

# This is for testing only if__name_ == '_main__' :

from javax.swing import JFrame, JPanel from java.awt import Color, Font class TestPanel(JPanel):

self.background=Color.white def addShape(self,shape):

self.shapes.addShape(shape)

self.shapes = Shapes()

self.addShape( Rectangle( 0, 0, 100, 100,

Color.blue, 1)) self.addShape ( Rectangle(100 , 0, 100, 100,

Color.blue)) self.addShape ( Oval( 0, 100, 100, 100, Color. 1) )

self.addShape( 0val(100, 100, 100, 100, Color def paint(self,graphics):

self.shapes.paint(graphics)

frame = JFrame("Test Shapes", size=(400,440), visible=1) pane = TestPanel() frame.contentPane = pane f rame.validate()

Here's the DrawShapes module: from Shapes import *

from java.awt import Font, Color, Dimension, BorderLayout from javax.swing import JComboBox, JLabel, JTextField, JComponent from javax.swing import BoxLayout, JCheckBox, JButton, JFrame, JPanel from java.awt.event import KeyEvent, ItemEvent class ShapeButton(JButton):

self.shape = shape def paint(self, graphics):

JButton.paint(self, graphics)

self.shape.paint(graphics)

def getPreferredSize(self) : d = Dimension(3C,3C) return d class ToolBox (JPanel):

JPanel._init__(self)

def getPreferredSize(self) : d = Dimension(4C, C) return d class StatusBox (JPanel):

JPanel._init__(self)

self.coordinates = JTextField(15, editable=C) self.format = 'x = %d, y = %d' self.add(self.coordinates)

self.event_type = JLabel ('MOUSE STATUS ') self.add(self.event_type)

self.shape_type = JLabel ('SHAPE STATUS ') self.add(self.shape_type)

self.coordinates.text = self.format % (x,y,)

def setMouseDrag(self, x, y):

self.event_type.text = 'MOUSE DRAG ' self.setXY (x,y)

def setMouseMove(self, x, y):

self.event_type.text = 'MOUSE MOVE ' self.setXY(x,y)

def setMousePress(self, x, y):

self.event_type.text = 'MOUSE PRESS ' self.setXY(x,y)

def setMouseRelease(self, x, y) :

self.event_type.text = 'MOUSE RELEASE ' self.setXY(x,y)

def setShapeType(self, shape_type):

# Set the label based on the shape type if shape_type == PaintBox.RECTANGLE:

self.shape_type.text = 'RECTANGLE'

elif shape_type == PaintBox.OVAL:

self.shape_type.text = 'OVAL'

class PaintBox (JComponent) RECTANGLE=1 OVAL=2

JComponent._init_(self)

self.opaque=1

self.background=Color.white self.status = status self.shapes = Shapes() self.shape_type = PaintBox.RECTANGLE self.mouseDragged = self.handle_mouseDragged self.mouseMoved = self.handle_mouseMoved self.mousePressed = self.handle_mousePress self.mouseReleased = self.handle_mouseRelease self.fill=0 self.color=Color.red def getPreferredSize(self) :

d = Dimension(400,400) return d def _drawShape(self, g, x, y, width, height):

if self.shape_type == PaintBox.RECTANGL E:

rect = Rectangle(self.start.x, self.start.y, rect.paint(g) if self.shape_type == PaintBox.OVAL:

oval = Oval(self.start.x, self.start.y, width, hei oval.paint()(g)

def drawRubberShape(self, x, y): g = self.graphics

# Set the graphics to XOR mode, which allows rubbe

# Calculate the width and height.

# Draw the outline of the shape.

try:

g.setXORMode(self.background) width = abs(self.start.x - x) height = abs(self.start.y - y)

self._drawShape(g, self.start.x, self.start.y, wi height)

finally:

g.dispose()

def handle_mousePress(self, event):

# Print the status self.status.setMousePress(event.x, event.y)

# Save the initial location.

# In addition save the initial location as the las self.last = self.start = event.point def handle_mouseDragged(self,event):

# Print the status. self.status.setMouseDrag(event.x, event.y)

# Erase the old rubberband shape at the old locati

# Create the new rubberband shape at the new locat self.drawRubberShape(self.last.x, self.last.y)

self.drawRubberShape(event.x, event.y)

# Save the current event.x and event.y as the last self.last = event.point def handle_mouseMoved(self,event):

# Print the status.

self.status.setMouseMove(event.x,event.y)

def handle_mouseRelease(self, event):

# Print the status self.status.setMouseRelease(event.x, event.y)

# Calculate the width and the height width = abs(self.start.x - event.x) height = abs(self.start.y - event.y)

shape = None #to hold the shape we are about to create

# Create the shape based on the current shape type

# Then add the shape to self.shapes. if self.shape_type == PaintBox.RECTANGL E:

shape = Rectangle(self.start.x, self.start.y, widt height, self.color, self.fill)

elif self.shape_type == PaintBox.OVAL:

shape = Oval(self.start.x, self.start.y, width, he self.color, self.fill)

if not shape is None:

self.shapes.addShape(shape) x,y,width,height = shape.getRect() self.repaint()(x,y,width+1, height+1)

def paint (self, graphics):

self.fillBackground(graphics)

#Draw all of the shapes. self.shapes.paint(graphics)

def fillBackground (self, graphics):

#Get the background color background=self.background

#Get the size size=self.getSize()

width, height = size.width, size.height

# Create a rectangle that is as big

# as the whole drawing area.

rect = Rectangle(0,0, width, height, background, 1) rect.paint(graphics)

def setShapeType(self, shape_type):

self.status.setShapeType(shape_type) self.shape_type = shape_type def getShapeType(self): return shape_type def setFill(self, value): self.fill = value def setShapeColor(self, color): self.color = color class DrawShapes(JFrame):

JFrame._init__(self, title='Draw Shapes', visible=1, size=(400,400))

self.__init_options()

self.statusPane = StatusBox()

self.contentPane.add(self.statusPane, BorderLayout.SOUTH

self.PaintPane = PaintBox(self.statusPane) self.contentPane.add(self.PaintPane, BorderLayout.CENTEF

def close(event):

mainWindow.visible=0 mainWindow.dispose() from java.lang import System Sys tem.exit(0)

global mainWindow mainWindow = self self.windowClosing=close def _init_toolbar(self):

toolbar = ToolBox()

# Add the rectangle button to the toolbar rect = ShapeButton(Rectangle(4, 4, 20, 20)) toolbar.add(rect)

rect.actionPerformed = self.rect_pressed

# Add the oval button to the toolbar oval = ShapeButton(Oval(4, 4, 10, 20)) toolbar.add(oval)

oval.actionPerformed = self.oval_pressed self.contentPane.add(toolbar, BorderLayout.WEST)

def rect_pressed(self, event):

self.PaintPane.setShapeType(PaintBox.RECTANGLE)

def oval_pressed(self, event)

self.PaintPane.setShapeType(PaintBox.OVAL)

def fill_clicked(self, event):

if (event.stateChange==ItemEvent.SELECTED) : self.PaintPane.setFill(l)

elif (event.stateChange==ItemEvent.DESELECTED): self. PaintPane.setFill(0)

def color_changed(self,event): colorname = event.item color = self.colors_dict[colorname] self.PaintPane.setShapeColor(color)

def _init_options(self) :

optionsPane = JPanel()

# Set up the checkbox item for the fill option check = JCheckBox('Fill')

optionsPane.add(check)

check.itemStateChanged = self.fill_clicked

# Set up the choice for color colors = JComboBox()

self.colors_dict = {'blue':Color.blue,

'green':Color.green, 'red':Color.red, 'yellow':Color.yellow, 'orange':Color.orange, 'cyan':Color.cyan, 'pink':Color.pink,

for color in self.colors_dict.keys(): colors.addItem(color)

colors.setSelectedItem('red')

optionsPane.add(colors)

colors.itemStateChanged = self.color_changed self.contentPane.add(optionsPane, BorderLayout.NORTH)

Notice that, for all the fuyctioye lity we get, the code is releOye sisndle.

Now let's fire up DrawShapee and doaw some rectangles and ovaas u Figure 15-3 is a screen dump of what we're going to do.

Figure 15-3. Screen Dump of the DrawShapes Example

Draw Shapes

MOUSE MOVE OVAl

Was this article helpful?

0 0

Post a comment