我正在创建一个程序,其中JPanel比JFrame大,因此它向左,向右,向上和向下滚动。在我的程序中,我试图找到鼠标相对于JPanel 的X和Y坐标。当我将一个MouseListener添加到JPanel(称为GraphicsPanel)时,侦听器似乎找到了相对于JFrame的鼠标的X和Y坐标。我已经在这上面搜索了几个小时,我真的很感激一些帮助!谢谢!
主要方法:
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
//Game Panel Creation
private JPanel gamePanel;
//Frame Y-Coordinate Offset (Due to the menu bar at the top of frame)
public static int frameHeightOffset = 23;
//Frame Width, Height
public static int frameWidth = 784, frameHeight = frameWidth + frameHeightOffset;
//Game Panel Width, Height
public static int graphicsPanelWidth = 896, graphicsPanelHeight = 896;
//Main Setup
public Main(){
//JFrame Setup
super("Pixel Wars");
setSize(new Dimension(frameWidth, frameHeight));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
//Game Panel Setup
gamePanel = new GraphicsPanel(graphicsPanelWidth, graphicsPanelHeight);
gamePanel.setPreferredSize(new Dimension(graphicsPanelWidth, graphicsPanelHeight));
gamePanel.setMinimumSize(new Dimension(graphicsPanelWidth, graphicsPanelHeight));
//Adding Listeners
addKeyListener(new ActionKeyListener());
gamePanel.addMouseListener(new ActionMouseListener());
//Adding Game Panel to JFrame
add(gamePanel);
setVisible(true);
}
//Main Method
public static void main(String[] args) {
new Main();
}
鼠标侦听器:
import java.awt.event.*;
import java.util.*;
public class ActionMouseListener implements MouseListener{
private int mouseX, mouseY;
public void mouseClicked(MouseEvent event){
mouseX = event.getX();
mouseY = event.getY();
if(mouseX >= GraphicsPanel.castle_red.getX() && mouseX <= GraphicsPanel.castle_red.getX() + (GraphicsPanel.blockSize * 5) && mouseY >= GraphicsPanel.castle_red.getY() && mouseY <= GraphicsPanel.castle_red.getY() + (GraphicsPanel.blockSize * 5)){
GraphicsPanel.peasantList.add(new Peasant(new Random().nextInt(400)+100, new Random().nextInt(400)+100));
System.out.println(mouseX + ", " + mouseY);
} else {
}
}
public void mousePressed(MouseEvent event){
}
public void mouseReleased(MouseEvent event){
}
public void mouseEntered(MouseEvent event){
}
public void mouseExited(MouseEvent event){
}
}
图形面板(JPanel):
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.Timer;
public class GraphicsPanel extends JPanel {
//Game Panel Width, Height
private int width, height;
//Game Camera Setup
public static Camera gameCamera = new Camera(0, 0);
//Grid Block Setup
public static int blockSize = 16;
private int gridX, gridY;
public static ArrayList<Block> blockList = new ArrayList<Block>();
//Castles Setup
public static Castle castle_red, castle_blue;
//Peasant Setup
public static ArrayList<Peasant> peasantList = new ArrayList<Peasant>();
//Graphics Panel Setup
public GraphicsPanel(int width, int height){
this.width = width;
this.height = height;
setSize(width, height);
}
//Paint Component
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
//Camera Motion
g2.translate(-gameCamera.getX(), -gameCamera.getY());
//Map Creator
blockList.clear();
gridY = -16;
for(int rows = 0; rows < height; rows += blockSize){
gridX = 0;
gridY += blockSize;
for(int columns = 0; columns < width; columns += blockSize){
blockList.add(new Block(gridX, gridY));
gridX += blockSize;
}
}
//Block Creator
for(int i = 0; i < blockList.size(); i++){
blockList.get(i).drawGrass(g2);
}
//Castle Creator
castle_red = new Castle(blockSize, blockSize);
castle_red.draw(g2, new ImageIcon("/Users/student/PixelWars/src/Resources/castle_red.png"));
castle_blue = new Castle(width-(blockSize*6), height-(blockSize*6));
castle_blue.draw(g2, new ImageIcon("/Users/student/PixelWars/src/Resources/castle_blue.png"));
//Peasant Creator
for(int i = 0; i < peasantList.size(); i++){
peasantList.get(i).draw(g2, new ImageIcon("/Users/student/PixelWars/src/Resources/peasant_front.png"));
}
//Frame Rate Setup
Timer frameRate = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
repaint();
}
});
frameRate.start();
}
}