Richard S. Hall | f44c499 | 2009-02-03 02:29:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.example.servicebased.trapezoid; |
| 20 | |
| 21 | import java.awt.BasicStroke; |
| 22 | import java.awt.Color; |
| 23 | import java.awt.GradientPaint; |
| 24 | import java.awt.Graphics2D; |
| 25 | import java.awt.Point; |
| 26 | import java.awt.geom.*; |
| 27 | import java.util.Hashtable; |
| 28 | import javax.swing.ImageIcon; |
| 29 | import org.apache.felix.example.servicebased.host.service.SimpleShape; |
| 30 | import org.osgi.framework.*; |
| 31 | |
| 32 | /** |
| 33 | * This class implements a simple bundle activator for the trapezoid |
| 34 | * <tt>SimpleShape</tt> service. This activator simply creates an instance |
| 35 | * of the trapezoid service object and registers it with the service registry |
| 36 | * along with the service properties indicating the service's name and icon. |
| 37 | **/ |
| 38 | public class Activator implements BundleActivator |
| 39 | { |
| 40 | private BundleContext m_context = null; |
| 41 | |
| 42 | /** |
| 43 | * Implements the <tt>BundleActivator.start()</tt> method, which |
| 44 | * registers the trapezoid <tt>SimpleShape</tt> service. |
| 45 | * @param context The context for the bundle. |
| 46 | **/ |
| 47 | public void start(BundleContext context) |
| 48 | { |
| 49 | m_context = context; |
| 50 | Hashtable dict = new Hashtable(); |
| 51 | dict.put(SimpleShape.NAME_PROPERTY, "Trapezoid"); |
| 52 | dict.put(SimpleShape.ICON_PROPERTY, |
| 53 | new ImageIcon(this.getClass().getResource("trapezoid.png"))); |
| 54 | m_context.registerService( |
| 55 | SimpleShape.class.getName(), new Trapezoid(), dict); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Implements the <tt>BundleActivator.start()</tt> method, which |
| 60 | * does nothing. |
| 61 | * @param context The context for the bundle. |
| 62 | **/ |
| 63 | public void stop(BundleContext context) |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * This inner class implements the trapezoid <tt>SimpleShape</tt> service. |
| 69 | * It simply provides a <tt>draw()</tt> that paints a trapezoid. |
| 70 | **/ |
| 71 | public class Trapezoid implements SimpleShape |
| 72 | { |
| 73 | /** |
| 74 | * Implements the <tt>SimpleShape.draw()</tt> method for painting |
| 75 | * the shape. |
| 76 | * @param g2 The graphics object used for painting. |
| 77 | * @param p The position to paint the trapezoid. |
| 78 | **/ |
| 79 | public void draw(Graphics2D g2, Point p) |
| 80 | { |
| 81 | int x = p.x - 25; |
| 82 | int y = p.y - 25; |
| 83 | GradientPaint gradient = new GradientPaint( |
| 84 | x, y, Color.YELLOW, x + 50, y, Color.WHITE); |
| 85 | g2.setPaint(gradient); |
| 86 | int[] xcoords = { x + 10, x, x + 50, x + 40 }; |
| 87 | int[] ycoords = { y, y + 50, y + 50, y }; |
| 88 | GeneralPath polygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD, xcoords.length); |
| 89 | polygon.moveTo(x + 25, y); |
| 90 | for (int i = 0; i < xcoords.length; i++) |
| 91 | { |
| 92 | polygon.lineTo(xcoords[i], ycoords[i]); |
| 93 | } |
| 94 | polygon.closePath(); |
| 95 | g2.fill(polygon); |
| 96 | BasicStroke wideStroke = new BasicStroke(2.0f); |
| 97 | g2.setColor(Color.black); |
| 98 | g2.setStroke(wideStroke); |
| 99 | g2.draw(polygon); |
| 100 | } |
| 101 | } |
| 102 | } |