daily pastebin goal
11%
SHARE
TWEET

Untitled

a guest May 17th, 2018 111 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4.  
  5. public class DrawSwing {
  6.    
  7.     public static void main(String[] args) {
  8.         new DrawSwing();
  9.     }
  10.    
  11.     public DrawSwing() {
  12.         SwingUtilities.invokeLater(() -> createGUI());
  13.     }
  14.    
  15.     protected void createGUI() {
  16.     JFrame jf = new JFrame();
  17.     jf.setTitle("Rysowanie");
  18.     jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19.     jf.setLocation(50,50);
  20.     jf.setResizable(false);
  21.     jf.setLayout(new BorderLayout());
  22.     MyPanel p = new MyPanel();
  23.     JPanel buttonPanel = new JPanel();
  24.     JButton setButton = new JButton("Ustaw");
  25.     JTextField width = new JTextField();
  26.     width.setColumns(3);
  27.     JTextField height = new JTextField();
  28.     height.setColumns(3);
  29.     setButton.addActionListener(new ActionListener() {
  30.         public void actionPerformed(ActionEvent e) {
  31.                 p.setSizeLogo(Integer.parseInt(width.getText()), Integer.parseInt(height.getText()));
  32.                 jf.repaint();
  33.         }
  34.     });
  35.     buttonPanel.add(width);
  36.     buttonPanel.add(height);
  37.     buttonPanel.add(setButton);
  38.     jf.add(buttonPanel, BorderLayout.NORTH);
  39.     jf.add(p, BorderLayout.CENTER);
  40.     jf.pack();
  41.     jf.setVisible(true);
  42.     }
  43. }
  44.  
  45. class MyPanel extends JPanel {
  46.     private int width;
  47.     private int height;
  48.    
  49.     MyPanel() {
  50.         setPreferredSize(new Dimension(400,400));
  51.         width = 400;
  52.         height = 400;
  53.     }
  54.    
  55.     public void setSizeLogo(int width, int height) {
  56.         this.width = width;
  57.         this.height = height;
  58.     }
  59.    
  60.     public void paintComponent(Graphics g) {
  61.         super.paintComponent(g);
  62.         g.setColor(Color.red);
  63.         g.fillOval(0,0,width,height);
  64.         g.setColor(Color.white);
  65.         g.fillArc(width/20, height/20, width - (width/10), height - (height/10), 0, 180);
  66.     }
  67. }
RAW Paste Data
Top