Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class SwingMain extends JPanel implements MouseListener {
- final static int SEPARATION = 125;
- final static int DIAMETER = 15;
- final static int NBALLS = 5;
- final static int WIDTH = (NBALLS + 1) * (SEPARATION + DIAMETER);
- final static int HEIGHT = (NBALLS + 1) * (SEPARATION);
- final static int XSTART = SEPARATION;
- final static int YSTART = SEPARATION;
- JFrame frame = new JFrame();
- public static void main(String[] args) {
- SwingUtilities.invokeLater(() -> new SwingMain().start());
- }
- public void start() {
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setPreferredSize(new Dimension(WIDTH, HEIGHT));
- frame.add(this);
- setBackground(Color.gray);
- frame.pack();
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2d = (Graphics2D) g;
- g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g2d.setColor(Color.white);
- int y = YSTART;
- for (int r = 0; r < NBALLS; r++) {
- int x = XSTART;
- for (int c = 0; c < NBALLS; c++) {
- g2d.fillOval(x, y, DIAMETER, DIAMETER);
- x += SEPARATION;
- }
- y += SEPARATION;
- }
- drawHorizontalLine(g2d, 0, 0);
- drawButton(g2d);
- }
- public void drawHorizontalLine(Graphics2D g, int r, int c) {
- int x1 = (SEPARATION) * (c + 1);
- int x2 = x1 + SEPARATION;
- int y1 = (SEPARATION) * (r + 1) + 2;
- int y2 = SEPARATION * (r + 1) + 6;
- drawRectangle(g, x1, y1, x2, y2, Color.WHITE);
- }
- public void drawRectangle(Graphics2D graphic, int x1, int y1, int x2, int y2, Color c) {
- graphic.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
- }
- private void drawButton(Graphics2D g) {
- int centerX = WIDTH / 2;
- int x1 = centerX - 100;
- int x2 = centerX + 100;
- int y1 = 25;
- int y2 = 75;
- drawRectangle(g, x1, y1, x2, y2, Color.WHITE);
- FontMetrics fm = g.getFontMetrics();
- g.setColor(Color.gray);
- g.setFont(new Font("TimesRoman", Font.BOLD, 20));
- int strWidth = fm.stringWidth("Completed.");
- int strHeight = fm.getAscent();
- g.drawString("Completed.",
- (centerX - 20) - strWidth / 2,
- 45 + strHeight);
- }
- public void mousePressed(MouseEvent e) {
- //If the area of the button is clicked
- System.out.println("Button clicked.");
- }
- public void mouseClicked(MouseEvent e) {}
- public void mouseReleased(MouseEvent e) {}
- public void mouseEntered(MouseEvent e) {}
- public void mouseExited(MouseEvent e) {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement