Guest User

Untitled

a guest
Aug 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. Java JFrame size and centre
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.Rectangle;
  6. import javax.swing.JComponent;
  7. import javax.swing.JFrame;
  8.  
  9. public class DiceSimulator {
  10.  
  11. public static void main(String[] args) {
  12. JFrame frame = new JFrame("DiceSimulator");
  13. frame.setVisible(true);
  14. frame.setSize(400, 300);
  15. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. draw object = new draw();
  17. frame.add(object);
  18. frame.setLocationRelativeTo(null);
  19. object.drawing();
  20. }
  21. }
  22.  
  23. import javax.swing.*;
  24. import java.awt.*;
  25. //import java.util.Random;
  26. public class draw extends JComponent {
  27.  
  28. public void drawing() {
  29. repaint();
  30. }
  31.  
  32. public void paintComponent(Graphics g) {
  33. super.paintComponent(g);
  34. //Random1 random = new Random1();
  35.  
  36. Graphics2D g2 = (Graphics2D) g;
  37. g.setColor(Color.BLACK);
  38. Rectangle box = new Rectangle(115, 60, 150, 150);
  39. g2.fill(box);
  40.  
  41. g.setColor(Color.WHITE);
  42. g.fillOval(145, 75, 30, 30);
  43.  
  44. g.setColor(Color.WHITE);
  45. g.fillOval(205, 75, 30, 30);
  46.  
  47. g.setColor(Color.WHITE);
  48. g.fillOval(145, 115, 30, 30);
  49.  
  50. g.setColor(Color.WHITE);
  51. g.fillOval(205, 115, 30, 30);
  52.  
  53. g.setColor(Color.WHITE);
  54. g.fillOval(145, 155, 30, 30);
  55.  
  56. g.setColor(Color.WHITE);
  57. g.fillOval(205, 155, 30, 30);
  58. }
  59. }
  60.  
  61. int width = getSize().width;
  62. int height = getSize().height;
  63.  
  64. g.translate(5, 5);
Add Comment
Please, Sign In to add comment