.intersects()方法仅适用于某些示例

时间:2016-01-17 22:17:05

标签: java jpanel intersection

我正在编写一个简单的程序,当两个矩形相交时,它应显示“相交”。出于某种原因......这与我使用的代码不兼容。这是我的代码:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JPanel;

public class Game extends JPanel implements MouseMotionListener {
int rectX;
int rectY;
Rectangle rect1;
Rectangle rect2;

public Game() {
    rect1 = new Rectangle(rectX, rectY, 20, 20);
    rect2 = new Rectangle(250, 250, 50, 50);
    rectX = 150;
    rectY = 150;
    setBackground(Color.ORANGE);
    addMouseMotionListener(this);
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (rect1.intersects(rect2)) {
        g.drawString("Intersecting", 75, 75);
    } else {
        g.drawString("Not Intersecting", 75, 75);
    }

    g.setColor(Color.GREEN);
    g.fillRect(rect2.x, rect2.y, rect2.width, rect2.height);
    g.setColor(Color.BLUE);
    g.fillRect(rectX, rectY, rect1.width, rect1.height);

    repaint();
}

@Override
public void mouseDragged(MouseEvent e) {
    rectX = e.getX();
    rectY = e.getY();
}

@Override
public void mouseMoved(MouseEvent e) {
    rectX = e.getX();
    rectY = e.getY();

}
}
import java.awt.EventQueue;

import javax.swing.JFrame;

public class Main {

public static void main(String[] args) {
    new Main();
}

public Main() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {

            JFrame frame = new JFrame("Game");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new Game());
            frame.setSize(500, 500);
            frame.setVisible(true);
        }
    });
}
}

但由于某种原因,这段代码确实有效:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;

public class Main extends JFrame{

private Image dbImage;
private Graphics dbg;

int rectX, rectY;

public Main(){
    setSize(400,300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setVisible(true);
    addMouseMotionListener(new AL());
}




public void paint(Graphics g){
    dbImage = createImage(getWidth(), getHeight());
    dbg = dbImage.getGraphics();
    paintComponent(dbg);
    g.drawImage(dbImage, 0, 0, this);

}

public void paintComponent(Graphics g) {
    g.setColor(Color.BLACK);

    Rectangle r1 = new Rectangle(rectX, rectY, 25, 25);
    Rectangle r2 = new Rectangle(175, 175, 50, 50);
    g.fillRect(r2.x, r2.y, r2.width, r2.height);
    g.fillRect(r1.x, r1.y, r1.width, r1.height);
    if(r1.intersects(r2)){
        g.drawString("HIT", 175, 140);
    }
    repaint();
}

public class AL extends MouseAdapter {
    public void mouseMoved(MouseEvent e){
        rectX = e.getX()-12;
        rectY = e.getY()-12;
    }
}

public static void main(String[] args){
    Main main = new Main();
}   
}

任何人都可以向我解释这两者之间的区别以及为什么一个人工作而另一个人不工作?感谢。

0 个答案:

没有答案