Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Scanner;
- public class Classthingy implements ActionListener {
- private static JTextField xTextField, yTextField, rTextField;
- private static JButton drawButton;
- private static Panel mainPanel;
- private static int x = 0, y = 0, r = 0;
- public Classthingy() {
- //initializing all fields
- JFrame frame = new JFrame("Class thingy");
- xTextField = new JTextField();
- yTextField = new JTextField();
- rTextField = new JTextField();
- drawButton = new JButton("Draw");
- mainPanel = new Panel();
- //setting correct sizes so it doesnt squeeze it too small
- xTextField.setPreferredSize(new Dimension(75, 20));
- yTextField.setPreferredSize(new Dimension(75, 20));
- rTextField.setPreferredSize(new Dimension(75, 20));
- //adding action listener to update the panel
- drawButton.addActionListener(this);
- //creating new panel so I can add new layout and make it look decent
- JPanel panel = new JPanel();
- panel.setLayout(new GridLayout(1, 4));
- panel.add(new JLabel("X:"));
- panel.add(xTextField);
- panel.add(new JLabel("Y:"));
- panel.add(yTextField);
- panel.add(new JLabel("R:"));
- panel.add(rTextField);
- //adding components to frame in a order that makes sense
- frame.add(panel, BorderLayout.NORTH);
- frame.add(mainPanel, BorderLayout.CENTER);
- frame.add(drawButton, BorderLayout.SOUTH);
- //setting up frame so it doesnt act funny
- frame.pack();
- frame.setLocationRelativeTo(null);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- //setting x y and radius according to the values the user entered
- x = Integer.parseInt(xTextField.getText());
- y = Integer.parseInt(yTextField.getText());
- r = Integer.parseInt(rTextField.getText());
- //manually calling repaint to get the panel to draw the circle
- mainPanel.repaint();
- }
- public static void main(String[] args) {
- new Classthingy();
- }
- //creating new instance of a JPanel because we need control of the paintComponent method
- private static class Panel extends JPanel {
- public Panel() {
- this.setPreferredSize(new Dimension(200, 200));
- repaint();
- }
- //simply drawing a black background and drawing a red circle
- public void paintComponent(Graphics g) {
- g.setColor(Color.BLACK);
- g.fillRect(0, 0, getWidth(), getHeight());
- g.setColor(Color.RED);
- g.fillOval(x - r, y - r, r * 2, r * 2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement