# .. and stick them to our canvas @canvas.children.add(@minute_hand) @canvas.children.add(@hour_hand)
plot_face # draw a clock face plot_labels # draw clock numbers plot_hands # draw minute / hour hands
@window.content = @canvas app = System::Windows::Application.new app.run(@window) # the Application object handles the lifecycle of our app # including the execution loop end
# determine 2 sets of equidistant points around the circumference of a circle # of CLOCK_WIDTH and CLOCK_HEIGHT dimensions. def plot_locations for i in (0..60) # 60 minutes, and 12 hours a = i * 6 x = (RADIUS * Math.sin(a * RADS)).to_i + (CLOCK_WIDTH / 2) y = (CLOCK_HEIGHT / 2) - (RADIUS * Math.cos(a * RADS)).to_i coords = [x, y] HOUR_LOCATIONS[i / 5] = coords if i % 5 == 0 # is this also an "hour" location (ie. every 5 minutes)? MIN_LOCATIONS[i] = coords end end
# draws a circle to represent the clock"s face def plot_face extra_x = (CLOCK_WIDTH * 0.15) # pad our circle a little extra_y = (CLOCK_HEIGHT * 0.15) face = System::Windows::Shapes::Ellipse.new face.fill = System::Windows::Media::Brushes.White face.width = CLOCK_WIDTH + extra_x face.height = CLOCK_HEIGHT + extra_y face.margin = System::Windows::Thickness.new(0 - (extra_x/2), 0 - (extra_y/2), 0, 0) face.stroke = System::Windows::Media::Brushes.Gray # give it a slight border face.stroke_thickness = 1 System::Windows::Controls::Canvas.set_z_index(face, -1) # send our circle to the back @canvas.children.add(face) # add the clock face to our canvas end
# at each point along the hour locations, put a number def plot_labels HOUR_LOCATIONS.each_pair do |p, coords| unless p == 0 lbl = System::Windows::Controls::Label.new lbl.horizontal_content_alignment = System::Windows::HorizontalAlignment.Center lbl.width = LABEL_WIDTH lbl.height = LABEL_HEIGHT lbl.content = p.to_s lbl.margin = System::Windows::Thickness.new (coords[0] - (LABEL_WIDTH / 2), coords[1] - (LABEL_HEIGHT / 2), 0, 0) lbl.padding = System::Windows::Thickness.new(0, 0, 0, 0) @canvas.children.add(lbl) end end end