Rotating graphic is changing sizes at random

Well if you want it, here is what I threw together. You of course can add more customization if needed or wanted.

repaint script
from java.awt import Color
from java.awt import RadialGradientPaint
from java.awt import MultipleGradientPaint as mgp
from java.awt.geom import Rectangle2D
from java.awt.geom import Ellipse2D
from java.awt.geom import Line2D
from java.awt.geom import Point2D
from java.awt import BasicStroke
from java.awt.geom import Path2D
import math
g = event.graphics


#Watch Body

bodyOutline = Ellipse2D.Float(event.width * .075, event.height * .115, event.width * .85, event.height * .85)
bodyShape = Ellipse2D.Float(event.width * .1 ,event.height * .14 , event.width * .8, event.height * .8)
cX = bodyShape.getX() + bodyShape.getWidth() / 2
cY = bodyShape.getY() + bodyShape.getHeight() / 2
radius = bodyShape.width / 2

#Draw outside
g.setStroke(BasicStroke(1.0))
g.setPaint(Color.BLACK)
g.draw(bodyOutline)

center = Point2D.Float(cX,cY)
focus = Point2D.Float((radius - 10) * math.cos(math.pi / 4) + cX,(radius - 10) * math.sin(math.pi / 4) + cY)
colors = [Color.WHITE,Color.LIGHT_GRAY,Color.GRAY]
bodyPaint = RadialGradientPaint(center,300,focus, [0.0,0.8,1.0],colors,mgp.CycleMethod.NO_CYCLE)
g.setPaint(bodyPaint)
g.fill(bodyOutline)

#Draw Face
g.draw(bodyShape)
g.setPaint(event.source.background)
g.fill(bodyShape)
g.setPaint(Color.DARK_GRAY)
g.draw(bodyShape)

#Set Font
g.setFont(event.source.font)

#Tick Marks
theta = -math.pi/2
for tick in xrange(60):
	
	tickLength = event.source.MinorTickLength if tick % 5 else event.source.MajorTickLength
	#calculate the start and end points for the tick line
	x1 = radius * math.cos(theta) + cX
	y1 = radius * math.sin(theta) + cY
	x2 = (radius - tickLength) * math.cos(theta) + cX
	y2 = (radius - tickLength) * math.sin(theta) + cY
	
	
	tickLine = Line2D.Float(x1,y1,x2,y2)
	if tick % 5:
		g.setColor(event.source.MinorTickColor)
		g.setStroke(BasicStroke(event.source.MinorTickWidth))
	else:
		g.setColor(event.source.MajorTickColor)
		g.setStroke(BasicStroke(event.source.MajorTickWidth))
		
	g.draw(tickLine)
	
	#Draw the tick increment text
	if not tick % 5:
		
		
		fontMetrics = g.getFontMetrics()
		x2 = (radius - tickLength - (fontMetrics.getHeight() /2.0 + fontMetrics.getAscent()/5)) * math.cos(theta) + cX  - (fontMetrics.stringWidth(str(tick)) / 2.0)
		y2 = (radius - tickLength - (fontMetrics.getHeight() /2.0 + fontMetrics.getAscent()/5)) * math.sin(theta) + cY + 4
		g.setColor(event.source.foreground)
		#Uncomment if you would rather it say 60 than 0
		#if tick == 0:
		#	g.drawString("60",x2,y2)
		#else:
		#tab in the next line if you uncomment the above code.
		g.drawString(str(tick),x2,y2)
		
	#increment theta to next angle
	theta += math.radians(360/60)

#Draw the Set-Point hand
g.setPaint(Color.ORANGE)

spNeedle = Path2D.Float()
radPerTick = math.radians(360/60)
value = event.source.Setpoint

spNeedle.moveTo((radius-45) * math.cos(value * radPerTick - math.pi / 2) + cX,(radius-45) * math.sin(value * radPerTick - math.pi / 2) + cY)
spNeedle.lineTo(3 * math.cos(value * radPerTick - math.pi) + cX,3 *math.sin(value * radPerTick - math.pi) + cY)
spNeedle.curveTo(15 * math.cos(value * radPerTick - 5 * math.pi/4) + cX, 15 * math.sin(value * radPerTick - 5 * math.pi/4) + cY,15 * math.cos(value * radPerTick + math.pi/4) + cX,15 * math.sin(value * radPerTick + math.pi/4) + cY, 3 * math.cos(value * radPerTick) + cX, 3 * math.sin(value * radPerTick) + cY)
spNeedle.closePath()
g.fill(spNeedle)


#Draw the Value-Hand
value = event.source.Value
g.setPaint(Color.BLACK)
vNeedle = Path2D.Float()
vNeedle.moveTo(radius * math.cos(value * radPerTick - math.pi/2) + cX, radius * math.sin(value * radPerTick - math.pi/2) + cY)
vNeedle.lineTo(3 * math.cos(value * radPerTick - math.pi) + cX, 3 * math.sin(value * radPerTick - math.pi) + cY)
vNeedle.curveTo(15 * math.cos(value * radPerTick - 5 * math.pi/4) + cX, 15 * math.sin(value * radPerTick - 5 * math.pi/4) + cY, 15 * math.cos(value * radPerTick + math.pi/4) + cX, 15 * math.sin(value * radPerTick + math.pi/4) + cY, 3 * math.cos(value * radPerTick) + cX, 3 * math.sin(value * radPerTick) + cY)
vNeedle.closePath()
g.fill(vNeedle)

g.setPaint(event.source.SetpointColor)

#Draw Set Point Text
fm = g.getFontMetrics()
g.drawString(str(event.source.Setpoint),cX - fm.stringWidth(str(event.source.Setpoint)) / 2.0, cY + 50 )

#Draw Value Text
g.setPaint(event.source.ValueColor)
g.drawString(str(event.source.Value),cX - fm.stringWidth(str(event.source.Value)) / 2.0, cY + 65 )

stopwatch

8 Likes