Java相交方法无法正常工作

时间:2014-06-27 23:20:23

标签: java swing

有人可以告诉我为什么我的代码无法正常工作吗?

当我移动它时,我试图改变颜色,但它只是不起作用。

我正在使用intersects方法。

package com.dilkiel.game;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Screen extends JPanel implements Runnable {

private static final long serialVersionUID = 1L;

private Thread thread;

public int tickCount = 0;
public boolean running = false;
public int xPlayerOffset = 100;
public int yPlayerOffset = 100;
public int WIDTH = 1280;
public int HEIGHT = 720;

public InputHandler input = new InputHandler(this);

public Screen() {
    setFocusable(true);
    setMaximumSize(new Dimension(WIDTH, HEIGHT));
    setMinimumSize(new Dimension(WIDTH, HEIGHT));
    setPreferredSize(new Dimension(WIDTH, HEIGHT));
    start();
}

public void tick() {
    tickCount++;
    if (input.up.isPressed()) {
        yPlayerOffset -= 5;
    }
    if (input.down.isPressed()) {
        yPlayerOffset += 5;
    }
    if (input.left.isPressed()) {
        xPlayerOffset -= 5;
    }
    if (input.right.isPressed()) {
        xPlayerOffset += 5;
    }
}

public synchronized void start() {
    running = true;
    thread = new Thread(this, "Game Loop");
    thread.start();
}

public synchronized void stop() {
    running = false;
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

Image frontView = new ImageIcon("res/frontView.png").getImage();
Rectangle player = new Rectangle(xPlayerOffset, yPlayerOffset + 40, 40, 40);
Rectangle box = new Rectangle(250, 250, 80, 80);

public void paint(Graphics g) {

    g.setColor(Color.CYAN);
    g.fillRect(0, 0, WIDTH, HEIGHT);
    g.drawImage(frontView, xPlayerOffset, yPlayerOffset, this);

    g.setColor(Color.BLACK);
    if (player.intersects(box)) {
        //i want this to work can you please show me how. Thanks
        g.setColor(Color.GREEN);
    }
    g.fillRect(xPlayerOffset, yPlayerOffset + 40, 40, 40);
    g.fillRect(250, 250, 80, 80);
    g.dispose();
}

public void run() {
    long lastTime = System.nanoTime();
    double nsPerTick = 1000000000D / 60D;

    int ticks = 0;
    int frames = 0;

    long lastTimer = System.currentTimeMillis();
    double delta = 0;

    while (running) {
        long now = System.nanoTime();
        delta += (now - lastTime) / nsPerTick;
        lastTime = now;
        boolean shouldRender = false;

        while (delta >= 1) {
            ticks++;
            tick();
            delta -= 1;
            shouldRender = true;
        }
        /*
         * try { Thread.sleep(2); } catch (InterruptedException e) {
         * e.printStackTrace(); }
         */
        if (shouldRender) {
            frames++;
            repaint();
        }

        if (System.currentTimeMillis() - lastTimer >= 1000) {
            lastTimer += 1000;

            System.out.println(ticks + ", " + frames);
            frames = 0;
            ticks = 0;
        }
    }
}
}

2 个答案:

答案 0 :(得分:1)

我提到了一些可能对你有帮助的要点。

  1. 使用Swing Timer代替Thread

    了解详情How to Use Swing Timers

  2. 覆盖自定义绘图的paintComponent()方法,而不是覆盖paint()方法。

  3. 不要忘记在重写方法中调用super.paintComponent()
  4. 覆盖JComponent#getPreferredSize()而非使用setPreferredSize()

    了解详情Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?


  5. 示例代码:( JPanel中的自定义绘图

    final JPanel panel = new JPanel(){
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            // your custom painting code here
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(..., ...);
        }
    };
    

    示例代码:( Swing Timer

    // wait for 1 second
    Timer timer = new Timer(1000, new ActionListener() {
    
        @Override
        public void actionPerformed(ActionEvent arg0) {            
            // next call 
        }
    });
    timer.setRepeats(false);
    timer.start()
    

答案 1 :(得分:0)

我认为不是使用交叉方法,而是可以添加鼠标悬停监听器并编写代码来更改其中的颜色,这样可以更好地控制和响应。