From ee592b95c405b97311167370a7d05e919bdd9346 Mon Sep 17 00:00:00 2001 From: Geratoptus Date: Mon, 26 Feb 2024 21:37:46 +0500 Subject: [PATCH 1/6] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=20=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D1=83=D1=80=D1=83=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B0=20=D1=81=20Eclipse?= =?UTF-8?q?=20=D0=BD=D0=B0=20IntelliJ=20Idea?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 42 +++++- .idea/.gitignore | 3 + .idea/encodings.xml | 7 + .idea/misc.xml | 14 ++ .idea/uiDesigner.xml | 124 ++++++++++++++++++ .idea/vcs.xml | 6 + pom.xml | 17 +++ robots/.classpath | 6 - robots/.gitignore | 2 - robots/.project | 17 --- .../main/java/org/robots/Main.java | 6 +- .../java/org/robots}/gui/GameVisualizer.java | 2 +- .../main/java/org/robots}/gui/GameWindow.java | 2 +- .../main/java/org/robots}/gui/LogWindow.java | 8 +- .../org/robots}/gui/MainApplicationFrame.java | 4 +- .../org/robots}/log/LogChangeListener.java | 2 +- .../main/java/org/robots}/log/LogEntry.java | 2 +- .../main/java/org/robots}/log/LogLevel.java | 2 +- .../java/org/robots}/log/LogWindowSource.java | 2 +- .../main/java/org/robots}/log/Logger.java | 2 +- 20 files changed, 226 insertions(+), 44 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 pom.xml delete mode 100644 robots/.classpath delete mode 100644 robots/.gitignore delete mode 100644 robots/.project rename robots/src/gui/RobotsProgram.java => src/main/java/org/robots/Main.java (90%) rename {robots/src => src/main/java/org/robots}/gui/GameVisualizer.java (99%) rename {robots/src => src/main/java/org/robots}/gui/GameWindow.java (95%) rename {robots/src => src/main/java/org/robots}/gui/LogWindow.java (90%) rename {robots/src => src/main/java/org/robots}/gui/MainApplicationFrame.java (98%) rename {robots/src => src/main/java/org/robots}/log/LogChangeListener.java (75%) rename {robots/src => src/main/java/org/robots}/log/LogEntry.java (94%) rename {robots/src => src/main/java/org/robots}/log/LogLevel.java (92%) rename {robots/src => src/main/java/org/robots}/log/LogWindowSource.java (99%) rename {robots/src => src/main/java/org/robots}/log/Logger.java (95%) diff --git a/.gitignore b/.gitignore index 2c6eb3893..5ff6309b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,38 @@ -/.metadata -/robots/.settings -/robots/bin -eclipse.bat \ No newline at end of file +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 000000000..26d33521a --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 000000000..aa00ffab7 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 000000000..ecfa09c0d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 000000000..2b63946d5 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 000000000..35eb1ddfb --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 000000000..007462f84 --- /dev/null +++ b/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + org.robots + Robots + 1.0-SNAPSHOT + + + 21 + 21 + UTF-8 + + + \ No newline at end of file diff --git a/robots/.classpath b/robots/.classpath deleted file mode 100644 index fceb4801b..000000000 --- a/robots/.classpath +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/robots/.gitignore b/robots/.gitignore deleted file mode 100644 index 2757ffa76..000000000 --- a/robots/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/bin/ -/.settings/ diff --git a/robots/.project b/robots/.project deleted file mode 100644 index 78e165663..000000000 --- a/robots/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - Robots - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/robots/src/gui/RobotsProgram.java b/src/main/java/org/robots/Main.java similarity index 90% rename from robots/src/gui/RobotsProgram.java rename to src/main/java/org/robots/Main.java index ae0930a8b..dc818fcc5 100644 --- a/robots/src/gui/RobotsProgram.java +++ b/src/main/java/org/robots/Main.java @@ -1,11 +1,13 @@ -package gui; +package org.robots; + +import org.robots.gui.MainApplicationFrame; import java.awt.Frame; import javax.swing.SwingUtilities; import javax.swing.UIManager; -public class RobotsProgram +public class Main { public static void main(String[] args) { try { diff --git a/robots/src/gui/GameVisualizer.java b/src/main/java/org/robots/gui/GameVisualizer.java similarity index 99% rename from robots/src/gui/GameVisualizer.java rename to src/main/java/org/robots/gui/GameVisualizer.java index f82cfd8f8..b4c00624d 100644 --- a/robots/src/gui/GameVisualizer.java +++ b/src/main/java/org/robots/gui/GameVisualizer.java @@ -1,4 +1,4 @@ -package gui; +package org.robots.gui; import java.awt.Color; import java.awt.EventQueue; diff --git a/robots/src/gui/GameWindow.java b/src/main/java/org/robots/gui/GameWindow.java similarity index 95% rename from robots/src/gui/GameWindow.java rename to src/main/java/org/robots/gui/GameWindow.java index ecb63c00f..dd09f0d96 100644 --- a/robots/src/gui/GameWindow.java +++ b/src/main/java/org/robots/gui/GameWindow.java @@ -1,4 +1,4 @@ -package gui; +package org.robots.gui; import java.awt.BorderLayout; diff --git a/robots/src/gui/LogWindow.java b/src/main/java/org/robots/gui/LogWindow.java similarity index 90% rename from robots/src/gui/LogWindow.java rename to src/main/java/org/robots/gui/LogWindow.java index 723d3e2fc..52464f5ff 100644 --- a/robots/src/gui/LogWindow.java +++ b/src/main/java/org/robots/gui/LogWindow.java @@ -1,4 +1,4 @@ -package gui; +package org.robots.gui; import java.awt.BorderLayout; import java.awt.EventQueue; @@ -7,9 +7,9 @@ import javax.swing.JInternalFrame; import javax.swing.JPanel; -import log.LogChangeListener; -import log.LogEntry; -import log.LogWindowSource; +import org.robots.log.LogChangeListener; +import org.robots.log.LogEntry; +import org.robots.log.LogWindowSource; public class LogWindow extends JInternalFrame implements LogChangeListener { diff --git a/robots/src/gui/MainApplicationFrame.java b/src/main/java/org/robots/gui/MainApplicationFrame.java similarity index 98% rename from robots/src/gui/MainApplicationFrame.java rename to src/main/java/org/robots/gui/MainApplicationFrame.java index 62e943ee1..1981221e3 100644 --- a/robots/src/gui/MainApplicationFrame.java +++ b/src/main/java/org/robots/gui/MainApplicationFrame.java @@ -1,4 +1,4 @@ -package gui; +package org.robots.gui; import java.awt.Dimension; import java.awt.Toolkit; @@ -14,7 +14,7 @@ import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; -import log.Logger; +import org.robots.log.Logger; /** * Что требуется сделать: diff --git a/robots/src/log/LogChangeListener.java b/src/main/java/org/robots/log/LogChangeListener.java similarity index 75% rename from robots/src/log/LogChangeListener.java rename to src/main/java/org/robots/log/LogChangeListener.java index 0b0fb85dd..950a70cd5 100644 --- a/robots/src/log/LogChangeListener.java +++ b/src/main/java/org/robots/log/LogChangeListener.java @@ -1,4 +1,4 @@ -package log; +package org.robots.log; public interface LogChangeListener { diff --git a/robots/src/log/LogEntry.java b/src/main/java/org/robots/log/LogEntry.java similarity index 94% rename from robots/src/log/LogEntry.java rename to src/main/java/org/robots/log/LogEntry.java index 3d9147107..ef966f298 100644 --- a/robots/src/log/LogEntry.java +++ b/src/main/java/org/robots/log/LogEntry.java @@ -1,4 +1,4 @@ -package log; +package org.robots.log; public class LogEntry { diff --git a/robots/src/log/LogLevel.java b/src/main/java/org/robots/log/LogLevel.java similarity index 92% rename from robots/src/log/LogLevel.java rename to src/main/java/org/robots/log/LogLevel.java index 582d010cc..70913ac9b 100644 --- a/robots/src/log/LogLevel.java +++ b/src/main/java/org/robots/log/LogLevel.java @@ -1,4 +1,4 @@ -package log; +package org.robots.log; public enum LogLevel { diff --git a/robots/src/log/LogWindowSource.java b/src/main/java/org/robots/log/LogWindowSource.java similarity index 99% rename from robots/src/log/LogWindowSource.java rename to src/main/java/org/robots/log/LogWindowSource.java index ca0ce4426..3f882ede4 100644 --- a/robots/src/log/LogWindowSource.java +++ b/src/main/java/org/robots/log/LogWindowSource.java @@ -1,4 +1,4 @@ -package log; +package org.robots.log; import java.util.ArrayList; import java.util.Collections; diff --git a/robots/src/log/Logger.java b/src/main/java/org/robots/log/Logger.java similarity index 95% rename from robots/src/log/Logger.java rename to src/main/java/org/robots/log/Logger.java index b008a5d01..4e05b59f5 100644 --- a/robots/src/log/Logger.java +++ b/src/main/java/org/robots/log/Logger.java @@ -1,4 +1,4 @@ -package log; +package org.robots.log; public final class Logger { From bc60ff7f6e29699df84e7450306ff3dd6d92e68b Mon Sep 17 00:00:00 2001 From: Geratoptus Date: Sun, 3 Mar 2024 15:58:46 +0500 Subject: [PATCH 2/6] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1) Унифицировал скобочную нотацию 2) Сменил нотацию полей с m_ на this 3) Убрал generateMenuBar, вынеся MenuBar в отдельный класс 4) Постарался разбить энтерами логические блоки кода --- src/main/java/org/robots/Main.java | 4 +- .../java/org/robots/gui/GameVisualizer.java | 148 +++++++++--------- src/main/java/org/robots/gui/GameWindow.java | 13 +- src/main/java/org/robots/gui/LogWindow.java | 33 ++-- .../org/robots/gui/MainApplicationFrame.java | 130 +++------------ src/main/java/org/robots/gui/MenuBar.java | 81 ++++++++++ src/main/java/org/robots/log/LogEntry.java | 18 +-- src/main/java/org/robots/log/LogLevel.java | 9 +- .../java/org/robots/log/LogWindowSource.java | 79 +++++----- src/main/java/org/robots/log/Logger.java | 6 +- 10 files changed, 254 insertions(+), 267 deletions(-) create mode 100644 src/main/java/org/robots/gui/MenuBar.java diff --git a/src/main/java/org/robots/Main.java b/src/main/java/org/robots/Main.java index dc818fcc5..764f694a2 100644 --- a/src/main/java/org/robots/Main.java +++ b/src/main/java/org/robots/Main.java @@ -15,9 +15,11 @@ public static void main(String[] args) { // UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); - } catch (Exception e) { + } + catch (Exception e) { e.printStackTrace(); } + SwingUtilities.invokeLater(() -> { MainApplicationFrame frame = new MainApplicationFrame(); frame.pack(); diff --git a/src/main/java/org/robots/gui/GameVisualizer.java b/src/main/java/org/robots/gui/GameVisualizer.java index b4c00624d..d3c2b69bb 100644 --- a/src/main/java/org/robots/gui/GameVisualizer.java +++ b/src/main/java/org/robots/gui/GameVisualizer.java @@ -13,29 +13,26 @@ import javax.swing.JPanel; -public class GameVisualizer extends JPanel -{ - private final Timer m_timer = initTimer(); +public class GameVisualizer extends JPanel { + private final Timer timer = initTimer(); - private static Timer initTimer() - { + private static Timer initTimer() { Timer timer = new Timer("events generator", true); return timer; } - private volatile double m_robotPositionX = 100; - private volatile double m_robotPositionY = 100; - private volatile double m_robotDirection = 0; + private volatile double robotPositionX = 100; + private volatile double robotPositionY = 100; + private volatile double robotDirection = 0; - private volatile int m_targetPositionX = 150; - private volatile int m_targetPositionY = 100; + private volatile int targetPositionX = 150; + private volatile int targetPositionY = 100; private static final double maxVelocity = 0.1; private static final double maxAngularVelocity = 0.001; - public GameVisualizer() - { - m_timer.schedule(new TimerTask() + public GameVisualizer() { + timer.schedule(new TimerTask() { @Override public void run() @@ -43,7 +40,7 @@ public void run() onRedrawEvent(); } }, 0, 50); - m_timer.schedule(new TimerTask() + timer.schedule(new TimerTask() { @Override public void run() @@ -51,6 +48,7 @@ public void run() onModelUpdateEvent(); } }, 0, 10); + addMouseListener(new MouseAdapter() { @Override @@ -60,13 +58,13 @@ public void mouseClicked(MouseEvent e) repaint(); } }); + setDoubleBuffered(true); } - protected void setTargetPosition(Point p) - { - m_targetPositionX = p.x; - m_targetPositionY = p.y; + protected void setTargetPosition(Point p) { + targetPositionX = p.x; + targetPositionY = p.y; } protected void onRedrawEvent() @@ -74,87 +72,86 @@ protected void onRedrawEvent() EventQueue.invokeLater(this::repaint); } - private static double distance(double x1, double y1, double x2, double y2) - { + private static double distance(double x1, double y1, double x2, double y2) { double diffX = x1 - x2; double diffY = y1 - y2; return Math.sqrt(diffX * diffX + diffY * diffY); } - private static double angleTo(double fromX, double fromY, double toX, double toY) - { + private static double angleTo(double fromX, double fromY, double toX, double toY) { double diffX = toX - fromX; double diffY = toY - fromY; return asNormalizedRadians(Math.atan2(diffY, diffX)); } - protected void onModelUpdateEvent() - { - double distance = distance(m_targetPositionX, m_targetPositionY, - m_robotPositionX, m_robotPositionY); - if (distance < 0.5) - { + protected void onModelUpdateEvent() { + double distance = distance(targetPositionX, targetPositionY, + robotPositionX, robotPositionY); + if (distance < 0.5) { return; } + double velocity = maxVelocity; - double angleToTarget = angleTo(m_robotPositionX, m_robotPositionY, m_targetPositionX, m_targetPositionY); + double angleToTarget = angleTo(robotPositionX, robotPositionY, targetPositionX, targetPositionY); double angularVelocity = 0; - if (angleToTarget > m_robotDirection) - { + + if (angleToTarget > robotDirection) { angularVelocity = maxAngularVelocity; } - if (angleToTarget < m_robotDirection) - { + + if (angleToTarget < robotDirection) { angularVelocity = -maxAngularVelocity; } moveRobot(velocity, angularVelocity, 10); } - private static double applyLimits(double value, double min, double max) - { + private static double applyLimits(double value, double min, double max) { if (value < min) return min; + if (value > max) return max; + return value; } - private void moveRobot(double velocity, double angularVelocity, double duration) - { + private void moveRobot(double velocity, double angularVelocity, double duration) { velocity = applyLimits(velocity, 0, maxVelocity); angularVelocity = applyLimits(angularVelocity, -maxAngularVelocity, maxAngularVelocity); - double newX = m_robotPositionX + velocity / angularVelocity * - (Math.sin(m_robotDirection + angularVelocity * duration) - - Math.sin(m_robotDirection)); - if (!Double.isFinite(newX)) - { - newX = m_robotPositionX + velocity * duration * Math.cos(m_robotDirection); + + double newX = robotPositionX + velocity / angularVelocity * + (Math.sin(robotDirection + angularVelocity * duration) - + Math.sin(robotDirection)); + + if (!Double.isFinite(newX)) { + newX = robotPositionX + velocity * duration * Math.cos(robotDirection); } - double newY = m_robotPositionY - velocity / angularVelocity * - (Math.cos(m_robotDirection + angularVelocity * duration) - - Math.cos(m_robotDirection)); - if (!Double.isFinite(newY)) - { - newY = m_robotPositionY + velocity * duration * Math.sin(m_robotDirection); + + double newY = robotPositionY - velocity / angularVelocity * + (Math.cos(robotDirection + angularVelocity * duration) - + Math.cos(robotDirection)); + + if (!Double.isFinite(newY)) { + newY = robotPositionY + velocity * duration * Math.sin(robotDirection); } - m_robotPositionX = newX; - m_robotPositionY = newY; - double newDirection = asNormalizedRadians(m_robotDirection + angularVelocity * duration); - m_robotDirection = newDirection; + + robotPositionX = newX; + robotPositionY = newY; + double newDirection = asNormalizedRadians(robotDirection + angularVelocity * duration); + robotDirection = newDirection; } - private static double asNormalizedRadians(double angle) - { - while (angle < 0) - { + private static double asNormalizedRadians(double angle) { + while (angle < 0) { angle += 2*Math.PI; } - while (angle >= 2*Math.PI) - { + + while (angle >= 2*Math.PI) { angle -= 2*Math.PI; } + return angle; } @@ -164,46 +161,49 @@ private static int round(double value) } @Override - public void paint(Graphics g) - { + public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D)g; - drawRobot(g2d, round(m_robotPositionX), round(m_robotPositionY), m_robotDirection); - drawTarget(g2d, m_targetPositionX, m_targetPositionY); + + drawRobot(g2d, round(robotPositionX), round(robotPositionY), robotDirection); + drawTarget(g2d, targetPositionX, targetPositionY); } - private static void fillOval(Graphics g, int centerX, int centerY, int diam1, int diam2) - { + private static void fillOval(Graphics g, int centerX, int centerY, int diam1, int diam2) { g.fillOval(centerX - diam1 / 2, centerY - diam2 / 2, diam1, diam2); } - private static void drawOval(Graphics g, int centerX, int centerY, int diam1, int diam2) - { + private static void drawOval(Graphics g, int centerX, int centerY, int diam1, int diam2) { g.drawOval(centerX - diam1 / 2, centerY - diam2 / 2, diam1, diam2); } - private void drawRobot(Graphics2D g, int x, int y, double direction) - { - int robotCenterX = round(m_robotPositionX); - int robotCenterY = round(m_robotPositionY); - AffineTransform t = AffineTransform.getRotateInstance(direction, robotCenterX, robotCenterY); + private void drawRobot(Graphics2D g, int x, int y, double direction) { + int robotCenterX = round(robotPositionX); + int robotCenterY = round(robotPositionY); + + AffineTransform t = AffineTransform.getRotateInstance(direction, robotCenterX, robotCenterY); + g.setTransform(t); g.setColor(Color.MAGENTA); fillOval(g, robotCenterX, robotCenterY, 30, 10); + g.setColor(Color.BLACK); drawOval(g, robotCenterX, robotCenterY, 30, 10); + g.setColor(Color.WHITE); fillOval(g, robotCenterX + 10, robotCenterY, 5, 5); + g.setColor(Color.BLACK); drawOval(g, robotCenterX + 10, robotCenterY, 5, 5); } - private void drawTarget(Graphics2D g, int x, int y) - { + private void drawTarget(Graphics2D g, int x, int y) { AffineTransform t = AffineTransform.getRotateInstance(0, 0, 0); + g.setTransform(t); g.setColor(Color.GREEN); fillOval(g, x, y, 5, 5); + g.setColor(Color.BLACK); drawOval(g, x, y, 5, 5); } diff --git a/src/main/java/org/robots/gui/GameWindow.java b/src/main/java/org/robots/gui/GameWindow.java index dd09f0d96..0277d3508 100644 --- a/src/main/java/org/robots/gui/GameWindow.java +++ b/src/main/java/org/robots/gui/GameWindow.java @@ -5,15 +5,14 @@ import javax.swing.JInternalFrame; import javax.swing.JPanel; -public class GameWindow extends JInternalFrame -{ - private final GameVisualizer m_visualizer; - public GameWindow() - { +public class GameWindow extends JInternalFrame { + private final GameVisualizer visualizer; + public GameWindow() { super("Игровое поле", true, true, true, true); - m_visualizer = new GameVisualizer(); + visualizer = new GameVisualizer(); JPanel panel = new JPanel(new BorderLayout()); - panel.add(m_visualizer, BorderLayout.CENTER); + + panel.add(visualizer, BorderLayout.CENTER); getContentPane().add(panel); pack(); } diff --git a/src/main/java/org/robots/gui/LogWindow.java b/src/main/java/org/robots/gui/LogWindow.java index 52464f5ff..c0061db96 100644 --- a/src/main/java/org/robots/gui/LogWindow.java +++ b/src/main/java/org/robots/gui/LogWindow.java @@ -11,35 +11,34 @@ import org.robots.log.LogEntry; import org.robots.log.LogWindowSource; -public class LogWindow extends JInternalFrame implements LogChangeListener -{ - private LogWindowSource m_logSource; - private TextArea m_logContent; +public class LogWindow extends JInternalFrame implements LogChangeListener { + private LogWindowSource logSource; + private TextArea logContent; - public LogWindow(LogWindowSource logSource) - { + public LogWindow(LogWindowSource logSource) { super("Протокол работы", true, true, true, true); - m_logSource = logSource; - m_logSource.registerListener(this); - m_logContent = new TextArea(""); - m_logContent.setSize(200, 500); + + this.logSource = logSource; + this.logSource.registerListener(this); + this.logContent = new TextArea(""); + this.logContent.setSize(200, 500); JPanel panel = new JPanel(new BorderLayout()); - panel.add(m_logContent, BorderLayout.CENTER); + panel.add(logContent, BorderLayout.CENTER); getContentPane().add(panel); pack(); updateLogContent(); } - private void updateLogContent() - { + private void updateLogContent() { StringBuilder content = new StringBuilder(); - for (LogEntry entry : m_logSource.all()) - { + + for (LogEntry entry : logSource.all()) { content.append(entry.getMessage()).append("\n"); } - m_logContent.setText(content.toString()); - m_logContent.invalidate(); + + logContent.setText(content.toString()); + logContent.invalidate(); } @Override diff --git a/src/main/java/org/robots/gui/MainApplicationFrame.java b/src/main/java/org/robots/gui/MainApplicationFrame.java index 1981221e3..6665bcd97 100644 --- a/src/main/java/org/robots/gui/MainApplicationFrame.java +++ b/src/main/java/org/robots/gui/MainApplicationFrame.java @@ -22,8 +22,7 @@ * Следует разделить его на серию более простых методов (или вообще выделить отдельный класс). * */ -public class MainApplicationFrame extends JFrame -{ +public class MainApplicationFrame extends JFrame { private final JDesktopPane desktopPane = new JDesktopPane(); public MainApplicationFrame() { @@ -31,126 +30,45 @@ public MainApplicationFrame() { //of the screen. int inset = 50; Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + setBounds(inset, inset, - screenSize.width - inset*2, - screenSize.height - inset*2); + screenSize.width - inset * 2, + screenSize.height - inset * 2); setContentPane(desktopPane); - - - LogWindow logWindow = createLogWindow(); - addWindow(logWindow); - GameWindow gameWindow = new GameWindow(); - gameWindow.setSize(400, 400); - addWindow(gameWindow); + addWindow(createLogWindow()); + addWindow(createGameWindow()); - setJMenuBar(generateMenuBar()); + setJMenuBar(new MenuBar(this)); setDefaultCloseOperation(EXIT_ON_CLOSE); } - - protected LogWindow createLogWindow() - { + + protected GameWindow createGameWindow(){ + GameWindow gameWindow = new GameWindow(); + gameWindow.setSize(400, 400); + + return gameWindow; + } + + protected LogWindow createLogWindow() { + LogWindow logWindow = new LogWindow(Logger.getDefaultLogSource()); + logWindow.setLocation(10,10); logWindow.setSize(300, 800); + setMinimumSize(logWindow.getSize()); logWindow.pack(); Logger.debug("Протокол работает"); + return logWindow; } - - protected void addWindow(JInternalFrame frame) - { + + protected void addWindow(JInternalFrame frame) { + desktopPane.add(frame); frame.setVisible(true); } - -// protected JMenuBar createMenuBar() { -// JMenuBar menuBar = new JMenuBar(); -// -// //Set up the lone menu. -// JMenu menu = new JMenu("Document"); -// menu.setMnemonic(KeyEvent.VK_D); -// menuBar.add(menu); -// -// //Set up the first menu item. -// JMenuItem menuItem = new JMenuItem("New"); -// menuItem.setMnemonic(KeyEvent.VK_N); -// menuItem.setAccelerator(KeyStroke.getKeyStroke( -// KeyEvent.VK_N, ActionEvent.ALT_MASK)); -// menuItem.setActionCommand("new"); -//// menuItem.addActionListener(this); -// menu.add(menuItem); -// -// //Set up the second menu item. -// menuItem = new JMenuItem("Quit"); -// menuItem.setMnemonic(KeyEvent.VK_Q); -// menuItem.setAccelerator(KeyStroke.getKeyStroke( -// KeyEvent.VK_Q, ActionEvent.ALT_MASK)); -// menuItem.setActionCommand("quit"); -//// menuItem.addActionListener(this); -// menu.add(menuItem); -// -// return menuBar; -// } - - private JMenuBar generateMenuBar() - { - JMenuBar menuBar = new JMenuBar(); - - JMenu lookAndFeelMenu = new JMenu("Режим отображения"); - lookAndFeelMenu.setMnemonic(KeyEvent.VK_V); - lookAndFeelMenu.getAccessibleContext().setAccessibleDescription( - "Управление режимом отображения приложения"); - - { - JMenuItem systemLookAndFeel = new JMenuItem("Системная схема", KeyEvent.VK_S); - systemLookAndFeel.addActionListener((event) -> { - setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); - this.invalidate(); - }); - lookAndFeelMenu.add(systemLookAndFeel); - } - - { - JMenuItem crossplatformLookAndFeel = new JMenuItem("Универсальная схема", KeyEvent.VK_S); - crossplatformLookAndFeel.addActionListener((event) -> { - setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); - this.invalidate(); - }); - lookAndFeelMenu.add(crossplatformLookAndFeel); - } - - JMenu testMenu = new JMenu("Тесты"); - testMenu.setMnemonic(KeyEvent.VK_T); - testMenu.getAccessibleContext().setAccessibleDescription( - "Тестовые команды"); - - { - JMenuItem addLogMessageItem = new JMenuItem("Сообщение в лог", KeyEvent.VK_S); - addLogMessageItem.addActionListener((event) -> { - Logger.debug("Новая строка"); - }); - testMenu.add(addLogMessageItem); - } - - menuBar.add(lookAndFeelMenu); - menuBar.add(testMenu); - return menuBar; - } - - private void setLookAndFeel(String className) - { - try - { - UIManager.setLookAndFeel(className); - SwingUtilities.updateComponentTreeUI(this); - } - catch (ClassNotFoundException | InstantiationException - | IllegalAccessException | UnsupportedLookAndFeelException e) - { - // just ignore - } - } + } diff --git a/src/main/java/org/robots/gui/MenuBar.java b/src/main/java/org/robots/gui/MenuBar.java new file mode 100644 index 000000000..b36303c2f --- /dev/null +++ b/src/main/java/org/robots/gui/MenuBar.java @@ -0,0 +1,81 @@ +package org.robots.gui; + +import javax.swing.JFrame; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; +import javax.swing.UnsupportedLookAndFeelException; + +import org.robots.log.Logger; + +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; + +public class MenuBar extends JMenuBar{ + private final JFrame parent; + + public MenuBar(JFrame parent) { + this.parent = parent; + this.add(initTestMenu()); + this.add(initLookAndFeelMenu()); + } + + private JMenu createJMenu(String label, int mnemonic, String accessDescription){ + JMenu menu = new JMenu(label); + menu.setMnemonic(mnemonic); + menu.getAccessibleContext().setAccessibleDescription(accessDescription); + + return menu; + } + + private JMenuItem createJMenuItem(String label, int mnemonic, ActionListener listener){ + JMenuItem menuItem = new JMenuItem(label, mnemonic); + menuItem.addActionListener(listener); + + return menuItem; + } + + private void setLookAndFeel(String className){ + try{ + UIManager.setLookAndFeel(className); + SwingUtilities.updateComponentTreeUI(parent); + } + + catch (ClassNotFoundException | InstantiationException + | IllegalAccessException | UnsupportedLookAndFeelException exception) { + //ignore + } + + finally{ + parent.invalidate(); + } + + } + + private JMenu initLookAndFeelMenu(){ + JMenu menu = createJMenu("Режим отображения", KeyEvent.VK_V, + "Управление режимом отображения приложения"); + + menu.add(createJMenuItem("Системная схема", KeyEvent.VK_S, + event -> setLookAndFeel(UIManager.getSystemLookAndFeelClassName()))); + + menu.add(createJMenuItem("Универсальная схема", KeyEvent.VK_S, + event -> setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()))); + + return menu; + } + private JMenu initTestMenu(){ + JMenu menu = createJMenu("Тесты", KeyEvent.VK_T, + "Тестовые команды"); + + menu.add(createJMenuItem("Сообщение в лог", KeyEvent.VK_S, + event -> Logger.debug("Новая строка"))); + + return menu; + } + + + +} diff --git a/src/main/java/org/robots/log/LogEntry.java b/src/main/java/org/robots/log/LogEntry.java index ef966f298..f5cfbb8ab 100644 --- a/src/main/java/org/robots/log/LogEntry.java +++ b/src/main/java/org/robots/log/LogEntry.java @@ -1,24 +1,22 @@ package org.robots.log; -public class LogEntry -{ - private LogLevel m_logLevel; - private String m_strMessage; +public class LogEntry { + private LogLevel logLevel; + private String strMessage; - public LogEntry(LogLevel logLevel, String strMessage) - { - m_strMessage = strMessage; - m_logLevel = logLevel; + public LogEntry(LogLevel logLevel, String strMessage) { + this.strMessage = strMessage; + this.logLevel = logLevel; } public String getMessage() { - return m_strMessage; + return strMessage; } public LogLevel getLevel() { - return m_logLevel; + return logLevel; } } diff --git a/src/main/java/org/robots/log/LogLevel.java b/src/main/java/org/robots/log/LogLevel.java index 70913ac9b..1f175f594 100644 --- a/src/main/java/org/robots/log/LogLevel.java +++ b/src/main/java/org/robots/log/LogLevel.java @@ -1,7 +1,6 @@ package org.robots.log; -public enum LogLevel -{ +public enum LogLevel { Trace(0), Debug(1), Info(2), @@ -9,16 +8,16 @@ public enum LogLevel Error(4), Fatal(5); - private int m_iLevel; + private int iLevel; private LogLevel(int iLevel) { - m_iLevel = iLevel; + this.iLevel = iLevel; } public int level() { - return m_iLevel; + return iLevel; } } diff --git a/src/main/java/org/robots/log/LogWindowSource.java b/src/main/java/org/robots/log/LogWindowSource.java index 3f882ede4..b08da4f11 100644 --- a/src/main/java/org/robots/log/LogWindowSource.java +++ b/src/main/java/org/robots/log/LogWindowSource.java @@ -12,78 +12,71 @@ * величиной m_iQueueLength (т.е. реально нужна очередь сообщений * ограниченного размера) */ -public class LogWindowSource -{ - private int m_iQueueLength; +public class LogWindowSource { + private int iQueueLength; - private ArrayList m_messages; - private final ArrayList m_listeners; - private volatile LogChangeListener[] m_activeListeners; + private ArrayList messages; + private final ArrayList listeners; + private volatile LogChangeListener[] activeListeners; - public LogWindowSource(int iQueueLength) - { - m_iQueueLength = iQueueLength; - m_messages = new ArrayList(iQueueLength); - m_listeners = new ArrayList(); + public LogWindowSource(int iQueueLength){ + this.iQueueLength = iQueueLength; + messages = new ArrayList(iQueueLength); + listeners = new ArrayList(); } - public void registerListener(LogChangeListener listener) - { - synchronized(m_listeners) - { - m_listeners.add(listener); - m_activeListeners = null; + public void registerListener(LogChangeListener listener) { + + synchronized(listeners) { + listeners.add(listener); + activeListeners = null; } } - public void unregisterListener(LogChangeListener listener) - { - synchronized(m_listeners) - { - m_listeners.remove(listener); - m_activeListeners = null; + public void unregisterListener(LogChangeListener listener) { + + synchronized(listeners) { + listeners.remove(listener); + activeListeners = null; } } - public void append(LogLevel logLevel, String strMessage) - { + public void append(LogLevel logLevel, String strMessage) { LogEntry entry = new LogEntry(logLevel, strMessage); - m_messages.add(entry); - LogChangeListener [] activeListeners = m_activeListeners; - if (activeListeners == null) - { - synchronized (m_listeners) - { - if (m_activeListeners == null) + messages.add(entry); + LogChangeListener [] activeListeners = this.activeListeners; + if (activeListeners == null) { + + synchronized (listeners) { + + if (this.activeListeners == null) { - activeListeners = m_listeners.toArray(new LogChangeListener [0]); - m_activeListeners = activeListeners; + activeListeners = listeners.toArray(new LogChangeListener [0]); + this.activeListeners = activeListeners; } } } - for (LogChangeListener listener : activeListeners) - { + for (LogChangeListener listener : activeListeners) { listener.onLogChanged(); } } public int size() { - return m_messages.size(); + return messages.size(); } - public Iterable range(int startFrom, int count) - { - if (startFrom < 0 || startFrom >= m_messages.size()) + public Iterable range(int startFrom, int count) { + if (startFrom < 0 || startFrom >= messages.size()) { return Collections.emptyList(); } - int indexTo = Math.min(startFrom + count, m_messages.size()); - return m_messages.subList(startFrom, indexTo); + int indexTo = Math.min(startFrom + count, messages.size()); + return messages.subList(startFrom, indexTo); } public Iterable all() { - return m_messages; + return messages; } } diff --git a/src/main/java/org/robots/log/Logger.java b/src/main/java/org/robots/log/Logger.java index 4e05b59f5..5294bf8d6 100644 --- a/src/main/java/org/robots/log/Logger.java +++ b/src/main/java/org/robots/log/Logger.java @@ -1,14 +1,12 @@ package org.robots.log; -public final class Logger -{ +public final class Logger { private static final LogWindowSource defaultLogSource; static { defaultLogSource = new LogWindowSource(100); } - private Logger() - { + private Logger() { } public static void debug(String strMessage) From f762c775b731c74d3b2b4628f1f7421341b20dbc Mon Sep 17 00:00:00 2001 From: Geratoptus Date: Sun, 3 Mar 2024 17:03:37 +0500 Subject: [PATCH 3/6] =?UTF-8?q?=D0=97=D0=B0=D0=BA=D1=80=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=81=20=D0=BF=D0=BE=D0=B4=D1=82=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D0=B6=D0=B4=D0=B5=D0=BD=D0=B8=D0=B5=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1) Добавил метод confirmWindowClose для выхода с подтверждением 2) Распространил метод confirmWindowClose на закрытие по "X" и по кнопке в меню. 3) Локализовал yesButtonText и noButtontext --- src/main/java/org/robots/Main.java | 5 +-- .../org/robots/gui/MainApplicationFrame.java | 33 ++++++++++++------- src/main/java/org/robots/gui/MenuBar.java | 21 ++++++++++-- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/robots/Main.java b/src/main/java/org/robots/Main.java index 764f694a2..e6d5595ce 100644 --- a/src/main/java/org/robots/Main.java +++ b/src/main/java/org/robots/Main.java @@ -4,14 +4,15 @@ import java.awt.Frame; -import javax.swing.SwingUtilities; -import javax.swing.UIManager; +import javax.swing.*; public class Main { public static void main(String[] args) { try { UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); + UIManager.put("OptionPane.yesButtonText", "Да" ); + UIManager.put("OptionPane.noButtonText", "Нет" ); // UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); diff --git a/src/main/java/org/robots/gui/MainApplicationFrame.java b/src/main/java/org/robots/gui/MainApplicationFrame.java index 6665bcd97..19a05d512 100644 --- a/src/main/java/org/robots/gui/MainApplicationFrame.java +++ b/src/main/java/org/robots/gui/MainApplicationFrame.java @@ -2,17 +2,10 @@ import java.awt.Dimension; import java.awt.Toolkit; -import java.awt.event.KeyEvent; - -import javax.swing.JDesktopPane; -import javax.swing.JFrame; -import javax.swing.JInternalFrame; -import javax.swing.JMenu; -import javax.swing.JMenuBar; -import javax.swing.JMenuItem; -import javax.swing.SwingUtilities; -import javax.swing.UIManager; -import javax.swing.UnsupportedLookAndFeelException; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +import javax.swing.*; import org.robots.log.Logger; @@ -41,11 +34,27 @@ public MainApplicationFrame() { addWindow(createGameWindow()); setJMenuBar(new MenuBar(this)); - setDefaultCloseOperation(EXIT_ON_CLOSE); + + addWindowListener(new WindowAdapter(){ + @Override + public void windowClosing(WindowEvent event){ + MainApplicationFrame.this.confirmWindowClose(); + } + }); + + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); + } + + public void confirmWindowClose(){ + if (JOptionPane.showConfirmDialog(this, "Вы уверены, что хотите закрыть приложение?", + "Закрыть?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){ + System.exit(0); + } } protected GameWindow createGameWindow(){ GameWindow gameWindow = new GameWindow(); + gameWindow.setLocation(300, 10); gameWindow.setSize(400, 400); return gameWindow; diff --git a/src/main/java/org/robots/gui/MenuBar.java b/src/main/java/org/robots/gui/MenuBar.java index b36303c2f..07fc4e9db 100644 --- a/src/main/java/org/robots/gui/MenuBar.java +++ b/src/main/java/org/robots/gui/MenuBar.java @@ -1,6 +1,5 @@ package org.robots.gui; -import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; @@ -12,14 +11,17 @@ import java.awt.event.ActionListener; import java.awt.event.KeyEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; public class MenuBar extends JMenuBar{ - private final JFrame parent; + private final MainApplicationFrame parent; - public MenuBar(JFrame parent) { + public MenuBar(MainApplicationFrame parent) { this.parent = parent; this.add(initTestMenu()); this.add(initLookAndFeelMenu()); + this.add(initExitMenu()); } private JMenu createJMenu(String label, int mnemonic, String accessDescription){ @@ -30,6 +32,19 @@ private JMenu createJMenu(String label, int mnemonic, String accessDescription){ return menu; } + private JMenu initExitMenu(){ + JMenu menu = createJMenu("Выход", KeyEvent.VK_E, + "Выход из приложения"); + + menu.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent event) { + parent.confirmWindowClose(); + } + }); + + return menu; + } private JMenuItem createJMenuItem(String label, int mnemonic, ActionListener listener){ JMenuItem menuItem = new JMenuItem(label, mnemonic); menuItem.addActionListener(listener); From dc9a06c19d8cfbd96a327f338180a7b52b00ef84 Mon Sep 17 00:00:00 2001 From: Geratoptus Date: Mon, 4 Mar 2024 21:33:13 +0500 Subject: [PATCH 4/6] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C=D1=88?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1) Сделал положение и размер окон параметрами 2) Сделал окна полями 3) Заменил System.exit(0) на dispose() --- .../org/robots/gui/MainApplicationFrame.java | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/robots/gui/MainApplicationFrame.java b/src/main/java/org/robots/gui/MainApplicationFrame.java index 19a05d512..c0e27a8fb 100644 --- a/src/main/java/org/robots/gui/MainApplicationFrame.java +++ b/src/main/java/org/robots/gui/MainApplicationFrame.java @@ -17,7 +17,11 @@ */ public class MainApplicationFrame extends JFrame { private final JDesktopPane desktopPane = new JDesktopPane(); - + + private LogWindow logWindow; + private GameWindow gameWindow; + + public MainApplicationFrame() { //Make the big window be indented 50 pixels from each edge //of the screen. @@ -30,8 +34,10 @@ public MainApplicationFrame() { setContentPane(desktopPane); - addWindow(createLogWindow()); - addWindow(createGameWindow()); + logWindow = createLogWindow(10, 10, 300, 800); + gameWindow = createGameWindow(300, 10, 400 ,400); + addWindow(logWindow); + addWindow(gameWindow); setJMenuBar(new MenuBar(this)); @@ -48,24 +54,25 @@ public void windowClosing(WindowEvent event){ public void confirmWindowClose(){ if (JOptionPane.showConfirmDialog(this, "Вы уверены, что хотите закрыть приложение?", "Закрыть?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){ - System.exit(0); + this.setVisible(false); + this.dispose(); } } - protected GameWindow createGameWindow(){ + protected GameWindow createGameWindow(int xLocation, int yLocation, int width, int height){ GameWindow gameWindow = new GameWindow(); - gameWindow.setLocation(300, 10); - gameWindow.setSize(400, 400); + gameWindow.setLocation(xLocation, yLocation); + gameWindow.setSize(width, height); return gameWindow; } - protected LogWindow createLogWindow() { + protected LogWindow createLogWindow(int xLocation, int yLocation, int width, int height) { LogWindow logWindow = new LogWindow(Logger.getDefaultLogSource()); - logWindow.setLocation(10,10); - logWindow.setSize(300, 800); + logWindow.setLocation(xLocation,yLocation); + logWindow.setSize(width, height); setMinimumSize(logWindow.getSize()); logWindow.pack(); @@ -80,4 +87,12 @@ protected void addWindow(JInternalFrame frame) { frame.setVisible(true); } + public GameWindow getGameWindow(){ + return gameWindow; + } + + public LogWindow getLogWindow(){ + return logWindow; + } + } From a959e9d1fdd7a513cee3516450bcb85a13338dc2 Mon Sep 17 00:00:00 2001 From: Geratoptus Date: Tue, 5 Mar 2024 11:35:28 +0500 Subject: [PATCH 5/6] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=D0=B4=D0=BE=D0=B2=D0=B0=D1=82?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20dispose=20=D0=B2=D1=81?= =?UTF-8?q?=D0=B5=D1=85=20=D0=BE=D0=BA=D0=BE=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 +---- src/main/java/org/robots/gui/MainApplicationFrame.java | 4 +++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 5ff6309b7..d76946210 100644 --- a/.gitignore +++ b/.gitignore @@ -4,10 +4,7 @@ target/ !**/src/test/**/target/ ### IntelliJ IDEA ### -.idea/modules.xml -.idea/jarRepositories.xml -.idea/compiler.xml -.idea/libraries/ +.idea *.iws *.iml *.ipr diff --git a/src/main/java/org/robots/gui/MainApplicationFrame.java b/src/main/java/org/robots/gui/MainApplicationFrame.java index c0e27a8fb..8febfd87f 100644 --- a/src/main/java/org/robots/gui/MainApplicationFrame.java +++ b/src/main/java/org/robots/gui/MainApplicationFrame.java @@ -54,7 +54,9 @@ public void windowClosing(WindowEvent event){ public void confirmWindowClose(){ if (JOptionPane.showConfirmDialog(this, "Вы уверены, что хотите закрыть приложение?", "Закрыть?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){ - this.setVisible(false); + + logWindow.dispose(); + gameWindow.dispose(); this.dispose(); } } From 6c69edf853578f2398230695916ea4da06a3bc5e Mon Sep 17 00:00:00 2001 From: Geratoptus Date: Tue, 5 Mar 2024 11:48:57 +0500 Subject: [PATCH 6/6] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C=D1=88?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1) Вместо параметров в createGameWindow и createLogWindow сделал декоратор над addWindow с параметрами. 2) Добавил ускоритель на кнопки --- .../org/robots/gui/MainApplicationFrame.java | 22 ++++++++++-------- src/main/java/org/robots/gui/MenuBar.java | 23 +++++++------------ 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/robots/gui/MainApplicationFrame.java b/src/main/java/org/robots/gui/MainApplicationFrame.java index 8febfd87f..a90a447b3 100644 --- a/src/main/java/org/robots/gui/MainApplicationFrame.java +++ b/src/main/java/org/robots/gui/MainApplicationFrame.java @@ -34,10 +34,10 @@ public MainApplicationFrame() { setContentPane(desktopPane); - logWindow = createLogWindow(10, 10, 300, 800); - gameWindow = createGameWindow(300, 10, 400 ,400); - addWindow(logWindow); - addWindow(gameWindow); + logWindow = createLogWindow(); + gameWindow = createGameWindow(); + addWindow(logWindow, 10, 10, 300, 800); + addWindow(gameWindow, 300, 10, 400, 400); setJMenuBar(new MenuBar(this)); @@ -61,20 +61,16 @@ public void confirmWindowClose(){ } } - protected GameWindow createGameWindow(int xLocation, int yLocation, int width, int height){ + protected GameWindow createGameWindow(){ GameWindow gameWindow = new GameWindow(); - gameWindow.setLocation(xLocation, yLocation); - gameWindow.setSize(width, height); return gameWindow; } - protected LogWindow createLogWindow(int xLocation, int yLocation, int width, int height) { + protected LogWindow createLogWindow() { LogWindow logWindow = new LogWindow(Logger.getDefaultLogSource()); - logWindow.setLocation(xLocation,yLocation); - logWindow.setSize(width, height); setMinimumSize(logWindow.getSize()); logWindow.pack(); @@ -89,6 +85,12 @@ protected void addWindow(JInternalFrame frame) { frame.setVisible(true); } + protected void addWindow(JInternalFrame frame, int xLocation, int yLocation, int width, int height){ + frame.setSize(width, height); + frame.setLocation(xLocation, yLocation); + addWindow(frame); + } + public GameWindow getGameWindow(){ return gameWindow; } diff --git a/src/main/java/org/robots/gui/MenuBar.java b/src/main/java/org/robots/gui/MenuBar.java index 07fc4e9db..63462b4ef 100644 --- a/src/main/java/org/robots/gui/MenuBar.java +++ b/src/main/java/org/robots/gui/MenuBar.java @@ -1,18 +1,10 @@ package org.robots.gui; -import javax.swing.JMenu; -import javax.swing.JMenuBar; -import javax.swing.JMenuItem; -import javax.swing.SwingUtilities; -import javax.swing.UIManager; -import javax.swing.UnsupportedLookAndFeelException; +import javax.swing.*; import org.robots.log.Logger; -import java.awt.event.ActionListener; -import java.awt.event.KeyEvent; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; +import java.awt.event.*; public class MenuBar extends JMenuBar{ private final MainApplicationFrame parent; @@ -45,8 +37,9 @@ public void mouseClicked(MouseEvent event) { return menu; } - private JMenuItem createJMenuItem(String label, int mnemonic, ActionListener listener){ - JMenuItem menuItem = new JMenuItem(label, mnemonic); + private JMenuItem createJMenuItem(String label, KeyStroke key, ActionListener listener){ + JMenuItem menuItem = new JMenuItem(label); + menuItem.setAccelerator(key); menuItem.addActionListener(listener); return menuItem; @@ -73,10 +66,10 @@ private JMenu initLookAndFeelMenu(){ JMenu menu = createJMenu("Режим отображения", KeyEvent.VK_V, "Управление режимом отображения приложения"); - menu.add(createJMenuItem("Системная схема", KeyEvent.VK_S, + menu.add(createJMenuItem("Системная схема", KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.ALT_MASK), event -> setLookAndFeel(UIManager.getSystemLookAndFeelClassName()))); - menu.add(createJMenuItem("Универсальная схема", KeyEvent.VK_S, + menu.add(createJMenuItem("Универсальная схема", KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.ALT_MASK), event -> setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()))); return menu; @@ -85,7 +78,7 @@ private JMenu initTestMenu(){ JMenu menu = createJMenu("Тесты", KeyEvent.VK_T, "Тестовые команды"); - menu.add(createJMenuItem("Сообщение в лог", KeyEvent.VK_S, + menu.add(createJMenuItem("Сообщение в лог", KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.ALT_MASK), event -> Logger.debug("Новая строка"))); return menu;