Медленная функция repaint() - Java
Формулировка задачи:
Здравствуйте! Я пытаюсь написать арканоид по урокам из youtube. При запуске компилятором приложения картинка обновляется очень медленно, так что платформа, отбивающая мячик, перемещается на любое расстояние за 2 минуты, а не плавно. Еще возникает ошибка Exception in thread. Как можно решить проблему?
Листинг программы
- //Main.java
- package com.simplegame.arcanoid;
- import java.awt.*;
- import javax.swing.JFrame;
- /**
- * Created by Admin on 26.08.2017.
- */
- public class Main {
- public static void main(String[] args) {
- JFrame obj = new JFrame();
- game_mechanics mech = new game_mechanics();
- obj.setBounds(10, 10, 710, 630);
- obj.setTitle("Welcome to arcanoid!");
- obj.setVisible(true);
- obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- obj.add(mech);
- }
- }
- //BrickGenerator
- package com.simplegame.arcanoid;
- import javax.swing.*;
- import java.awt.*;
- /**
- * Created by Admin on 26.08.2017.
- */
- public class BrickGenerator {
- public int map[][];
- public int brickWidth;
- public int brickHeight;
- public BrickGenerator(int row, int col){
- map = new int[row][col];
- for(int i = 0; i < map.length; i++){
- for(int j = 0; j<map[0].length; j++){
- map[i][j] = 1;
- }
- }
- brickWidth = 540/col;
- brickHeight = 150/row;
- }
- public void draw(Graphics2D g ){
- for(int i = 0; i < map.length; i++){
- for(int j = 0; j<map[0].length; j++){
- if( map[i][j] > 0) {
- g.setColor(Color.WHITE);
- g.fillRect(j * brickWidth + 80, i* brickHeight + 50, brickWidth, brickHeight);
- }
- }
- }
- }
- }
- //game_mechanics.java
- package com.simplegame.arcanoid;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- public class game_mechanics extends JPanel implements KeyListener, ActionListener {
- private boolean play = false;
- private int score = 0;
- private int totalBricks = 21;
- private Timer time;
- private int delay = 8;
- private int playerX = 310;
- private int ballposX = 120;
- private int ballposY = 350;
- private int ballXdir = -1;
- private int ballYdir = -2;
- private BrickGenerator map;
- public game_mechanics(){
- BrickGenerator map = new BrickGenerator(3, 7);
- addKeyListener(this);
- setFocusable(true);
- setFocusTraversalKeysEnabled(false);
- Timer timer = new Timer(delay, this);
- timer.start();
- }
- public void paint(Graphics g) {
- // background
- g.setColor(Color.black);
- g.fillRect(1, 1, 692, 592);
- //drawing map
- map.draw((Graphics2D) g);
- //borders
- g.setColor(Color.yellow);
- g.fillRect(0, 0, 3, 592);
- g.fillRect(0, 0, 692, 3);
- g.fillRect(691, 0, 3, 592);
- //the paddle
- g.setColor(Color.green);
- g.fillRect(playerX, 550 , 100, 8);
- //the ball
- g.setColor(Color.yellow);
- g.fillOval(ballposX , ballposY , 20, 20);
- g.dispose();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- Timer timer = new Timer(delay, this);
- timer.start();
- if(play) {
- if(new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX, 550, 100, 8))){
- ballYdir = -ballYdir;
- }
- ballposX += ballXdir;
- ballposY += ballYdir;
- if(ballposX < 0) {
- ballXdir = -ballXdir;
- }
- if(ballposY < 0) {
- ballYdir = -ballYdir;
- }
- if(ballposX > 670) {
- ballXdir = -ballXdir;
- }
- }
- repaint();
- }
- @Override
- public void keyTyped(KeyEvent e) { }
- @Override
- public void keyPressed(KeyEvent e) {
- if(e.getKeyCode() == KeyEvent.VK_RIGHT){
- if(playerX >=600) {
- playerX = 600;
- } else{
- moveRight();
- }
- }
- if(e.getKeyCode() == KeyEvent.VK_LEFT){
- if(playerX < 10) {
- playerX = 10;
- } else{
- moveLeft();
- }
- }
- }
- public void moveRight() {
- play = true;
- playerX += 20;
- }
- public void moveLeft() {
- play = true;
- playerX -= 20;
- }
- @Override
- public void keyReleased(KeyEvent e) { }
- }
Решение задачи: «Медленная функция repaint()»
textual
Листинг программы
- import java.awt.*;
- import java.util.Random;
- /**
- * Created by Admin on 26.08.2017.
- */
- public class BrickGenerator {
- public int map[][];
- public int brickWidth;
- public int brickHeight;
- private static Random rnd = new Random();
- public BrickGenerator(int row, int col) {
- map = new int[row][col];
- for (int i = 0; i < map.length; i++) {
- for (int j = 0; j < map[0].length; j++) {
- map[i][j] = (i+j+1)%2;
- }
- }
- brickWidth = 540 / col;
- brickHeight = 150 / row;
- }
- public void draw(Graphics2D g) {
- for (int i = 0; i < map.length; i++) {
- for (int j = 0; j < map[0].length; j++) {
- if (map[i][j] > 0) {
- g.setColor(Color.WHITE);
- g.fillRect(j * brickWidth + 80, i * brickHeight + 50, brickWidth, brickHeight);
- }
- }
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д