/* * JaamSim Discrete Event Simulation * Copyright (C) 2015 Ausenco Engineering Canada Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Adapted from CC0-licensed code posted by "aterai" (TERAI Atshuhiro) // on http://ateraimemo.com/Swing/RoundImageButton.html package com.jaamsim.ui; import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Insets; import java.awt.RenderingHints; import java.awt.Shape; import java.awt.geom.Ellipse2D; import javax.swing.BorderFactory; import javax.swing.Icon; import javax.swing.JToggleButton; class RoundToggleButton extends JToggleButton { protected Shape shape, base; public RoundToggleButton(Icon icon) { super(icon); } @Override public void updateUI() { super.updateUI(); setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1)); //setBackground(Color.BLACK); setContentAreaFilled(false); setFocusPainted(false); setAlignmentY(Component.TOP_ALIGNMENT); initShape(); } @Override public Dimension getPreferredSize() { Icon icon = getIcon(); Insets i = getInsets(); int iw = Math.max(icon.getIconWidth(), icon.getIconHeight()); return new Dimension(iw + i.right + i.left, iw + i.top + i.bottom); } private void initShape() { if (!getBounds().equals(base)) { Dimension s = getPreferredSize(); base = getBounds(); shape = new Ellipse2D.Float(0, 0, s.width - 1, s.height - 1); } } @Override protected void paintBorder(Graphics g) { initShape(); Graphics2D g2 = (Graphics2D) g.create(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(getBackground()); g2.draw(shape); g2.dispose(); } @Override public boolean contains(int x, int y) { initShape(); return shape == null ? false : shape.contains(x, y); } }