質問内容
質問を評価する
(0ポイント)
|
以下のプログラムを改良して アナログ時計を作りたいんですけど 時間をCalendarで取得したあとの処理を教えてください できればソースなども含めて教えていただけると助かり ます
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.Rectangle2D;
public class RotatePanel extends JPanel implements ActionListener { Timer timer = new Timer(1000, this); int times = 0;
public RotatePanel() { timer.start(); }
public void actionPerformed(ActionEvent e) { times = (times + 1) % 60; repaint(); }
public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(5.0f)); g2d.setPaint(Color.blue); g2d.draw(new Polygon(new int[]{0, 0, 40}, new int[]{0, 40, 20}, 3));
int cx = getWidth() / 2; int cy = getHeight() / 2; g2d.rotate((2 * Math.PI * times) / 60, cx, cy);
Font font = new Font("Serif", Font.PLAIN, 20); g2d.setFont(font); String str = "--------";
g2d.drawString(str, cx, cy); FontMetrics fm = g2d.getFontMetrics(); Rectangle2D rec = fm.getStringBounds(str, g2d); int x = cx + (int) rec.getX(); int y = cy + (int) rec.getY(); int w = (int) rec.getWidth(); int h = (int) rec.getHeight(); g2d.draw(new Rectangle(x, y, w, h)); }
public static void main (String[] args) { JFrame jFrame = new JFrame("analog"); RotatePanel rotatePanel = new RotatePanel(); jFrame.getContentPane().add(rotatePanel, BorderLayout.CENTER); jFrame.setSize(400, 200); jFrame.setVisible(true); } }
|