问题描述
我正在与Swing第一次合作,并且遇到了一些问题。我有一个JFrame分成四个JPanel。在JFrame上有一个MouseListener,就像这样点击时,如果点击位于左侧栏内,请确定正在选择13个图标中的哪一个。如果点击位于右侧游戏窗格&一个图标已被选中,将其放置在点击的位置。
这里完成
@Override
public void mouseClicked(MouseEvent event){
// TODO自动生成的方法存根
xPos = event.getX();
yPos = event.getY() - 25;
//如果点击在工具栏内
if(xPos if(yPos> -1&&& yPos< 48)
//第一个工具image
image = new ImageIcon(getClass()。getResource(_image path _))。getImage();
else if(yPos> = 48&& yPos< 96)
//第二个工具镜像
image = new ImageIcon(getClass()。getResource(_image path _))。getImage );
else if(yPos> = 96&& yPos< 144)
//第三个工具镜像
image = new ImageIcon(getClass()。getResource(_image path _))。getImage );
else if(yPos> = 144&& yPos< 192)
//第四工具图像
image = new ImageIcon(getClass()。getResource(imagepath))。getImage() ;
else if(yPos> = 192&& yPos< 240)
//第五工具图像
image = new ImageIcon(getClass()。getResource(imagepath))。getImage() ;
else if(yPos> = 240&& yPos< 288)
//第六工具图像
image = new ImageIcon(getClass()。getResource(imagepath))。getImage() ;
else if(yPos> = 288&& yPos< 336)
//第七个工具镜像
image = new ImageIcon(getClass()。getResource(imagepath))。getImage() ;
else if(yPos> = 336&& yPos< 384)
//首先NPC图像
image = new ImageIcon(getClass()。getResource(imagepath))。getImage() ;
else if(yPos> = 384&& yPos< 432)
// second NPC image
image = new ImageIcon(getClass()。getResource(imagepath))。getImage() ;
else if(yPos> = 432&& yPos< 480)
//第三个NPC图像
image = new ImageIcon(getClass()。getResource(imagepath))。getImage() ;
else if(yPos> = 480&& yPos< 528)
//第一个装饰图像
image = new ImageIcon(getClass()。getResource(imagepath))。getImage() ;
else if(yPos> = 528&& yPos< 576)
// Second Decoration image
image = new ImageIcon(getClass()。getResource(imagepath))。getImage() ;
else if(yPos> = 576&& yPos< = 625)
//第三个装饰图像
image = new ImageIcon(getClass()。getResource(imagepath))。getImage );
}
//如果点击位于游戏窗格内
else if(xPos> 75&& yPos< 625){
//已选择一个工具
if(image!= null){
placedTool = this.image;
this.image = null;
placeable = true;
//游戏窗格上的图像和位置已被选中
if(placeable& this< image == null){
ImageInfo newImg = new ImageInfo(xPos,yPos,image);
gamePane.additions.add(newImg);
gamePane.repaint();
System.out.println(IMAGE PLACED @+ xPos +,+ yPos);
placeable = false;
System.out.println(CLICK:(+ xPos +,+ yPos +));
}
其中imagepath是50x50图标的路径。这部分工作正常,没有错误。然而,gamePane并没有得到正确的重绘。
gamePane现在只有一张背景图片。随着组件的添加,它们应该被绘制在最上面。所有被绘的是背景图片。有没有办法使用Graphics.drawImage()指定Z组件。这是我为gamePane的paintComponent函数所做的(粗体,因为这是主要问题)
$ $ p $ @Override
protected void paintComponent(Graphics g){
g.drawImage(backgroundImg,0,0,null);
for(ImageInfo add:additions){
g.drawImage(add.getImage(),add.getX(),add.getY(),null);
$ / code $ / pre
$ b $ p
$ b
List adds = new ArrayList();
ImageInfo类只包含一个图像,一个x坐标和一个y坐标
public class ImageInfo {
private int x;
private int y;
私人图片图片;
public int getX(){
return x;
}
public int getY(){
return y;
}
public Image getImage(){
return image;
public ImageInfo(int x,int y,Image image){
super();
this.x = x;
this.y = y;
this.image = image;
}
}
修正:
谢谢mKorbel。通过定义mouseClicked方法之外的所有图像
Image tool1 = new ImageIcon(getClass()。getResource(toolBar.TOOL1) ).getImage();
Image tool2 = new ImageIcon(getClass()。getResource(toolBar.TOOL2))。getImage();
Image tool3 = new ImageIcon(getClass()。getResource(toolBar.TOOL3))。getImage();
Image tool4 = new ImageIcon(getClass()。getResource(toolBar.TOOL4))。getImage();
Image tool5 = new ImageIcon(getClass()。getResource(toolBar.TOOL5))。getImage();
Image tool6 = new ImageIcon(getClass()。getResource(toolBar.TOOL6))。getImage();
Image tool7 = new ImageIcon(getClass()。getResource(toolBar.TOOL7))。getImage();
Image npc1 = new ImageIcon(getClass()。getResource(toolBar.NPC1))。getImage();
Image npc2 = new ImageIcon(getClass()。getResource(toolBar.NPC2))。getImage();
Image npc3 = new ImageIcon(getClass()。getResource(toolBar.NPC3))。getImage();
Image decor1 = new ImageIcon(getClass()。getResource(toolBar.DECOR1))。getImage();
Image decor2 = new ImageIcon(getClass()。getResource(toolBar.DECOR2))。getImage();
Image decor3 = new ImageIcon(getClass()。getResource(toolBar.DECOR3))。getImage();
并执行mouseClicked函数,如
@Override
public void mouseClicked(MouseEvent event){
// TODO自动生成的方法存根
xPos = event.getX();
yPos = event.getY() - 25;
//如果点击在工具栏内
if(xPos if(yPos> -1&&& yPos< 48)
//第一个工具image
image = tool1;
else if(yPos> = 48&& yPos< 96)
//第二工具图像
image = tool2;
else if(yPos> = 96&& yPos< 144)
//第三工具图像
image = tool3;
else if(yPos> = 144&& yPos< 192)
//第四工具图像
image = tool4;
else if(yPos> = 192&& yPos< 240)
//第五工具图像
image = tool5;
else if(yPos> = 240&& yPos< 288)
//第六工具图像
image = tool6;
else if(yPos> = 288&& yPos< 336)
//第七工具图像
image = tool7;
else if(yPos> = 336&& yPos< 384)
//第一个NPC图像
image = npc1;
else if(yPos> = 384&& yPos< 432)
//第二个NPC图像
image = npc2;
else if(yPos> = 432&& yPos< 480)
//第三NPC图像
image = npc3;
else if(yPos> = 480&& yPos< 528)
// First Yard Decoration image
image = decor1;
else if(yPos> = 528&& yPos< 576)
// Second Yard Decoration image
image = decor2;
else if(yPos> = 576&& yPos< = 625)
// Third Yard装饰图片
image = decor3;
}
//如果点击位于游戏窗格内
else if(xPos> 75&& yPos< 625){
//已选择一个工具
if(image!= null){
placedTool = this.image;
this.image = null;
placeable = true;
if(placeable& this.image == null){
GamePiece newImg = new GamePiece(placedTool,xPos,yPos);
gamePane.additions.add(newImg);
gamePane.repaint();
System.out.println(IMAGE PLACED @+ xPos +,+ yPos);
placeable = false;
System.out.println(CLICK:(+ xPos +,+ yPos +));
}
图片被添加到背景图片的顶部,由前面给出的paintComponent方法。他们有点偏离立场,但仍然可见。
只有一条评论
-
发布可短时间运行,可编辑
-
为什么有
yPos = event.getY() - 25;
什么是逻辑代表整数在-25
-
在局部变量中存储所有
ImageIcons
ImageIcons
在任何数组中或List
在有固定逻辑而不是坐标,然后加载image = new ImageIcon(getClass()。getResource(
)绘制在paintComponent
中,不要提供任何FileIO
在运行时
- 创建由
GridLayout
放置的JLabel
的网格,添加MouseListener
到JLabel
s,从更改
JLabel.setIcon(myImageIcon)
莫
例如
使用事件,其他逻辑我缺少SSCCE b$ b
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class ChessBoard extends JFrame {
private JFrame frame = new JFrame();
private JPanel panel = new JPanel();
private图标errorIcon = UIManager.getIcon(OptionPane.errorIcon);
私有图标infoIcon = UIManager.getIcon(OptionPane.informationIcon);
private图标warnIcon = UIManager.getIcon(OptionPane.warningIcon);
private图标questnIcon = UIManager.getIcon(OptionPane.questionIcon);
私人JButton按钮=新JButton(重置我的棋盘);
public ChessBoard(){
panel.setLayout(new GridLayout(8,8,0,0));
for(int i = 0; i final JLabel label = new JLabel();
label.setIcon(errorIcon);
label.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
if(e.getButton()== MouseEvent.BUTTON3){
label.setIcon(infoIcon);
} else {
label.setIcon(warnIcon);
}
}
@Override
public void mouseEntered(MouseEvent me){
label.setIcon(questnIcon);
}
});
panel.add(label);
}
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
for(Component c:panel.getComponents() ){
if(c instance of JLabel){
JLabel label =(JLabel)c;
label.setIcon(errorIcon);
}
}
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.add(button,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
public static void main(String [] args){
java.awt.EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
new ChessBoard();
}
});
}
}
I am working with Swing for the first time and am having a bit of an issue. What I have is a JFrame split into four JPanels. There is a MouseListener on the JFrame that acts like this
On a click, if the click is inside of the left hand bar, determine which of the 13 icons is being selected. If the click is inside the right hand "game pane" & an icon has been selected, place it at the location clicked.
This is done here
@Override
public void mouseClicked(MouseEvent event) {
// TODO Auto-generated method stub
xPos = event.getX();
yPos = event.getY()-25;
//If click is inside tool bar
if(xPos<=75){
if(yPos>-1 && yPos<48)
//First tool image
image = new ImageIcon(getClass().getResource(_image path_)).getImage();
else if(yPos>=48 && yPos<96)
//Second tool image
image = new ImageIcon(getClass().getResource(_image path_)).getImage();
else if(yPos>=96 && yPos<144)
//Third tool image
image = new ImageIcon(getClass().getResource(_image path_)).getImage();
else if(yPos>=144 && yPos<192)
//Fourth tool image
image = new ImageIcon(getClass().getResource(imagepath)).getImage();
else if(yPos>=192 && yPos<240)
//Fifth tool image
image = new ImageIcon(getClass().getResource(imagepath)).getImage();
else if(yPos>=240 && yPos<288)
//Sixth tool image
image = new ImageIcon(getClass().getResource(imagepath)).getImage();
else if(yPos>=288 && yPos<336)
//Seventh tool image
image = new ImageIcon(getClass().getResource(imagepath)).getImage();
else if(yPos>=336 && yPos<384)
//First NPC image
image = new ImageIcon(getClass().getResource(imagepath)).getImage();
else if(yPos>=384 && yPos<432)
//second NPC image
image = new ImageIcon(getClass().getResource(imagepath)).getImage();
else if(yPos>=432 && yPos<480)
//Third NPC image
image = new ImageIcon(getClass().getResource(imagepath)).getImage();
else if(yPos>=480 && yPos<528)
//First Decoration image
image = new ImageIcon(getClass().getResource(imagepath)).getImage();
else if(yPos>=528 && yPos<576)
//Second Decoration image
image = new ImageIcon(getClass().getResource(imagepath)).getImage();
else if(yPos>=576 && yPos<=625)
//Third Decoration image
image = new ImageIcon(getClass().getResource(imagepath)).getImage();
}
//If click is within Game Pane
else if (xPos>75 && yPos<625){
//A tool has been selected
if(image!=null){
placedTool = this.image;
this.image = null;
placeable = true;
}
}
//An image and location on the game pane has been selected
if(placeable && this.image==null){
ImageInfo newImg = new ImageInfo(xPos, yPos, image);
gamePane.additions.add(newImg);
gamePane.repaint();
System.out.println("IMAGE PLACED @ " + xPos + ", " + yPos);
placeable = false;
}
System.out.println("CLICK: (" + xPos + "," + yPos +")");
}
Where imagepath is the path of a 50x50 icon. This portion works correctly with no errors. However gamePane doesn't get repainted properly.
gamePane simply has a background image for now. As components are added, they're supposed to be painted on top. All that gets painted is the background image though. Is there any way to specify the Z component using Graphics.drawImage(); Here's what I have for the paintComponent function of gamePane (bolded because this is the main issue)
@Override
protected void paintComponent(Graphics g) {
g.drawImage(backgroundImg, 0, 0, null);
for(ImageInfo add : additions){
g.drawImage(add.getImage(), add.getX(), add.getY(), null);
}
}
Where additions is defined like this
List additions = new ArrayList();
And the ImageInfo class just contains an image, an x coordinate, and a y coordinate
public class ImageInfo {
private int x;
private int y;
private Image image;
public int getX() {
return x;
}
public int getY() {
return y;
}
public Image getImage() {
return image;
}
public ImageInfo(int x, int y, Image image) {
super();
this.x = x;
this.y = y;
this.image = image;
}
}
FIXED:
Thank you mKorbel. By defining all the images outside of the mouseClicked method
Image tool1 = new ImageIcon(getClass().getResource(toolBar.TOOL1)).getImage();
Image tool2 = new ImageIcon(getClass().getResource(toolBar.TOOL2)).getImage();
Image tool3 = new ImageIcon(getClass().getResource(toolBar.TOOL3)).getImage();
Image tool4 = new ImageIcon(getClass().getResource(toolBar.TOOL4)).getImage();
Image tool5 = new ImageIcon(getClass().getResource(toolBar.TOOL5)).getImage();
Image tool6 = new ImageIcon(getClass().getResource(toolBar.TOOL6)).getImage();
Image tool7 = new ImageIcon(getClass().getResource(toolBar.TOOL7)).getImage();
Image npc1 = new ImageIcon(getClass().getResource(toolBar.NPC1)).getImage();
Image npc2 = new ImageIcon(getClass().getResource(toolBar.NPC2)).getImage();
Image npc3 = new ImageIcon(getClass().getResource(toolBar.NPC3)).getImage();
Image decor1 = new ImageIcon(getClass().getResource(toolBar.DECOR1)).getImage();
Image decor2 = new ImageIcon(getClass().getResource(toolBar.DECOR2)).getImage();
Image decor3 = new ImageIcon(getClass().getResource(toolBar.DECOR3)).getImage();
and the executing the mouseClicked function like
@Override
public void mouseClicked(MouseEvent event) {
// TODO Auto-generated method stub
xPos = event.getX();
yPos = event.getY()-25;
//If click is inside tool bar
if(xPos<=75){
if(yPos>-1 && yPos<48)
//First tool image
image = tool1;
else if(yPos>=48 && yPos<96)
//Second tool image
image = tool2;
else if(yPos>=96 && yPos<144)
//Third tool image
image = tool3;
else if(yPos>=144 && yPos<192)
//Fourth tool image
image = tool4;
else if(yPos>=192 && yPos<240)
//Fifth tool image
image = tool5;
else if(yPos>=240 && yPos<288)
//Sixth tool image
image = tool6;
else if(yPos>=288 && yPos<336)
//Seventh tool image
image = tool7;
else if(yPos>=336 && yPos<384)
//First NPC image
image = npc1;
else if(yPos>=384 && yPos<432)
//second NPC image
image = npc2;
else if(yPos>=432 && yPos<480)
//Third NPC image
image = npc3;
else if(yPos>=480 && yPos<528)
//First Yard Decoration image
image = decor1;
else if(yPos>=528 && yPos<576)
//Second Yard Decoration image
image = decor2;
else if(yPos>=576 && yPos<=625)
//Third Yard Decoration image
image = decor3;
}
//If click is within Game Pane
else if (xPos>75 && yPos<625){
//A tool has been selected
if(image!=null){
placedTool = this.image;
this.image = null;
placeable = true;
}
}
if(placeable && this.image==null){
GamePiece newImg = new GamePiece(placedTool, xPos, yPos);
gamePane.additions.add(newImg);
gamePane.repaint();
System.out.println("IMAGE PLACED @ " + xPos + ", " + yPos);
placeable = false;
}
System.out.println("CLICK: (" + xPos + "," + yPos +")");
}
The images were added over top of the background image, by the previously given paintComponent method. They are a little off position, but still visible.
only a comments
post an SSCCE short runnable, compilable
why there is
yPos = event.getY()-25;
whats logics representing aninteger at -25
store all
ImageIcons
in local variable, store thoseImageIcons
in any array orList
in the case that there is fixed logics instead of get coordinates and then loadimage = new ImageIcon(getClass().getResource(
painted inpaintComponent
, do not provide anyFileIO
at runtime
- create an grid of
JLabel
s laid byGridLayout
, addMouseListener
to everyJLabel
s, change anJLabel.setIcon(myImageIcon)
fromMouse Event
, rest of logics I'm missing without an SSCCE
for example
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class ChessBoard extends JFrame {
private JFrame frame = new JFrame();
private JPanel panel = new JPanel();
private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");
private Icon questnIcon = UIManager.getIcon("OptionPane.questionIcon");
private JButton button = new JButton("Reset my board");
public ChessBoard() {
panel.setLayout(new GridLayout(8, 8, 0, 0));
for (int i = 0; i < 64; i++) {
final JLabel label = new JLabel();
label.setIcon(errorIcon);
label.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
label.setIcon(infoIcon);
} else {
label.setIcon(warnIcon);
}
}
@Override
public void mouseEntered(MouseEvent me) {
label.setIcon(questnIcon);
}
});
panel.add(label);
}
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (Component c : panel.getComponents()) {
if (c instanceof JLabel) {
JLabel label = (JLabel) c;
label.setIcon(errorIcon);
}
}
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.add(button, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ChessBoard();
}
});
}
}
这篇关于在鼠标上添加图像单击JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!