import java.awt.*; import javax.swing.*; class Guruguru extends JPanel { private int r = 50; private int x = 110, y = 70 ; private double theta = 0; // 角度 public Guruguru() { setPreferredSize(new Dimension(200, 180)); Timer timer = new Timer(30, e -> { theta += 0.02; x = 60 + (int)(r * Math.cos(theta)); y = 100 - (int)(r * Math.sin(theta)); repaint(); }); timer.start(); JButton startBtn = new JButton("start"); startBtn.addActionListener(e -> timer.start()); JButton stopBtn = new JButton("stop"); stopBtn.addActionListener(e -> timer.stop()); setLayout(new FlowLayout()); add(startBtn); add(stopBtn); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString("Hello, World!", x, y); } } void main() { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("ぐるぐる!"); frame.add(new Guruguru()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }); }