From 8747b3fff803990c2b23691c258441641417f095 Mon Sep 17 00:00:00 2001 From: OKrylov Date: Thu, 6 May 2021 19:14:26 +0300 Subject: [PATCH 1/3] Lesson 8 --- .../java1/lesson8/CalculatorApp.java | 142 ++++++++++++++++++ .../geekbrains/java1/lesson8/CounterApp.java | 57 +++++++ .../lesson8/borderlayout/BorderLayout1.java | 39 +++++ .../lesson8/borderlayout/BorderLayout2.java | 29 ++++ .../lesson8/borderlayout/BorderLayout3.java | 27 ++++ .../lesson8/borderlayout/BorderLayout4.java | 27 ++++ .../java1/lesson8/events/KeyboardExample.java | 47 ++++++ .../lesson8/events/MouseEventExample.java | 39 +++++ .../java1/lesson8/examples/Example1.java | 41 +++++ .../java1/lesson8/examples/Example2.java | 40 +++++ .../lesson8/graphics/ButtonDrawExample.java | 69 +++++++++ .../lesson8/graphics/DrawingExample.java | 51 +++++++ 12 files changed, 608 insertions(+) create mode 100644 src/ru/geekbrains/java1/lesson8/CalculatorApp.java create mode 100644 src/ru/geekbrains/java1/lesson8/CounterApp.java create mode 100644 src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout1.java create mode 100644 src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout2.java create mode 100644 src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout3.java create mode 100644 src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout4.java create mode 100644 src/ru/geekbrains/java1/lesson8/events/KeyboardExample.java create mode 100644 src/ru/geekbrains/java1/lesson8/events/MouseEventExample.java create mode 100644 src/ru/geekbrains/java1/lesson8/examples/Example1.java create mode 100644 src/ru/geekbrains/java1/lesson8/examples/Example2.java create mode 100644 src/ru/geekbrains/java1/lesson8/graphics/ButtonDrawExample.java create mode 100644 src/ru/geekbrains/java1/lesson8/graphics/DrawingExample.java diff --git a/src/ru/geekbrains/java1/lesson8/CalculatorApp.java b/src/ru/geekbrains/java1/lesson8/CalculatorApp.java new file mode 100644 index 0000000..acff3a9 --- /dev/null +++ b/src/ru/geekbrains/java1/lesson8/CalculatorApp.java @@ -0,0 +1,142 @@ +package ru.geekbrains.java1.lesson8; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class CalculatorApp extends JFrame { + private JTextField textField; + + public CalculatorApp() { + initComponents(); + setTitle("Calculator"); + setBounds(0, 0, 300, 500); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setResizable(false); + + setLayout(new BorderLayout()); + + add(initTop(), BorderLayout.NORTH); + add(initBottom(), BorderLayout.CENTER); + + setVisible(true); + } + + private void initComponents() { + textField = new JTextField(); + } + + private JPanel initTop() { + JPanel top = new JPanel(); + top.setLayout(new BorderLayout()); + + textField = new JTextField(); + textField.setEditable(false); + top.add(textField, BorderLayout.CENTER); + return top; + } + + private JPanel initBottom() { + JPanel bottom = new JPanel(); + bottom.setLayout(new GridLayout(7, 3)); + + ActionListener operatorButtonListener = new OperatorsButtonListener(textField); + + for (int i = 1; i <= 9; i++) { + JButton btn = new JButton(String.valueOf(i)); + btn.addActionListener(operatorButtonListener); + bottom.add(btn); + } + + + JButton submit = new JButton("="); + + submit.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String value = textField.getText(); + String[] operators; + if (value.contains("+")) { + operators = value.split("\\+"); + textField.setText( + String.valueOf( + Double.parseDouble(operators[0]) + Double.parseDouble(operators[1]) + ) + ); + } else if (value.contains("-")) { + operators = value.split("-"); + textField.setText( + String.valueOf( + Double.parseDouble(operators[0]) - Double.parseDouble(operators[1]) + ) + ); + } else if (value.contains("*")) { + operators = value.split("\\*"); + textField.setText( + String.valueOf( + Double.parseDouble(operators[0]) * Double.parseDouble(operators[1]) + ) + ); + } else if (value.contains("/")) { + operators = value.split("/"); + textField.setText( + String.valueOf( + Double.parseDouble(operators[0]) / Double.parseDouble(operators[1]) + ) + ); + } else if (value.contains("^")) { + operators = value.split("\\^"); + textField.setText( + String.valueOf( + Math.pow(Double.parseDouble(operators[0]), Double.parseDouble(operators[1])) + ) + ); + } + } + }); + + + JButton zero = new JButton("0"); + zero.addActionListener(operatorButtonListener); + JButton clear = new JButton("C"); + clear.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + textField.setText(""); + } + }); + + JButton plus = new JButton("+"); + plus.addActionListener(operatorButtonListener); + JButton minus = new JButton("-"); + minus.addActionListener(operatorButtonListener); + JButton multiply = new JButton("*"); + multiply.addActionListener(operatorButtonListener); + JButton division = new JButton("/"); + division.addActionListener(operatorButtonListener); + JButton pointer = new JButton("."); + pointer.addActionListener(operatorButtonListener); + JButton sqrt = new JButton("√"); + sqrt.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + textField.setText(String.valueOf(Math.sqrt(Double.parseDouble(textField.getText())))); + } + }); + JButton sqr = new JButton("^"); + sqr.addActionListener(operatorButtonListener); + + bottom.add(submit); + bottom.add(zero); + bottom.add(clear); + bottom.add(plus); + bottom.add(minus); + bottom.add(multiply); + bottom.add(division); + bottom.add(pointer); + bottom.add(sqrt); + bottom.add(sqr); + return bottom; + } +} \ No newline at end of file diff --git a/src/ru/geekbrains/java1/lesson8/CounterApp.java b/src/ru/geekbrains/java1/lesson8/CounterApp.java new file mode 100644 index 0000000..bbc1536 --- /dev/null +++ b/src/ru/geekbrains/java1/lesson8/CounterApp.java @@ -0,0 +1,57 @@ +package ru.geekbrains.java1.lesson8; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class CounterApp extends JFrame { + private int value; + + public CounterApp(int initialValue) { + setBounds(500, 500, 300, 120); + setTitle("Simple Counter"); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + + Font font = new Font("Arial", Font.BOLD, 32); + + JLabel counterValueView = new JLabel(); + counterValueView.setFont(font); + counterValueView.setHorizontalAlignment(SwingConstants.CENTER); + add(counterValueView, BorderLayout.CENTER); + + value = initialValue; + counterValueView.setText(String.valueOf(value)); + + JButton decrementButton = new JButton("<"); + decrementButton.setFont(font); + add(decrementButton, BorderLayout.WEST); + + JButton incrementButton = new JButton(">"); + incrementButton.setFont(font); + add(incrementButton, BorderLayout.EAST); + + decrementButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + value--; + counterValueView.setText(String.valueOf(value)); + } + }); + + incrementButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + value++; + counterValueView.setText(String.valueOf(value)); + } + }); + + setVisible(true); + } + + public static void main(String[] args) { + new CounterApp(0); + } +} + diff --git a/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout1.java b/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout1.java new file mode 100644 index 0000000..3d2745a --- /dev/null +++ b/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout1.java @@ -0,0 +1,39 @@ +package ru.geekbrains.java1.lesson8.borderlayout; + +import javax.swing.*; +import java.awt.*; + +public class BorderLayout1 { + + static class MyWindow extends JFrame { + public MyWindow() { + setTitle("Test Window"); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setSize(800, 800); + setLocationRelativeTo(null); + + JButton button = new JButton("Button 1 (PAGE_START)"); + add(button, BorderLayout.PAGE_START); + + button = new JButton("Button 2 (CENTER)"); + add(button, BorderLayout.CENTER); + + button = new JButton("Button 3 (LINE_START)"); + button.setSize(new Dimension(200, 200)); + add(button, BorderLayout.LINE_START); + + button = new JButton("Long-Named Button 4 (PAGE_END)"); + add(button, BorderLayout.PAGE_END); + + button = new JButton("5 (LINE_END)"); + add(button, BorderLayout.LINE_END); + + setVisible(true); + } + } + + public static void main(String[] args) { + new MyWindow(); + } + +} \ No newline at end of file diff --git a/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout2.java b/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout2.java new file mode 100644 index 0000000..db201c6 --- /dev/null +++ b/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout2.java @@ -0,0 +1,29 @@ +package ru.geekbrains.java1.lesson8.borderlayout; + +import javax.swing.*; +import java.awt.*; + +public class BorderLayout2 { + + static public class MyWindow extends JFrame { + public MyWindow() { + setBounds(500,500,500,300); + setTitle("BoxLayoutDemo"); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + JButton[] jbs = new JButton[10]; + setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); // одну из строк надо закомментировать +// setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS)); // одну из строк надо закомментировать + for (int i = 0; i < jbs.length; i++) { + jbs[i] = new JButton("#" + i); + jbs[i].setAlignmentX(Component.CENTER_ALIGNMENT); + add(jbs[i]); + } + setVisible(true); + } + } + + public static void main(String[] args) { + new MyWindow(); + } + +} \ No newline at end of file diff --git a/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout3.java b/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout3.java new file mode 100644 index 0000000..399cab1 --- /dev/null +++ b/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout3.java @@ -0,0 +1,27 @@ +package ru.geekbrains.java1.lesson8.borderlayout; + +import javax.swing.*; +import java.awt.*; + +public class BorderLayout3 { + + static public class MyWindow extends JFrame { + public MyWindow() { + setBounds(500, 500, 400, 300); + setTitle("FlowLayoutDemo"); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + JButton[] jbs = new JButton[10]; + setLayout(new FlowLayout()); + for (int i = 0; i < jbs.length; i++) { + jbs[i] = new JButton("#" + i); + add(jbs[i]); + } + setVisible(true); + } + } + + public static void main(String[] args) { + new MyWindow(); + } + +} \ No newline at end of file diff --git a/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout4.java b/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout4.java new file mode 100644 index 0000000..0d62ef3 --- /dev/null +++ b/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout4.java @@ -0,0 +1,27 @@ +package ru.geekbrains.java1.lesson8.borderlayout; + +import javax.swing.*; +import java.awt.*; + +public class BorderLayout4 { + + static public class MyWindow extends JFrame { + public MyWindow() { + setBounds(500,500,400,300); + setTitle("GridLayoutDemo"); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + JButton[] jbs = new JButton[10]; + setLayout(new GridLayout(4, 3)); + for (int i = 0; i < jbs.length; i++) { + jbs[i] = new JButton("#" + i); + add(jbs[i]); + } + setVisible(true); + } + } + + public static void main(String[] args) { + new MyWindow(); + } + +} \ No newline at end of file diff --git a/src/ru/geekbrains/java1/lesson8/events/KeyboardExample.java b/src/ru/geekbrains/java1/lesson8/events/KeyboardExample.java new file mode 100644 index 0000000..7a7d374 --- /dev/null +++ b/src/ru/geekbrains/java1/lesson8/events/KeyboardExample.java @@ -0,0 +1,47 @@ +package ru.geekbrains.java1.lesson8.events; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; + +public class KeyboardExample { + + static public class MyWindow extends JFrame { + public MyWindow() { + setBounds(500, 500, 400, 300); + setTitle("Demo"); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + JTextField field = new JTextField(); + add(field); + field.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + System.out.println("Your message: " + field.getText()); + field.setText(null); + } + }); + + field.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + if (e.isShiftDown()) { + System.out.println("Shift down"); + } else { + System.out.println(e.getKeyChar() + " down"); + } + } + }); + + setVisible(true); + } + + } + + + public static void main(String[] args) { + new MyWindow(); + } + +} \ No newline at end of file diff --git a/src/ru/geekbrains/java1/lesson8/events/MouseEventExample.java b/src/ru/geekbrains/java1/lesson8/events/MouseEventExample.java new file mode 100644 index 0000000..d04f191 --- /dev/null +++ b/src/ru/geekbrains/java1/lesson8/events/MouseEventExample.java @@ -0,0 +1,39 @@ +package ru.geekbrains.java1.lesson8.events; + +import javax.swing.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +public class MouseEventExample { + + static public class MyWindow extends JFrame { + public MyWindow() { + setBounds(500, 500, 400, 300); + setTitle("Demo"); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + JPanel pan = new JPanel(); + add(pan); + pan.addMouseListener(new MouseAdapter() { + @Override + public void mouseReleased(MouseEvent e) { + System.out.println("MousePos: " + e.getX() + " " + e.getY()); + } + + @Override + public void mouseExited(MouseEvent e) { + System.out.println("Exited!"); + } + }); + + + + setVisible(true); + } + } + + + public static void main(String[] args) { + new MyWindow(); + } + +} \ No newline at end of file diff --git a/src/ru/geekbrains/java1/lesson8/examples/Example1.java b/src/ru/geekbrains/java1/lesson8/examples/Example1.java new file mode 100644 index 0000000..319e241 --- /dev/null +++ b/src/ru/geekbrains/java1/lesson8/examples/Example1.java @@ -0,0 +1,41 @@ +package ru.geekbrains.java1.lesson8.examples; + +import javax.swing.*; +import java.awt.*; + +public class Example1 { + + static class MyWindow extends JFrame { + public MyWindow() { + setTitle("Test Window"); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setLocationRelativeTo(null); + setSize(400, 400); +// setLocation(2300, 300); +// setBounds(2400, 300, 400, 400); + setVisible(true); + } + } + + private static class WindowRunnable implements /* extends */ Runnable { + + @Override + public void run() { + new MyWindow(); + } + } + + public static void main(String[] args) { +// new MyWindow(); +// EventQueue.invokeLater(new WindowRunnable()); + EventQueue.invokeLater(new Runnable() { + @Override + public void run() { + new MyWindow(); + } + }); + + } + + +} diff --git a/src/ru/geekbrains/java1/lesson8/examples/Example2.java b/src/ru/geekbrains/java1/lesson8/examples/Example2.java new file mode 100644 index 0000000..dba0118 --- /dev/null +++ b/src/ru/geekbrains/java1/lesson8/examples/Example2.java @@ -0,0 +1,40 @@ +package ru.geekbrains.java1.lesson8.examples; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class Example2 { + + static class MyWindow extends JFrame { + public MyWindow() { + setTitle("Test Window"); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setBounds(300, 300, 400, 400); + + JButton[] jbs = new JButton[5]; + for (int i = 0; i < 5; i++) { + jbs[i] = new JButton("#" + i); + jbs[i].addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + System.out.println(((JButton)e.getSource()).getText() + " was pressed"); + } + }); + } + setLayout(new BorderLayout()); // выбор компоновщика элементов + add(jbs[0], BorderLayout.EAST); // добавление кнопки на форму + add(jbs[1], BorderLayout.WEST); + add(jbs[2], BorderLayout.SOUTH); + add(jbs[3], BorderLayout.NORTH); + add(jbs[4], BorderLayout.CENTER); + setVisible(true); + } + } + + public static void main(String[] args) { + new MyWindow(); + } + +} diff --git a/src/ru/geekbrains/java1/lesson8/graphics/ButtonDrawExample.java b/src/ru/geekbrains/java1/lesson8/graphics/ButtonDrawExample.java new file mode 100644 index 0000000..7585ad3 --- /dev/null +++ b/src/ru/geekbrains/java1/lesson8/graphics/ButtonDrawExample.java @@ -0,0 +1,69 @@ +package ru.geekbrains.java1.lesson8.graphics; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class ButtonDrawExample { + static class MyWindow extends JFrame { + + private static final String DRAW_X = "DRAW_X"; + private static final String DRAW_O = "DRAW_O"; + + public MyWindow() { + setSize(800,500); + setDefaultCloseOperation(EXIT_ON_CLOSE); + setLocationRelativeTo(null); + + setLayout(new GridLayout(1, 3)); + add(createButton()); + add(createButton()); + add(createButton()); + + setVisible(true); + } + + private JButton createButton() { + return new JButton() { + + { + setActionCommand(DRAW_O); + addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String action = getActionCommand().equals(DRAW_O) ? DRAW_X : DRAW_O; + setActionCommand(action); + } + }); + } + + @Override + public void paint(Graphics graphics) { + super.paint(graphics); + + if (getActionCommand().equals(DRAW_O)) { + graphics.drawOval(0, this.getHeight() / 4, getWidth(), getWidth()); + graphics.setColor(Color.RED); + graphics.fillOval(0, this.getHeight() / 4, getWidth(), getWidth()); + } + else { + Graphics2D g2d = (Graphics2D) graphics; + g2d.setStroke(new BasicStroke(10)); + g2d.setColor(Color.BLUE); + g2d.drawLine(0, 0, this.getWidth(), this.getHeight()); + g2d.drawLine(this.getWidth(), 0, 0, this.getHeight()); + } + } + }; + + } + + } + + + public static void main(String[] args){ + new MyWindow(); + } + +} diff --git a/src/ru/geekbrains/java1/lesson8/graphics/DrawingExample.java b/src/ru/geekbrains/java1/lesson8/graphics/DrawingExample.java new file mode 100644 index 0000000..5b01de2 --- /dev/null +++ b/src/ru/geekbrains/java1/lesson8/graphics/DrawingExample.java @@ -0,0 +1,51 @@ +package ru.geekbrains.java1.lesson8.graphics; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +public class DrawingExample extends JFrame { + + public DrawingExample() { + super("Lines Drawing Demo"); + + setSize(680, 600); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLocationRelativeTo(null); + setAlwaysOnTop(true); + addMouseListener(new MouseAdapter() { + @Override + public void mouseReleased(MouseEvent e) { + System.out.println("MousePos: " + e.getX() + " " + e.getY()); + }}); + } + + @Override + public void paint(Graphics g) { + Graphics2D g2D = (Graphics2D) g; + super.paint(g); + simpleDrawLines(g); + } + + private void simpleDrawLines(Graphics g2d) { + g2d.drawLine(120, 50, 360, 50); + g2d.setColor(Color.RED); + g2d.drawRect(150, 150, 150, 150); + + g2d.setColor(Color.GREEN); + g2d.fillPolygon(new int[] {400}, new int[] {400}, 1); + g2d.fillRect(151, 151, 149, 75); + + g2d.drawLine(350, 50, 350, 550); + g2d.drawLine(250, 150, 550, 150); + g2d.drawOval(350, 150, 150, 150); + g2d.setColor(Color.RED); + g2d.fillOval(351, 151, 149, 149); + g2d.clearRect(350, 150, 75, 75); + } + + public static void main(String[] args) { + new DrawingExample().setVisible(true); + } +} \ No newline at end of file From de029df4ad483aadcc87c764cc55be13870f1d51 Mon Sep 17 00:00:00 2001 From: OKrylov Date: Thu, 6 May 2021 22:26:41 +0300 Subject: [PATCH 2/3] Lesson 8 --- .idea/misc.xml | 2 +- .../java1/lesson8/CalculatorApp.java | 142 ------------------ .../geekbrains/java1/lesson8/CounterApp.java | 21 ++- .../lesson8/borderlayout/BorderLayout2.java | 7 +- .../lesson8/borderlayout/BorderLayout3.java | 1 + .../java1/lesson8/events/KeyboardExample.java | 16 +- .../lesson8/events/MouseEventExample.java | 2 - .../java1/lesson8/examples/Example1.java | 2 +- .../java1/lesson8/examples/Example2.java | 14 +- .../lesson8/graphics/DrawingExample.java | 5 +- 10 files changed, 42 insertions(+), 170 deletions(-) delete mode 100644 src/ru/geekbrains/java1/lesson8/CalculatorApp.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 5c10972..501b26a 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/ru/geekbrains/java1/lesson8/CalculatorApp.java b/src/ru/geekbrains/java1/lesson8/CalculatorApp.java deleted file mode 100644 index acff3a9..0000000 --- a/src/ru/geekbrains/java1/lesson8/CalculatorApp.java +++ /dev/null @@ -1,142 +0,0 @@ -package ru.geekbrains.java1.lesson8; - -import javax.swing.*; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -public class CalculatorApp extends JFrame { - private JTextField textField; - - public CalculatorApp() { - initComponents(); - setTitle("Calculator"); - setBounds(0, 0, 300, 500); - setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); - setResizable(false); - - setLayout(new BorderLayout()); - - add(initTop(), BorderLayout.NORTH); - add(initBottom(), BorderLayout.CENTER); - - setVisible(true); - } - - private void initComponents() { - textField = new JTextField(); - } - - private JPanel initTop() { - JPanel top = new JPanel(); - top.setLayout(new BorderLayout()); - - textField = new JTextField(); - textField.setEditable(false); - top.add(textField, BorderLayout.CENTER); - return top; - } - - private JPanel initBottom() { - JPanel bottom = new JPanel(); - bottom.setLayout(new GridLayout(7, 3)); - - ActionListener operatorButtonListener = new OperatorsButtonListener(textField); - - for (int i = 1; i <= 9; i++) { - JButton btn = new JButton(String.valueOf(i)); - btn.addActionListener(operatorButtonListener); - bottom.add(btn); - } - - - JButton submit = new JButton("="); - - submit.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - String value = textField.getText(); - String[] operators; - if (value.contains("+")) { - operators = value.split("\\+"); - textField.setText( - String.valueOf( - Double.parseDouble(operators[0]) + Double.parseDouble(operators[1]) - ) - ); - } else if (value.contains("-")) { - operators = value.split("-"); - textField.setText( - String.valueOf( - Double.parseDouble(operators[0]) - Double.parseDouble(operators[1]) - ) - ); - } else if (value.contains("*")) { - operators = value.split("\\*"); - textField.setText( - String.valueOf( - Double.parseDouble(operators[0]) * Double.parseDouble(operators[1]) - ) - ); - } else if (value.contains("/")) { - operators = value.split("/"); - textField.setText( - String.valueOf( - Double.parseDouble(operators[0]) / Double.parseDouble(operators[1]) - ) - ); - } else if (value.contains("^")) { - operators = value.split("\\^"); - textField.setText( - String.valueOf( - Math.pow(Double.parseDouble(operators[0]), Double.parseDouble(operators[1])) - ) - ); - } - } - }); - - - JButton zero = new JButton("0"); - zero.addActionListener(operatorButtonListener); - JButton clear = new JButton("C"); - clear.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - textField.setText(""); - } - }); - - JButton plus = new JButton("+"); - plus.addActionListener(operatorButtonListener); - JButton minus = new JButton("-"); - minus.addActionListener(operatorButtonListener); - JButton multiply = new JButton("*"); - multiply.addActionListener(operatorButtonListener); - JButton division = new JButton("/"); - division.addActionListener(operatorButtonListener); - JButton pointer = new JButton("."); - pointer.addActionListener(operatorButtonListener); - JButton sqrt = new JButton("√"); - sqrt.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - textField.setText(String.valueOf(Math.sqrt(Double.parseDouble(textField.getText())))); - } - }); - JButton sqr = new JButton("^"); - sqr.addActionListener(operatorButtonListener); - - bottom.add(submit); - bottom.add(zero); - bottom.add(clear); - bottom.add(plus); - bottom.add(minus); - bottom.add(multiply); - bottom.add(division); - bottom.add(pointer); - bottom.add(sqrt); - bottom.add(sqr); - return bottom; - } -} \ No newline at end of file diff --git a/src/ru/geekbrains/java1/lesson8/CounterApp.java b/src/ru/geekbrains/java1/lesson8/CounterApp.java index bbc1536..3106217 100644 --- a/src/ru/geekbrains/java1/lesson8/CounterApp.java +++ b/src/ru/geekbrains/java1/lesson8/CounterApp.java @@ -12,30 +12,31 @@ public CounterApp(int initialValue) { setBounds(500, 500, 300, 120); setTitle("Simple Counter"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setAlwaysOnTop(true); Font font = new Font("Arial", Font.BOLD, 32); - JLabel counterValueView = new JLabel(); - counterValueView.setFont(font); + JLabel counterValueView = new JLabel(String.valueOf(value)); counterValueView.setHorizontalAlignment(SwingConstants.CENTER); + counterValueView.setFont(font); add(counterValueView, BorderLayout.CENTER); value = initialValue; - counterValueView.setText(String.valueOf(value)); + refreshCounterLabel(counterValueView); JButton decrementButton = new JButton("<"); + add(decrementButton, BorderLayout.LINE_START); decrementButton.setFont(font); - add(decrementButton, BorderLayout.WEST); JButton incrementButton = new JButton(">"); + add(incrementButton, BorderLayout.LINE_END); incrementButton.setFont(font); - add(incrementButton, BorderLayout.EAST); decrementButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { value--; - counterValueView.setText(String.valueOf(value)); + refreshCounterLabel(counterValueView); } }); @@ -43,14 +44,18 @@ public void actionPerformed(ActionEvent actionEvent) { @Override public void actionPerformed(ActionEvent actionEvent) { value++; - counterValueView.setText(String.valueOf(value)); + refreshCounterLabel(counterValueView); } }); setVisible(true); } - public static void main(String[] args) { + private void refreshCounterLabel(JLabel counterValueView) { + counterValueView.setText(String.valueOf(value)); + } + + public static void main(String[] args) { new CounterApp(0); } } diff --git a/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout2.java b/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout2.java index db201c6..2029fa5 100644 --- a/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout2.java +++ b/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout2.java @@ -11,11 +11,12 @@ public MyWindow() { setTitle("BoxLayoutDemo"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); JButton[] jbs = new JButton[10]; - setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); // одну из строк надо закомментировать -// setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS)); // одну из строк надо закомментировать +// setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); // одну из строк надо закомментировать + setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS)); // одну из строк надо закомментировать for (int i = 0; i < jbs.length; i++) { jbs[i] = new JButton("#" + i); - jbs[i].setAlignmentX(Component.CENTER_ALIGNMENT); +// jbs[i].setAlignmentX(Component.CENTER_ALIGNMENT); + jbs[i].setAlignmentY(Component.CENTER_ALIGNMENT); add(jbs[i]); } setVisible(true); diff --git a/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout3.java b/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout3.java index 399cab1..7281a11 100644 --- a/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout3.java +++ b/src/ru/geekbrains/java1/lesson8/borderlayout/BorderLayout3.java @@ -14,6 +14,7 @@ public MyWindow() { setLayout(new FlowLayout()); for (int i = 0; i < jbs.length; i++) { jbs[i] = new JButton("#" + i); + jbs[i].setPreferredSize(new Dimension(100, 30)); add(jbs[i]); } setVisible(true); diff --git a/src/ru/geekbrains/java1/lesson8/events/KeyboardExample.java b/src/ru/geekbrains/java1/lesson8/events/KeyboardExample.java index 7a7d374..0abdeea 100644 --- a/src/ru/geekbrains/java1/lesson8/events/KeyboardExample.java +++ b/src/ru/geekbrains/java1/lesson8/events/KeyboardExample.java @@ -1,6 +1,7 @@ package ru.geekbrains.java1.lesson8.events; import javax.swing.*; +import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; @@ -14,7 +15,7 @@ public MyWindow() { setTitle("Demo"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); JTextField field = new JTextField(); - add(field); + add(field, BorderLayout.PAGE_START); field.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -24,13 +25,22 @@ public void actionPerformed(ActionEvent e) { }); field.addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + System.out.println("key pressed"); + } + + @Override + public void keyReleased(KeyEvent e) { + System.out.println("key released"); + } + @Override public void keyTyped(KeyEvent e) { if (e.isShiftDown()) { System.out.println("Shift down"); - } else { - System.out.println(e.getKeyChar() + " down"); } + System.out.println(e.getKeyChar() + " down"); } }); diff --git a/src/ru/geekbrains/java1/lesson8/events/MouseEventExample.java b/src/ru/geekbrains/java1/lesson8/events/MouseEventExample.java index d04f191..fa3cc15 100644 --- a/src/ru/geekbrains/java1/lesson8/events/MouseEventExample.java +++ b/src/ru/geekbrains/java1/lesson8/events/MouseEventExample.java @@ -25,8 +25,6 @@ public void mouseExited(MouseEvent e) { } }); - - setVisible(true); } } diff --git a/src/ru/geekbrains/java1/lesson8/examples/Example1.java b/src/ru/geekbrains/java1/lesson8/examples/Example1.java index 319e241..66c8a56 100644 --- a/src/ru/geekbrains/java1/lesson8/examples/Example1.java +++ b/src/ru/geekbrains/java1/lesson8/examples/Example1.java @@ -11,7 +11,7 @@ public MyWindow() { setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setLocationRelativeTo(null); setSize(400, 400); -// setLocation(2300, 300); + setLocation(300, 150); // setBounds(2400, 300, 400, 400); setVisible(true); } diff --git a/src/ru/geekbrains/java1/lesson8/examples/Example2.java b/src/ru/geekbrains/java1/lesson8/examples/Example2.java index dba0118..e41a3f3 100644 --- a/src/ru/geekbrains/java1/lesson8/examples/Example2.java +++ b/src/ru/geekbrains/java1/lesson8/examples/Example2.java @@ -16,14 +16,14 @@ public MyWindow() { JButton[] jbs = new JButton[5]; for (int i = 0; i < 5; i++) { jbs[i] = new JButton("#" + i); - jbs[i].addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - System.out.println(((JButton)e.getSource()).getText() + " was pressed"); - } - }); +// jbs[i].addActionListener(new ActionListener() { +// @Override +// public void actionPerformed(ActionEvent e) { +// System.out.println(((JButton)e.getSource()).getText() + " was pressed"); +// } +// }); } - setLayout(new BorderLayout()); // выбор компоновщика элементов +// setLayout(new BorderLayout()); // выбор компоновщика элементов add(jbs[0], BorderLayout.EAST); // добавление кнопки на форму add(jbs[1], BorderLayout.WEST); add(jbs[2], BorderLayout.SOUTH); diff --git a/src/ru/geekbrains/java1/lesson8/graphics/DrawingExample.java b/src/ru/geekbrains/java1/lesson8/graphics/DrawingExample.java index 5b01de2..37f8243 100644 --- a/src/ru/geekbrains/java1/lesson8/graphics/DrawingExample.java +++ b/src/ru/geekbrains/java1/lesson8/graphics/DrawingExample.java @@ -24,6 +24,7 @@ public void mouseReleased(MouseEvent e) { @Override public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g; + super.paint(g); simpleDrawLines(g); } @@ -37,12 +38,10 @@ private void simpleDrawLines(Graphics g2d) { g2d.fillPolygon(new int[] {400}, new int[] {400}, 1); g2d.fillRect(151, 151, 149, 75); - g2d.drawLine(350, 50, 350, 550); - g2d.drawLine(250, 150, 550, 150); g2d.drawOval(350, 150, 150, 150); g2d.setColor(Color.RED); g2d.fillOval(351, 151, 149, 149); - g2d.clearRect(350, 150, 75, 75); +// g2d.clearRect(350, 150, 75, 75); } public static void main(String[] args) { From ad59b7b0740e46c7b1cd672c7a6a2bb56f7e0c34 Mon Sep 17 00:00:00 2001 From: OKrylov Date: Sun, 16 May 2021 21:32:22 +0300 Subject: [PATCH 3/3] Lesson 8 - homework --- .../geekbrains/java1/lesson4/TicTacToe.java | 23 +-- .../geekbrains/java1/lesson8/CounterApp.java | 124 +++++++++------ .../java1/lesson8/TicTacToeGui.java | 146 ++++++++++++++++++ 3 files changed, 236 insertions(+), 57 deletions(-) create mode 100644 src/ru/geekbrains/java1/lesson8/TicTacToeGui.java diff --git a/src/ru/geekbrains/java1/lesson4/TicTacToe.java b/src/ru/geekbrains/java1/lesson4/TicTacToe.java index b7b65ff..abe2e67 100644 --- a/src/ru/geekbrains/java1/lesson4/TicTacToe.java +++ b/src/ru/geekbrains/java1/lesson4/TicTacToe.java @@ -6,14 +6,14 @@ public class TicTacToe { - private static final int SIZE = 5; - private static final int DOTS_TO_WIN = 4; + public static final int SIZE = 3; + private static final int DOTS_TO_WIN = 3; - private static final char DOT_EMPTY = '•'; - private static final char DOT_X = 'X'; - private static final char DOT_O = 'O'; + public static final char DOT_EMPTY = '•'; + public static final char DOT_X = 'X'; + public static final char DOT_O = 'O'; - private static final char[][] map = new char[SIZE][SIZE]; + public static final char[][] map = new char[SIZE][SIZE]; public static void main(String[] args) { @@ -64,11 +64,11 @@ private static boolean isGameMoveWinning(int rowIndex, int colIndex, char symbol return result; } - private static void setCell(int rowIndex, int colIndex, char symbol) { + public static void setCell(int rowIndex, int colIndex, char symbol) { map[rowIndex][colIndex] = symbol; } - private static boolean isMapFull() { + public static boolean isMapFull() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (map[i][j] == DOT_EMPTY) { @@ -80,7 +80,7 @@ private static boolean isMapFull() { } - private static boolean isWin(char symbol) { + public static boolean isWin(char symbol) { if (checkRowsAndCols(symbol)) { return true; } else { @@ -122,7 +122,7 @@ private static void printMap() { System.out.println(); } - private static void computerTurn() { + public static int[] computerTurn() { int[] cell = getNextCellToWin(DOT_O); if (cell == null) { cell = getNextCellToWin(DOT_X); @@ -134,6 +134,7 @@ private static void computerTurn() { int colIndex = cell[1]; setCell(rowIndex, colIndex, DOT_O); + return cell; } private static int[] getRandomEmptyCell() { @@ -199,7 +200,7 @@ private static void printHeader() { System.out.println(); } - private static void initMap() { + public static void initMap() { for (int i = 0; i < SIZE; i++) { Arrays.fill(map[i], DOT_EMPTY); } diff --git a/src/ru/geekbrains/java1/lesson8/CounterApp.java b/src/ru/geekbrains/java1/lesson8/CounterApp.java index 3106217..34dc2f8 100644 --- a/src/ru/geekbrains/java1/lesson8/CounterApp.java +++ b/src/ru/geekbrains/java1/lesson8/CounterApp.java @@ -1,62 +1,94 @@ package ru.geekbrains.java1.lesson8; import javax.swing.*; +import javax.swing.text.NumberFormatter; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.text.NumberFormat; public class CounterApp extends JFrame { - private int value; - - public CounterApp(int initialValue) { - setBounds(500, 500, 300, 120); - setTitle("Simple Counter"); - setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); - setAlwaysOnTop(true); - - Font font = new Font("Arial", Font.BOLD, 32); - - JLabel counterValueView = new JLabel(String.valueOf(value)); - counterValueView.setHorizontalAlignment(SwingConstants.CENTER); - counterValueView.setFont(font); - add(counterValueView, BorderLayout.CENTER); - - value = initialValue; - refreshCounterLabel(counterValueView); - - JButton decrementButton = new JButton("<"); - add(decrementButton, BorderLayout.LINE_START); - decrementButton.setFont(font); - - JButton incrementButton = new JButton(">"); - add(incrementButton, BorderLayout.LINE_END); - incrementButton.setFont(font); - - decrementButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent actionEvent) { - value--; - refreshCounterLabel(counterValueView); - } - }); - - incrementButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent actionEvent) { - value++; - refreshCounterLabel(counterValueView); - } - }); - - setVisible(true); - } + private int value; + private int step = 1; + + public CounterApp(int initialValue) { + setBounds(500, 500, 300, 120); + setTitle("Simple Counter"); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setAlwaysOnTop(true); + + Font font = new Font("Arial", Font.BOLD, 32); + + JLabel counterValueView = new JLabel(String.valueOf(value)); + counterValueView.setHorizontalAlignment(SwingConstants.CENTER); + counterValueView.setFont(font); + add(counterValueView, BorderLayout.CENTER); + + value = initialValue; + refreshCounterLabel(counterValueView); + + JButton decrementButton = new JButton("<"); + add(decrementButton, BorderLayout.LINE_START); + decrementButton.setFont(font); + + JButton incrementButton = new JButton(">"); + add(incrementButton, BorderLayout.LINE_END); + incrementButton.setFont(font); + + NumberFormat longFormat = NumberFormat.getIntegerInstance(); + + NumberFormatter numberFormatter = new NumberFormatter(longFormat); + numberFormatter.setAllowsInvalid(false); //this is the key!! + + JFormattedTextField stepTextField = new JFormattedTextField(numberFormatter); + stepTextField.setHorizontalAlignment(SwingConstants.CENTER); + stepTextField.addKeyListener(new KeyAdapter() { + @Override + public void keyReleased(KeyEvent e) { + step = Integer.parseInt(stepTextField.getText()); + } + }); + add(stepTextField, BorderLayout.NORTH); + + decrementButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + value -= step; + refreshCounterLabel(counterValueView); + } + }); + + incrementButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + value += step; + refreshCounterLabel(counterValueView); + } + }); + + JButton clearButton = new JButton("Reset"); + clearButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + step = 1; + value = 0; + stepTextField.setValue(step); + refreshCounterLabel(counterValueView); + } + }); + add(clearButton, BorderLayout.SOUTH); + + setVisible(true); + } private void refreshCounterLabel(JLabel counterValueView) { counterValueView.setText(String.valueOf(value)); } public static void main(String[] args) { - new CounterApp(0); - } + new CounterApp(0); + } } diff --git a/src/ru/geekbrains/java1/lesson8/TicTacToeGui.java b/src/ru/geekbrains/java1/lesson8/TicTacToeGui.java new file mode 100644 index 0000000..1c439b5 --- /dev/null +++ b/src/ru/geekbrains/java1/lesson8/TicTacToeGui.java @@ -0,0 +1,146 @@ +package ru.geekbrains.java1.lesson8; + +import ru.geekbrains.java1.lesson4.TicTacToe; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +public class TicTacToeGui { + + private static final String FRAME_TITLE = "Крестики-Нолики"; + private static final String START_GAME_MSG = "Игра началачь!"; + + private final JLabel[][] gameField = new JLabel[TicTacToe.SIZE][TicTacToe.SIZE]; + + private JLabel gameStateLabel; + private JPanel gamePanel; + private JFrame frame; + + public TicTacToeGui() { + initFrame(); + frame.setVisible(true); + } + + private void initFrame() { + frame = new JFrame(); + frame.setTitle(FRAME_TITLE); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + frame.setLocationRelativeTo(null); + frame.setSize(400, 400); + + gameStateLabel = new JLabel(START_GAME_MSG); + gameStateLabel.setHorizontalAlignment(SwingConstants.CENTER); + frame.add(gameStateLabel, BorderLayout.NORTH); + + JButton restartGameButton = new JButton("Начать заново"); + restartGameButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + startGame(); + } + }); + + frame.add(restartGameButton, BorderLayout.SOUTH); + + gamePanel = new JPanel(); + gamePanel.setLayout(new GridLayout(TicTacToe.SIZE, TicTacToe.SIZE)); + initCells(gamePanel); + frame.add(gamePanel, BorderLayout.CENTER); + } + + private void initCells(JPanel gamePanel) { + Font font = new Font("Arial", Font.BOLD, 32); + for (int i = 0; i < TicTacToe.SIZE; i++) { + for (int j = 0; j < TicTacToe.SIZE; j++) { + JLabel cell = new JLabel(String.valueOf(TicTacToe.DOT_EMPTY)); + cell.setHorizontalAlignment(SwingConstants.CENTER); + cell.setVerticalAlignment(SwingConstants.CENTER); + cell.setFont(font); + cell.setBorder(BorderFactory.createLineBorder(Color.black)); + + final int rowIndex = i; + final int colIndex = j; + cell.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + if (!cell.isEnabled()) { + return; + } + + TicTacToe.setCell(rowIndex, colIndex, TicTacToe.DOT_X); + makeTurn(TicTacToe.DOT_X, cell); + + if (checkEndGameState(TicTacToe.DOT_X, "Игрок")) { + return; + } + + int[] computerCellIndex = TicTacToe.computerTurn(); + JLabel computerCell = gameField[computerCellIndex[0]][computerCellIndex[1]]; + makeTurn(TicTacToe.DOT_O, computerCell); + + checkEndGameState(TicTacToe.DOT_O, "Компьютер"); + } + }); + + gamePanel.add(cell); + gameField[i][j] = cell; + } + } + } + + private void makeTurn(char symbol, JLabel cell) { + cell.setText(String.valueOf(symbol)); + cell.setEnabled(false); + } + + private boolean checkEndGameState(char symbol, String playerName) { + if (TicTacToe.isWin(symbol)) { + setEndGameState(String.format("%s победил!", playerName)); + return true; + } + else if (TicTacToe.isMapFull()) { + setEndGameState("Ничья!"); + return true; + } + + return false; + } + + private void setEndGameState(String messageState) { + setEnabledGameField(false); + gameStateLabel.setText(messageState); + } + + private void startGame() { + setEnabledGameField(true); + gameStateLabel.setText(START_GAME_MSG); + TicTacToe.initMap(); + refreshGameField(); + } + + private void refreshGameField() { + for (int i = 0; i < TicTacToe.SIZE; i++) { + for (int j = 0; j < TicTacToe.SIZE; j++) { + gameField[i][j].setText(String.valueOf(TicTacToe.map[i][j])); + } + } + } + + private void setEnabledGameField(boolean enabled) { + gamePanel.setEnabled(true); + for (int i = 0; i < TicTacToe.SIZE; i++) { + for (int j = 0; j < TicTacToe.SIZE; j++) { + gameField[i][j].setEnabled(enabled); + } + } + } + + public static void main(String[] args) { + TicTacToeGui ticTacToeGui = new TicTacToeGui(); + ticTacToeGui.startGame(); + } +}