Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.Scanner;
  6.  
  7. public class Classthingy implements ActionListener {
  8.  
  9. private static JTextField xTextField, yTextField, rTextField;
  10. private static JButton drawButton;
  11.  
  12. private static Panel mainPanel;
  13.  
  14. private static int x = 0, y = 0, r = 0;
  15.  
  16. public Classthingy() {
  17. //initializing all fields
  18. JFrame frame = new JFrame("Class thingy");
  19.  
  20. xTextField = new JTextField();
  21. yTextField = new JTextField();
  22. rTextField = new JTextField();
  23. drawButton = new JButton("Draw");
  24.  
  25. mainPanel = new Panel();
  26.  
  27. //setting correct sizes so it doesnt squeeze it too small
  28. xTextField.setPreferredSize(new Dimension(75, 20));
  29. yTextField.setPreferredSize(new Dimension(75, 20));
  30. rTextField.setPreferredSize(new Dimension(75, 20));
  31.  
  32. //adding action listener to update the panel
  33. drawButton.addActionListener(this);
  34.  
  35. //creating new panel so I can add new layout and make it look decent
  36. JPanel panel = new JPanel();
  37.  
  38. panel.setLayout(new GridLayout(1, 4));
  39.  
  40. panel.add(new JLabel("X:"));
  41. panel.add(xTextField);
  42. panel.add(new JLabel("Y:"));
  43. panel.add(yTextField);
  44. panel.add(new JLabel("R:"));
  45. panel.add(rTextField);
  46.  
  47. //adding components to frame in a order that makes sense
  48. frame.add(panel, BorderLayout.NORTH);
  49. frame.add(mainPanel, BorderLayout.CENTER);
  50. frame.add(drawButton, BorderLayout.SOUTH);
  51.  
  52. //setting up frame so it doesnt act funny
  53. frame.pack();
  54. frame.setLocationRelativeTo(null);
  55. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  56. frame.setVisible(true);
  57. }
  58.  
  59. @Override
  60. public void actionPerformed(ActionEvent e) {
  61. //setting x y and radius according to the values the user entered
  62. x = Integer.parseInt(xTextField.getText());
  63. y = Integer.parseInt(yTextField.getText());
  64. r = Integer.parseInt(rTextField.getText());
  65.  
  66. //manually calling repaint to get the panel to draw the circle
  67. mainPanel.repaint();
  68. }
  69.  
  70. public static void main(String[] args) {
  71. new Classthingy();
  72. }
  73.  
  74. //creating new instance of a JPanel because we need control of the paintComponent method
  75. private static class Panel extends JPanel {
  76. public Panel() {
  77. this.setPreferredSize(new Dimension(200, 200));
  78. repaint();
  79. }
  80.  
  81. //simply drawing a black background and drawing a red circle
  82. public void paintComponent(Graphics g) {
  83. g.setColor(Color.BLACK);
  84. g.fillRect(0, 0, getWidth(), getHeight());
  85. g.setColor(Color.RED);
  86. g.fillOval(x - r, y - r, r * 2, r * 2);
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement