/** @version 1.00 2019-04-04 @author Agustín J. González */ import java.awt.*; import java.awt.geom.*; //import java.util.*; import java.awt.event.*; import javax.swing.*; public class MyOwnGraphicsObject { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { // implementación Swing recomendada public void run() { MyOwnFrame frame = new MyOwnFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } // run ends }); } } /** A frame with a panel for a simple graphics object */ class MyOwnFrame extends JFrame { public MyOwnFrame() { setTitle("MyOwnGraphcisObject"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // add panel to frame MyOwnPanel panel = new MyOwnPanel(); Container contentPane = getContentPane(); contentPane.add(panel); } public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 300; } /** A panel for a simple graphics object. */ class MyOwnPanel extends JPanel { public MyOwnPanel() { sem = new Semaphore(this); setFocusable(true); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; sem.paintComponent(g2); } private Semaphore sem; } class Semaphore { public Semaphore (JPanel p) { panel = p; color = Color.RED; ellipse = new Ellipse2D.Double(100,100,50,50); timer = new Timer(1000, this); timer.start(); } public void actionPerformed(ActionEvent event){ color = (color==Color.RED)?Color.GREEN : Color.RED; panel.repaint(); } public void paintComponent(Graphics2D g2d) { g2d.setColor(color); g2d.fill(ellipse); } private Timer timer; private JPanel panel; private Ellipse2D red, yellow; private Color color; private final int origen_X=150; private final int ORIGEN_Y=100; private final int LIGHT_RADIO; }