//Clock SquarePanel squarePanel = new SquarePanel(); squarePanel.setBounds(20, 20, 400, 400); getContentPane().add(squarePanel); class SquarePanel extends JPanel { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); setBackground(Color.WHITE); //Panel's size int panelWidth = getWidth(); int panelHeight = getHeight(); int squareSize = Math.min(panelWidth, panelHeight) - 40; //Draw circle int squareX = (panelWidth - squareSize) / 2; int squareY = (panelHeight - squareSize) / 2; g.setColor(Color.BLACK); g.drawRect(squareX, squareY, squareSize, squareSize); int circleDiameter = squareSize - 20; int circleX = squareX + 10; int circleY = squareY + 10; g.setColor(Color.GREEN); g.drawOval(circleX, circleY, circleDiameter, circleDiameter); //Numbers g.setColor(Color.BLACK); g.drawString("12", squareX + squareSize / 2 - 5, squareY + 15); g.drawString("3", squareX + squareSize - 20, squareY + squareSize / 2 + 5); g.drawString("6", squareX + squareSize / 2 - 5, squareY + squareSize - 5); g.drawString("9", squareX + 5, squareY + squareSize / 2 + 5); // Get the center of the circle int centerX = squareX + squareSize / 2; int centerY = squareY + squareSize / 2; // Hour hand g.setColor(Color.BLUE); drawRhombus(g, centerX+35, centerY, 70, 20); // Minute hand g.setColor(Color.RED); drawRhombus(g, centerX-70, centerY, 150, 40); } //draw a rhombus shape private void drawRhombus(Graphics g, int centerX, int centerY, int width, int height) { int[] xPoints = {centerX, centerX + width / 2, centerX, centerX - width / 2}; int[] yPoints = {centerY - height / 2, centerY, centerY + height / 2, centerY}; int numPoints = 4; Polygon rhombus = new Polygon(xPoints, yPoints, numPoints); g.fillPolygon(rhombus); } @Override public Dimension getPreferredSize() { return new Dimension(400, 400); } }