如何复制可视化工件错误

时间:2016-01-08 23:56:49

标签: java swing awt

我有一个程序可以绘制一个可以在JPanel上驾驶的正方形。我有这个问题,每当我开车时,它会留下屏幕上的文物。当我开车时它会像JPanel Drawing Glitch那样。然后我在网上找到了一个解决方案,在我的paint(Graphics g)方法的开头,我应该放置一个“super.paintComponent(g)”。

完美无缺。但与此同时,我很享受有机会用这辆车进行绘图,所以我添加了一段代码,让我可以选择是否是super.paintComponent(g)'ing。但是,当我将这个代码转移到另一台计算机时,它不再有原始的错误,如果我愿意,不允许我绘制。我的问题是:我怎样才能复制这个错误,在我开车的时候把我以前的所有位置都拉到我身后?

这是我的代码。

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;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

/**
 * Created by chris on 1/5/16.
 */
public class Game extends JPanel{
int[] size = new int[2];
private Car car;
private JFrame frame;
private final double speedScalar = .5;
private final double angleScalar = 5;
boolean isTurningLeft = false;
boolean isTurningRight = false;
boolean isSpeedingUp = false;
boolean isSlowingDown = false;
private static Toolkit tk = Toolkit.getDefaultToolkit();
private ArrayList<Point> rocks = new ArrayList<>();
public static final int width = tk.getScreenSize().width;
public static final int height = tk.getScreenSize().height;
public boolean isDrawing = false;
public Color[] colorChoices = {Color.black,Color.blue,Color.red,Color.orange,Color.pink} ;
public Color chosenColor = Color.black;
public Game(){
    frame = new JFrame();
    frame.setPreferredSize(new Dimension(width,height));
    car = new Car(300,300);
    setPreferredSize(new Dimension(width,height));
    frame.add(this);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.addKeyListener(new KeyListener() {
        @Override
        public void keyTyped(KeyEvent e) {
            System.out.println(e.getKeyChar());
            switch(e.getKeyCode()){
                case 37:
                    isTurningLeft = true;
                    isTurningRight = false;
                    break;
                case 38:
                    isSpeedingUp = true;
                    isSlowingDown = false;
                    break;
                case 39:
                    isTurningLeft = false;
                    isTurningRight = true;
                    break;
                case 40:
                    isSpeedingUp = false;
                    isSlowingDown = true;
                    break;
            }

        }

        @Override
        public void keyPressed(KeyEvent e) {
            switch(e.getKeyCode()){
                case 37:
                    isTurningLeft = true;
                    isTurningRight = false;
                    break;
                case 40:
                    isSpeedingUp = true;
                    isSlowingDown = false;
                    break;
                case 39:
                    isTurningLeft = false;
                    isTurningRight = true;
                    break;
                case 38:
                    isSpeedingUp = false;
                    isSlowingDown = true;
                    break;
                case 32:
                    isDrawing = !isDrawing;

            }
            if(e.getKeyChar()=='c'){
                chosenColor = colorChoices[(int)(Math.random()*5)];
            } else if (e.getKeyChar()==' '){
                isDrawing=!isDrawing;
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            switch(e.getKeyCode()){
                case 37:
                    isTurningLeft = false;
                    break;
                case 40:
                    isSpeedingUp = false;
                    break;
                case 39:
                    isTurningRight = false;
                    break;
                case 38:
                    isSlowingDown = false;
                    break;
            }
        }
    });
    Timer time = new Timer(1000/45, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(isTurningRight){
                car.setAngle(car.getAngle()+Math.toRadians(angleScalar));
            } else if (isTurningLeft){
                car.setAngle(car.getAngle()-Math.toRadians(angleScalar));
            }

            if(isSpeedingUp){
                car.setSpeed(car.getSpeed()-speedScalar);
            } else if (isSlowingDown){
                car.setSpeed(car.getSpeed()+speedScalar);
            }
            if(car.getSpeed()<0){
                car.setSpeed(0);
            }
            car.setX(car.getX() + (Math.cos(car.getAngle())*car.getSpeed()));
            car.setY(car.getY() + (Math.sin(car.getAngle())*car.getSpeed()));
            if(car.getX()<0){
                car.setX(0);
                car.setSpeed(0);
            }
            if(car.getX()>width){
                car.setX(width);
                car.setSpeed(0);
            }
            if(car.getY()<0){
                car.setY(0);
                car.setSpeed(0);
            }
            if(car.getY()>height){
                car.setY(height);
                car.setSpeed(0);
            }
            repaint();
        }
    });
    double end = Math.random()*15;
    frame.setVisible(true);
    time.start();

}

public void paint(Graphics g){
    if(!isDrawing) {
        super.paintComponent(g);
    }
    /**
     * Painting the car
     */
    g.setColor(chosenColor);
    double sin = Math.sin(car.getAngle());
    double cos = Math.cos(car.getAngle());
    int[] xPoints = {(int)car.getX(),(int)(car.getX()+cos*car.width),(int)(car.getX()-sin*car.length+cos*car.width),
            (int)(car.getX()-sin*car.length)};
    int[] yPoints = {(int)car.getY(),(int)(car.getY()+car.width*sin),(int)(car.getY()+cos*car.length+sin*car.width),
            (int)(car.getY()+car.length*cos)};

    Polygon car = new Polygon(xPoints,yPoints,4);
    int[] recX = {400,450,450,400};
    int[] recY = {400,400,450,450};
    Rectangle2D test = new Rectangle(400,400,100,100);
    ((Graphics2D)g).fill(car);
    ((Graphics2D)g).fill(test);
    if(car.intersects(test)){
        chosenColor = Color.red;
    }
    tk.sync();
}




/**
 * Created by chris on 1/5/16.
 */
public class Main {
public static void main(String[] args) {
    Game g = new Game();
}
}



/**
 * Created by chris on 1/5/16.
 * Car class for my driving program.
 */
public class Car {
private double speed;
private double angle;

/**
 * Coordinates on plane
 */
private double x;
private double y;

//For some reason this ends up being the size of the front
public int length = 10;
//and this ends up being the size of the sides.
public int width = 25;


public double getSpeed() {
    return speed;
}

public void setSpeed(double speed) {
    this.speed = speed;
}

public double getAngle() {
    return angle;
}

public void setAngle(double angle) {
    this.angle = angle;
}

public double getX() {
    return x;
}

public void setX(double x) {
    this.x = x;
}

public double getY() {
    return y;
}

public void setY(double y) {
    this.y = y;
}

public Car(double x, double y) {
    this.x = x;
    this.y = y;
    speed = 0;
    angle = 0;
}

}

0 个答案:

没有答案