Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class SwingMain extends JPanel implements MouseListener {
  6.  
  7. final static int SEPARATION = 125;
  8. final static int DIAMETER = 15;
  9. final static int NBALLS = 5;
  10. final static int WIDTH = (NBALLS + 1) * (SEPARATION + DIAMETER);
  11. final static int HEIGHT = (NBALLS + 1) * (SEPARATION);
  12. final static int XSTART = SEPARATION;
  13. final static int YSTART = SEPARATION;
  14. JFrame frame = new JFrame();
  15.  
  16. public static void main(String[] args) {
  17. SwingUtilities.invokeLater(() -> new SwingMain().start());
  18. }
  19.  
  20. public void start() {
  21. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22. setPreferredSize(new Dimension(WIDTH, HEIGHT));
  23. frame.add(this);
  24. setBackground(Color.gray);
  25. frame.pack();
  26. frame.setLocationRelativeTo(null);
  27. frame.setVisible(true);
  28. }
  29.  
  30. @Override
  31. public void paintComponent(Graphics g) {
  32. super.paintComponent(g);
  33. Graphics2D g2d = (Graphics2D) g;
  34.  
  35. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  36. g2d.setColor(Color.white);
  37.  
  38. int y = YSTART;
  39. for (int r = 0; r < NBALLS; r++) {
  40. int x = XSTART;
  41. for (int c = 0; c < NBALLS; c++) {
  42. g2d.fillOval(x, y, DIAMETER, DIAMETER);
  43. x += SEPARATION;
  44. }
  45. y += SEPARATION;
  46. }
  47.  
  48. drawHorizontalLine(g2d, 0, 0);
  49. drawButton(g2d);
  50. }
  51.  
  52. public void drawHorizontalLine(Graphics2D g, int r, int c) {
  53. int x1 = (SEPARATION) * (c + 1);
  54. int x2 = x1 + SEPARATION;
  55. int y1 = (SEPARATION) * (r + 1) + 2;
  56. int y2 = SEPARATION * (r + 1) + 6;
  57. drawRectangle(g, x1, y1, x2, y2, Color.WHITE);
  58. }
  59.  
  60. public void drawRectangle(Graphics2D graphic, int x1, int y1, int x2, int y2, Color c) {
  61. graphic.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
  62. }
  63.  
  64. private void drawButton(Graphics2D g) {
  65. int centerX = WIDTH / 2;
  66. int x1 = centerX - 100;
  67. int x2 = centerX + 100;
  68. int y1 = 25;
  69. int y2 = 75;
  70. drawRectangle(g, x1, y1, x2, y2, Color.WHITE);
  71. FontMetrics fm = g.getFontMetrics();
  72. g.setColor(Color.gray);
  73. g.setFont(new Font("TimesRoman", Font.BOLD, 20));
  74. int strWidth = fm.stringWidth("Completed.");
  75. int strHeight = fm.getAscent();
  76. g.drawString("Completed.",
  77. (centerX - 20) - strWidth / 2,
  78. 45 + strHeight);
  79. }
  80.  
  81. public void mousePressed(MouseEvent e) {
  82. //If the area of the button is clicked
  83. System.out.println("Button clicked.");
  84. }
  85. public void mouseClicked(MouseEvent e) {}
  86. public void mouseReleased(MouseEvent e) {}
  87. public void mouseEntered(MouseEvent e) {}
  88. public void mouseExited(MouseEvent e) {}
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement