Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main tasks #48

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/Robots.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions robots/src/gui/CoordinatesDrawer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package gui;

import log.Logger;

import javax.swing.*;
import java.awt.*;
import java.util.Timer;
import java.util.TimerTask;

public class CoordinatesDrawer extends JPanel {

private final java.util.Timer m_timer = initTimer();
public Parameters parameters;

private static java.util.Timer initTimer() {
java.util.Timer timer = new Timer("events generator", true);
return timer;
}

public CoordinatesDrawer(Parameters parameters) {
this.parameters = parameters;
m_timer.schedule(new TimerTask() {
@Override
public void run() {
onRedrawEvent();
}
}, 0, 50);
}


protected void onRedrawEvent() {
EventQueue.invokeLater(this::repaint);
}

@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawString("X: " + String.valueOf(parameters.m_robotPositionX), 25, 25);
g2d.drawString("Y: " + String.valueOf(parameters.m_robotPositionY), 25, 45);
g2d.drawString("DIRECTION: " + String.valueOf(parameters.m_robotDirection), 25, 65);
g2d.drawString("Target X: " + String.valueOf(parameters.m_targetPositionX), 25, 85);
g2d.drawString("Target Y: " + String.valueOf(parameters.m_targetPositionY), 25, 105);
setDoubleBuffered(true);
}


}
32 changes: 32 additions & 0 deletions robots/src/gui/CoordinatesWindow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gui;


import javax.swing.*;
import java.awt.*;
import java.util.Observable;
import java.util.Observer;

public class CoordinatesWindow extends JInternalFrame implements Observer {

private CoordinatesDrawer drawer;

public CoordinatesWindow(Parameters parameters) {
super("Текущие координаты робота", true, true, true, true);


drawer = new CoordinatesDrawer(parameters);

JPanel panel = new JPanel(new BorderLayout());
panel.add(drawer, BorderLayout.CENTER);
getContentPane().add(panel);
pack();

}

@Override
public void update(Observable o, Object arg) {
var t = (Parameters) o;
drawer.parameters = t;

}
}
97 changes: 97 additions & 0 deletions robots/src/gui/GameDrawer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.util.Observable;
import java.util.Observer;
import java.util.Timer;
import java.util.TimerTask;


public class GameDrawer extends JPanel implements Observer {
private final Timer m_timer = initTimer();
private Parameters parameters;

private static Timer initTimer() {
Timer timer = new Timer("events generator", true);
return timer;
}

public GameDrawer(Parameters parameters) {
this.parameters = parameters;
m_timer.schedule(new TimerTask() {
@Override
public void run() {
onRedrawEvent();
}
}, 0, 50);

addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
parameters.updateTarget(e.getPoint());
repaint();
}
});
setDoubleBuffered(true);

}

protected void onRedrawEvent() {
EventQueue.invokeLater(this::repaint);
}

@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
drawRobot(g2d, round(parameters.m_robotPositionX), round(parameters.m_robotPositionY), parameters.m_robotDirection);
drawTarget(g2d, parameters.m_targetPositionX, parameters.m_targetPositionY);
setDoubleBuffered(true);
}

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) {
g.drawOval(centerX - diam1 / 2, centerY - diam2 / 2, diam1, diam2);
}

private void drawRobot(Graphics2D g, int x, int y, double direction) {
int robotCenterX = round(parameters.m_robotPositionX);
int robotCenterY = round(parameters.m_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) {
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);
}

private static int round(double value) {
return (int) (value + 0.5);
}

@Override
public void update(Observable o, Object arg) {
parameters = (Parameters) o;
repaint();
}
}
Loading