/** * Paints the current frame on the specified component. * * @param c component on which to paint the dial. * @param graphics graphic context to use when painting the dial. * @param x horizontal coordinate at which to paint the dial. * @param y vertical coordinate at which to paint the dial. */ @Override public synchronized void paintFrame(Component c, Graphics graphics, int x, int y) { int currentFrame; // Ignores paint calls while not animated. if (isAnimated()) { // Checks whether the current frame has already been generated or not, generates // it if not. if ((frames[currentFrame = getFrame()]) == null) { Image frame; GraphicsConfiguration gc; Graphics2D g; int alpha; double cos; double sin; int radius; // Initialises the frame. // Note: getGraphicsConfiguration() returns null if the component has not yet been added to // a container if (c != null && (gc = c.getGraphicsConfiguration()) != null) frame = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT); else frame = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); // Initialises the frame's g. initialiseGraphics(g = (Graphics2D) frame.getGraphics()); // Draws each spoke in the dial. alpha = 255; radius = FULL_SIZE / 2 - 1 - (int) (strokeWidth / 2); for (int i = 0; i < getFrameCount(); i++) { cos = Math.cos((Math.PI * 2) - (Math.PI * 2 * (i - currentFrame)) / getFrameCount()); sin = Math.sin((Math.PI * 2) - (Math.PI * 2 * (i - currentFrame)) / getFrameCount()); g.setColor(getSpokeColor(alpha)); g.drawLine( (int) (radius * FRACTION * cos), (int) (radius * FRACTION * sin), (int) (radius * cos), (int) (radius * sin)); alpha = Math.max(MIN_ALPHA, (alpha * 3) / 4); } g.dispose(); // Stores the newly generated frame. frames[currentFrame] = frame; } // Draws the current frame. graphics.drawImage(frames[currentFrame], x, y, null); } }
/** * Constructs a Graphics context with the following graphics state: * * <UL> * <LI>Paint: The color of the component. * <LI>Font: The font of the component. * <LI>Stroke: Linewidth 1.0; No Dashing; Miter Join Style; Miter Limit 10; Square Endcaps; * <LI>Transform: The getDefaultTransform for the GraphicsConfiguration of the component. * <LI>Composite: AlphaComposite.SRC_OVER * <LI>Clip: The size of the component, Rectangle(0, 0, size.width, size.height) * </UL> * * @param component to be used to initialize the values of the graphics state * @param doRestoreOnDispose true if writeGraphicsRestore() should be called when this graphics * context is disposed of. */ protected AbstractVectorGraphicsIO(Component component, boolean doRestoreOnDispose) { super(); this.size = component.getSize(); this.component = component; this.doRestoreOnDispose = doRestoreOnDispose; deviceClip = (size != null ? new Rectangle(0, 0, size.width, size.height) : null); userClip = null; GraphicsConfiguration gc = component.getGraphicsConfiguration(); currentTransform = (gc != null) ? gc.getDefaultTransform() : new AffineTransform(); currentComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER); currentStroke = new BasicStroke(1.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f, null, 0.0f); super.setFont(component.getFont()); super.setBackground(component.getBackground()); super.setColor(component.getForeground()); // Initialize the rendering hints. hints = new RenderingHints(null); }
public static void keepComponentInsideScreen(int x, int y, Component c) { Dimension screenDim = c.getToolkit().getScreenSize(); GraphicsConfiguration g = c.getGraphicsConfiguration(); if (g != null) { Insets insets = c.getToolkit().getScreenInsets(g); if (x + c.getWidth() > screenDim.width - insets.right) { x = (screenDim.width - insets.right) - c.getWidth(); } else if (x < insets.left) { x = insets.left; } if (y + c.getHeight() > screenDim.height - insets.bottom) { y = (screenDim.height - insets.bottom) - c.getHeight(); } else if (y < insets.top) { y = insets.top; } c.setLocation(x, y); } else { System.out.println("null"); } }
public static void positionComponent(int p, Component c, Component o) { Rectangle d = null; /* * TODO This is very lame doesnt require the component to position * around, just assuming its a window. */ try { // #ifdef JAVA1 /* * throw new Exception(); */ // #else GraphicsConfiguration config = o.getGraphicsConfiguration(); GraphicsDevice dev = config.getDevice(); d = config.getBounds(); // #endif JAVA1 } catch (Throwable t) { } positionComponent(p, c, d); }
public static void centerComponent(Component c) { Rectangle r = c.getGraphicsConfiguration().getBounds(); c.setLocation( ((r.x + r.width) - c.getSize().width) / 2, ((r.y + r.height) - c.getSize().height) / 2); }