我想获取 ImageIcon 在 JPanel(Java) 上的当前位置
I want to get the current position of an ImageIcon on a JPanel(Java)
我想获取图像的坐标,以便可以根据坐标更改 JPanel 的背景颜色。这是我到目前为止所拥有的。该代码允许用户向上、向下、向右和向左移动箭头,因此如果箭头位于第一象限,背景应为红色,如果箭头移动到第二象限,背景颜色将变为绿色,依此类推
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DirectionPanel extends JPanel
{
private final int X = 200;
private Point point = null;
private final int WIDTH = 400, HEIGHT = 200;
private final int JUMP = 10; // increment for image movement
private final int IMAGE_SIZE = 31;
private ImageIcon up, down, right, left, currentImage;
private int x, y;
//-----------------------------------------------------------------
// Constructor: Sets up this panel and loads the images.
//-----------------------------------------------------------------
public DirectionPanel()
{
addKeyListener (new DirectionListener());
x = WIDTH / 2;
y = HEIGHT / 2;
up = new ImageIcon ("arrowUp.gif");
down = new ImageIcon ("arrowDown.gif");
left = new ImageIcon ("arrowLeft.gif");
right = new ImageIcon ("arrowRight.gif");
currentImage = right;
setBackground (Color.black);
setPreferredSize (new Dimension(WIDTH, HEIGHT));
setFocusable(true);
}
//-----------------------------------------------------------------
// Draws the image in the current location.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent (page);
currentImage.paintIcon (this, page, x, y);
}
//*****************************************************************
// Represents the listener for keyboard activity.
//*****************************************************************
private class DirectionListener implements KeyListener
{
//--------------------------------------------------------------
// Responds to the user pressing arrow keys by adjusting the
// image and image location accordingly.
//--------------------------------------------------------------
public void keyPressed (KeyEvent event)
{
switch (event.getKeyCode())
{
case KeyEvent.VK_UP:
currentImage = up;
y -= JUMP;
break;
case KeyEvent.VK_DOWN:
currentImage = down;
y += JUMP;
break;
case KeyEvent.VK_LEFT:
currentImage = left;
x -= JUMP;
break;
case KeyEvent.VK_RIGHT:
currentImage = right;
x += JUMP;
break;
}
point = ???????
if(point.x < 200 && point.y < 100)
{
setBackground(Color.red);
}
else if(point.x < 200 && point.y > 100)
{
setBackground(Color.green);
}
else if(point.y > 100 && point.x > 200)
{
setBackground(Color.cyan);
}
else if(point.y < 100 && point.x > 200)
{
setBackground(Color.yellow);
}
repaint();
}
//--------------------------------------------------------------
// Provide empty definitions for unused event methods.
//--------------------------------------------------------------
public void keyTyped (KeyEvent event) {}
public void keyReleased (KeyEvent event) {}
}
}
您使用变量:"x" 和 "y" 来绘制图像,因此这些变量将是您的图像在 JPanel 中的坐标。
您可以将您的积分设置为:
point = new Point(x, y);
现在,当您更新 "x" 和 "y" 时,点也会更新,背景将根据您的规格绘制。
希望对您有所帮助!
我想获取图像的坐标,以便可以根据坐标更改 JPanel 的背景颜色。这是我到目前为止所拥有的。该代码允许用户向上、向下、向右和向左移动箭头,因此如果箭头位于第一象限,背景应为红色,如果箭头移动到第二象限,背景颜色将变为绿色,依此类推
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DirectionPanel extends JPanel
{
private final int X = 200;
private Point point = null;
private final int WIDTH = 400, HEIGHT = 200;
private final int JUMP = 10; // increment for image movement
private final int IMAGE_SIZE = 31;
private ImageIcon up, down, right, left, currentImage;
private int x, y;
//-----------------------------------------------------------------
// Constructor: Sets up this panel and loads the images.
//-----------------------------------------------------------------
public DirectionPanel()
{
addKeyListener (new DirectionListener());
x = WIDTH / 2;
y = HEIGHT / 2;
up = new ImageIcon ("arrowUp.gif");
down = new ImageIcon ("arrowDown.gif");
left = new ImageIcon ("arrowLeft.gif");
right = new ImageIcon ("arrowRight.gif");
currentImage = right;
setBackground (Color.black);
setPreferredSize (new Dimension(WIDTH, HEIGHT));
setFocusable(true);
}
//-----------------------------------------------------------------
// Draws the image in the current location.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent (page);
currentImage.paintIcon (this, page, x, y);
}
//*****************************************************************
// Represents the listener for keyboard activity.
//*****************************************************************
private class DirectionListener implements KeyListener
{
//--------------------------------------------------------------
// Responds to the user pressing arrow keys by adjusting the
// image and image location accordingly.
//--------------------------------------------------------------
public void keyPressed (KeyEvent event)
{
switch (event.getKeyCode())
{
case KeyEvent.VK_UP:
currentImage = up;
y -= JUMP;
break;
case KeyEvent.VK_DOWN:
currentImage = down;
y += JUMP;
break;
case KeyEvent.VK_LEFT:
currentImage = left;
x -= JUMP;
break;
case KeyEvent.VK_RIGHT:
currentImage = right;
x += JUMP;
break;
}
point = ???????
if(point.x < 200 && point.y < 100)
{
setBackground(Color.red);
}
else if(point.x < 200 && point.y > 100)
{
setBackground(Color.green);
}
else if(point.y > 100 && point.x > 200)
{
setBackground(Color.cyan);
}
else if(point.y < 100 && point.x > 200)
{
setBackground(Color.yellow);
}
repaint();
}
//--------------------------------------------------------------
// Provide empty definitions for unused event methods.
//--------------------------------------------------------------
public void keyTyped (KeyEvent event) {}
public void keyReleased (KeyEvent event) {}
}
}
您使用变量:"x" 和 "y" 来绘制图像,因此这些变量将是您的图像在 JPanel 中的坐标。
您可以将您的积分设置为:
point = new Point(x, y);
现在,当您更新 "x" 和 "y" 时,点也会更新,背景将根据您的规格绘制。
希望对您有所帮助!