From 1ef901fe9d4cd18787b6813d66baf7bb27e6eeb6 Mon Sep 17 00:00:00 2001 From: "demon17ya@gmail.com" Date: Sat, 21 Sep 2019 01:03:33 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=94=D0=BE=D0=BC=D0=B0=D1=88=D0=BD=D1=8F?= =?UTF-8?q?=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20#1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BackGround.java | 27 ++++++++++++++ Ball.java | 47 ++++++++++++++++++++++++ GameCanvas.java | 44 +++++++++++++++++++++++ MainCircles.java | 86 ++++++++++++++++++++++++++++++++++++++++++++ MouseController.java | 30 ++++++++++++++++ Sprite.java | 45 +++++++++++++++++++++++ 6 files changed, 279 insertions(+) create mode 100644 BackGround.java create mode 100644 Ball.java create mode 100644 GameCanvas.java create mode 100644 MainCircles.java create mode 100644 MouseController.java create mode 100644 Sprite.java diff --git a/BackGround.java b/BackGround.java new file mode 100644 index 0000000..a4714b1 --- /dev/null +++ b/BackGround.java @@ -0,0 +1,27 @@ +package ru.gb.jtwo.online.circles; + +import java.awt.*; + +public class BackGround { + + private int changeSpeed; + private int counter; + + BackGround(int updateTime){ + this.changeSpeed = updateTime; + } + + void update(GameCanvas canvas){ + counter++; + + if ((counter % changeSpeed) == 0) { + Color color = new Color( + (int)(Math.random() * 255), + (int)(Math.random() * 255), + (int)(Math.random() * 255) + ); + canvas.setBackground(color); + } + + } +} diff --git a/Ball.java b/Ball.java new file mode 100644 index 0000000..1f63620 --- /dev/null +++ b/Ball.java @@ -0,0 +1,47 @@ +package ru.gb.jtwo.online.circles; + +import java.awt.*; + +public class Ball extends Sprite { + private float vx = 150 + (float)(Math.random() * 200f); + private float vy = 150 + (float)(Math.random() * 200f); + private final Color color = new Color( + (int)(Math.random() * 255), + (int)(Math.random() * 255), + (int)(Math.random() * 255) + ); + + Ball() { + halfHeight = 20 + (float)(Math.random() * 50f); + halfWidth = halfHeight; + } + + @Override + void update(GameCanvas canvas, float deltaTime) { + x += vx * deltaTime; + y += vy * deltaTime; + if (getLeft() < canvas.getLeft()) { + setLeft(canvas.getLeft()); + vx = -vx; + } + if (getRight() > canvas.getRight()) { + setRight(canvas.getRight()); + vx = -vx; + } + if (getTop() < canvas.getTop()) { + setTop(canvas.getTop()); + vy = -vy; + } + if (getBottom() > canvas.getBottom()) { + setBottom(canvas.getBottom()); + vy = -vy; + } + } + + @Override + void render(GameCanvas canvas, Graphics g) { + g.setColor(color); + g.fillOval((int) getLeft(), (int) getTop(), + (int) getWidth(), (int) getHeight()); + } +} diff --git a/GameCanvas.java b/GameCanvas.java new file mode 100644 index 0000000..f93cd77 --- /dev/null +++ b/GameCanvas.java @@ -0,0 +1,44 @@ +package ru.gb.jtwo.online.circles; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +public class GameCanvas extends JPanel { + + private MainCircles gameWindow; + private long lastFrameTime; + + + + GameCanvas(MainCircles gameWindow) { + this.gameWindow = gameWindow; + addMouseListener(new MouseController(this)); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + + long currentTime = System.nanoTime(); + float delta = (currentTime - lastFrameTime) * 0.000000001f; + lastFrameTime = currentTime; + + try { + Thread.sleep(17); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + gameWindow.onDrawPanel(this, g, delta); + repaint(); + } + + public int getLeft() { return 0; } + public int getRight() { return getWidth() - 1; } + public int getTop() { return 0; } + public int getBottom() { return getHeight() - 1; } + public MainCircles getGameWindow(){return gameWindow;}; + +} diff --git a/MainCircles.java b/MainCircles.java new file mode 100644 index 0000000..050c3f5 --- /dev/null +++ b/MainCircles.java @@ -0,0 +1,86 @@ +package ru.gb.jtwo.online.circles; + +import javax.swing.*; +import java.awt.*; +import java.lang.reflect.Array; + +public class MainCircles extends JFrame { + /* + Полностью разобраться с кодом + Прочитать методичку к следующему уроку + Написать класс Бэкграунд, изменяющий цвет канвы в зависимости от времени + * Реализовать добавление новых кружков по клику используя ТОЛЬКО массивы + ** Реализовать по клику другой кнопки удаление кружков (никаких эррейЛист) + * */ + + private static final int POS_X = 600; + private static final int POS_Y = 200; + private static final int WINDOW_WIDTH = 800; + private static final int WINDOW_HEIGHT = 600; + Sprite[] sprites = new Sprite[10]; + BackGround backGround = new BackGround(100); + + public static void main(String[] args) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + new MainCircles(); + } + }); + } + + private MainCircles() { + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setBounds(POS_X, POS_Y, WINDOW_WIDTH, WINDOW_HEIGHT); + setTitle("Circles"); + + GameCanvas gameCanvas = new GameCanvas(this); + add(gameCanvas); + initGame(); + setVisible(true); + } + + private void initGame() { + for (int i = 0; i < sprites.length; i++) { + sprites[i] = new Ball(); + } + } + + void onDrawPanel(GameCanvas canvas, Graphics g, float deltaTime) { + update(canvas, deltaTime); + render(canvas, g); + } + + private void update(GameCanvas canvas, float deltaTime) { + for (int i = 0; i < sprites.length; i++) { + sprites[i].update(canvas, deltaTime); + } + + //Обновление фона + backGround.update(canvas); + } + + private void render(GameCanvas canvas, Graphics g) { + for (int i = 0; i < sprites.length; i++) { + sprites[i].render(canvas, g); + } + + } + + //Удаление шарика из массива + protected void deleteSprite(){ + if (sprites.length > 0) { + Sprite[] newSptites = new Sprite[sprites.length - 1]; + System.arraycopy(sprites, 0, newSptites, 0, sprites.length - 1); + sprites = newSptites; + } + } + + //Добавление шарика + protected void addSprite(){ + Sprite[] newSprites = new Sprite[sprites.length + 1]; + System.arraycopy(sprites, 0, newSprites, 0, sprites.length); + newSprites[sprites.length] = new Ball(); + sprites = newSprites; + } +} diff --git a/MouseController.java b/MouseController.java new file mode 100644 index 0000000..f0128a9 --- /dev/null +++ b/MouseController.java @@ -0,0 +1,30 @@ +package ru.gb.jtwo.online.circles; + +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +public class MouseController extends MouseAdapter { + GameCanvas canvas; + + public MouseController(GameCanvas canvas) { + super(); + this.canvas = canvas; + } + @Override + public void mouseClicked(MouseEvent e) { + super.mouseClicked(e); + + if (e.getButton() == MouseEvent.BUTTON1) + { + canvas.getGameWindow().addSprite(); + } + + if (e.getButton() == MouseEvent.BUTTON3) + { + canvas.getGameWindow().deleteSprite(); + } + + } + + +} diff --git a/Sprite.java b/Sprite.java new file mode 100644 index 0000000..62a0cc2 --- /dev/null +++ b/Sprite.java @@ -0,0 +1,45 @@ +package ru.gb.jtwo.online.circles; + +import java.awt.*; + +public class Sprite { + protected float x; + protected float y; + protected float halfWidth; + protected float halfHeight; + + protected float getLeft() { + return x - halfWidth; + } + protected void setLeft(float left) { + x = left + halfWidth; + } + protected float getRight() { + return x + halfWidth; + } + protected void setRight(float right) { + x = right - halfWidth; + } + protected float getTop() { + return y - halfHeight; + } + protected void setTop(float top) { + y = top + halfHeight; + } + protected float getBottom() { + return y + halfHeight; + } + protected void setBottom(float bottom) { + y = bottom - halfHeight; + } + protected float getWidth() { + return 2f * halfWidth; + } + protected float getHeight() { + return 2f * halfHeight; + } + + void update(GameCanvas canvas, float deltaTime) {} + void render(GameCanvas canvas, Graphics g) {} + +} From e203493ce544f224fa4f64d6b10b727c32b2cc41 Mon Sep 17 00:00:00 2001 From: "demon17ya@gmail.com" Date: Wed, 25 Sep 2019 00:42:14 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=94=D0=BE=D0=BC=D0=B0=D1=88=D0=BD=D1=8F?= =?UTF-8?q?=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20#2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Java2_2/Main.java | 57 +++++++++++++++++++++++++++++++++++++++ Java2_2/SizeExeption.java | 5 ++++ 2 files changed, 62 insertions(+) create mode 100644 Java2_2/Main.java create mode 100644 Java2_2/SizeExeption.java diff --git a/Java2_2/Main.java b/Java2_2/Main.java new file mode 100644 index 0000000..c266ba4 --- /dev/null +++ b/Java2_2/Main.java @@ -0,0 +1,57 @@ +import java.util.Arrays; + +public class Main { + + public static void main(String[] args) { + String str = "500 3 1 2\n2 3 2 2\n5 6 7 1\n300 3 1 0"; + int summa = 0; + String[][] arr = null; + + try { + arr = strToArray(str); + summa = sum(arr); + System.out.println(summa); + }catch (SizeExeption e){ + System.out.println("Матрица имела неверный размер!"); + }catch (NumberFormatException e){ + System.out.println("Один из элементов не является числом!"); + } + + + } + + public static String[][] strToArray (String source) throws SizeExeption{ + String[][] returnedArray = new String[4][4]; + + String[] array_splitted = source.split("\n"); + + if (array_splitted.length != 4) { + throw new SizeExeption("Matrix size incorrect!"); + } + + for (int i = 0; i < array_splitted.length; i++){ + String[] row = array_splitted[i].split("\\s"); + + if (row.length != 4){ + throw new SizeExeption("Matrix size incorrect!"); + }else{ + for (int j = 0; j < row.length; j++) { + returnedArray[i][j] = row[j]; + } + } + + } + return returnedArray; + } + + public static int sum(String[][] source) throws NumberFormatException{ + int result = 0; + for (int i = 0; i < source.length; i++) { + for (int j = 0; j < source.length; j++) { + result+= Integer.parseInt(source[i][j]); + } + } + + return result / 2; + } +} diff --git a/Java2_2/SizeExeption.java b/Java2_2/SizeExeption.java new file mode 100644 index 0000000..2dafb6f --- /dev/null +++ b/Java2_2/SizeExeption.java @@ -0,0 +1,5 @@ +public class SizeExeption extends RuntimeException { + public SizeExeption(String message) { + super(message); + } +}