gpt4 book ai didi

java - 更改 JFrame 上图像的位置

转载 作者:行者123 更新时间:2023-12-02 05:13:29 24 4
gpt4 key购买 nike

所以我正在制作这个ABC学习游戏,我想做的是如果我多次点击A按钮,三个图像就会改变它们的位置,我想做的是这个![在此处输入图像描述][1]

当我点击A按钮时,屏幕上会出现三个图像,第一个是苹果,因为我在循环中这样设置,但后两个图像会随机出现,尽管有时其中一个又是苹果,我可以解决这个问题。

我的问题是,如果多次单击“A”按钮,如何将苹果的位置更改为第二张图像,将第二张图像更改为第一张图像,将第三张图像更改为第二个位置。所以,结果将是苹果将根据单击“A”按钮改变位置,而其他两张图片改变其位置并从数组中随机选择。

所以,这是我的 JPanel 代码,一切都在其中发生。大部分代码都在注释中进行了解释

import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.event.*;
import java.text.AttributedCharacterIterator;
import java.util.Random;
import javax.swing.ImageIcon;

/**
*
* @author Dip
*/
public class AbcGeniusPanel extends JPanel implements ActionListener {

//Declare the necessary Variables here
private JButton[] buttons; //create an array for buttons
private BorderLayout layout; //Declare object of BorderLayout
private Image image = null;
private boolean showImage = false;
//Initialize all the variables here
static int index = 0;
int randNumber = 0, id = 0;
int q = 0, w = 0;
int buttonClick = 0;
//Store all the imahges that will appear on the screen into an String type array
private static String[] imageList = {"src/Images/1.png", "src/Images/2.png", "src/Images/3.png", "src/Images/4.png", "src/Images/5.png", "src/Images/6.png", "src/Images/7.png", "src/Images/8.png", "src/Images/9.png", "src/Images /10.png",
"src/Images/11.png", "src/Images/12.png", "src/Images/13.png", "src/Images /14.png", "src/Images/15.png",
"src/Images/16.png", "src/Images/17.png", "src/Images/18.png", "src/Images /19.png", "src/Images/20.png",
"src/Images/21.png", "src/Images/22.png", "src/Images/23.png", "src/Images /24.png", "src/Images/25.png",
"src/Images/26.png"
};

//Define the constructor here
public AbcGeniusPanel() {
ImageIcon[] alphabets = new ImageIcon[26];
setBackground(Color.yellow);
//Load the images for alphabet images into the alphabets array using a for loop
for (int i = 0; i < alphabets.length; i++) {
alphabets[i] = new ImageIcon("C:\\Users\\Dip\\Desktop\\Java Projects\\AbcGeniusApp\\src\\Alphabets\\" + (i + 1) + ".png");
}
//Create a JPnael object
JPanel panel = new JPanel();
//Set a layoutManager on the panel
//panel.setLayout(new FlowLayout(FlowLayout.CENTER)); //This is not workling good
panel.setLayout(new GridLayout(2, 13, 5, 5)); //This is good for now
//Create an array for holdoing the buttons
buttons = new JButton[26];
//This Loop will Store the buttons in the buttons array attatching each image for each button
//Try passing Images inside the JButton parameter later.
for (int i = 0; i < 26; i++) {
buttons[i] = new JButton(alphabets[i]);
}
// Now Setting up a new Borderlayout so that we can set the whole gridLayout at the botton of the panel
setLayout(new BorderLayout(2, 0));
//add the panel to the Border layout
add(panel, BorderLayout.SOUTH);
//Add evenHandling mechanism to all the buttons
for (int k = 0; k < 26; k++) {
buttons[k].addActionListener(this);
}
for (int count1 = 0; count1 < 26; count1++) {
panel.add(buttons[count1]);
}
}

//This Method will generate a random Number and return it
public int random_number() {
int rand_num;
Random generator = new Random(System.currentTimeMillis());
rand_num = generator.nextInt(26);
return rand_num;
}

//This method will draw the font on the Panel
public void paintComponent(Graphics g) {
Font font; //Declare Font object here
font = new Font("Wide Latin", Font.BOLD, 22); //Set font
super.paintComponent(g); //Ensure the drawing in super class
g.setFont(font); //Set the font
g.setColor(Color.RED);
String text = "CLICK ON THE RIGHT IMAGE!"; //Display the text
g.drawString(text, 255, 20);
}

//To draw the picture on the screen we need to override the paint Method
@Override
public void paint(Graphics g) {
super.paint(g);
//Here, x and y will determine the x and y position os each image
int x = 0, y = 0;
// the varibale q is declared above
for (q = 0; q < 3; q++) //This loop will generate three images on the screen
{
if (showImage) {
x = x + 265; //X-Position of the image
y = 90; //Y-Position of the image
//q is declared as q=0, so this will always be true
if (w == 1 || q == 0) {
g.drawImage(image, x, y, image.getWidth(null), image.getHeight(null), null); //This method will put the image on the screen
showImage = true;
w = 0;
}
while (true) //this loop will run anyway
{
//go inside this loop only when the generated random
//doesn't match with the index of the button that was pressed
while ((randNumber = random_number()) != index) {
index = randNumber; //Now put the randomVlaue in the index
this.image = new ImageIcon(imageList[randNumber]).getImage();
showImage = true;
//make w=1 so that we can break from the outer loop
w = 1;
//break from the inner loop
break;
}
//Since we have made the w=1, so we are breaking out of the outer loop
if (w == 1) {
break;
}
}
}
}
}

@Override
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
id = 0;
while (true) {
//id is set to zero, for example if the button A (buttons[0])is not pressed then it will go below
//to increase id until it matches the index of the button that we pressed
if (source == buttons[id]) {
//get the image of that same index of the buttons and then set the showImage true
//SO the the paint function above can draw the image
this.image = new ImageIcon(imageList[id]).getImage();
showImage = true;
//save the index of the button that is presed in another variable
//then break from the while loop
index = id;
break;
} else {
id++;
//This is necessary to make sure that id will cross 26
//becasue we have only 26 letters or the array index is 26
//so highest value can be 26 only
id = id % 26;
}
}
repaint();
}
}

最佳答案

  1. 将 3 个 JLabels 或 JButton(无论将显示图像的内容)添加到 JPanel 容器中。 JPanel 可能会使用 GridLayout(1, 3,horizo​​ntal_gap, 0) 布局。
  2. 将所有图像作为 ImageIcons 放入 ArrayList。
  3. 在需要时对 ArrayList 进行随机排列
  4. 随机排列后,使用 setIcon(...) 方法将图标放入 for 循环中的 JLabels/JButton 中。

请注意

  • 您的 JPanel 应覆盖 paintComponent,而不是 paintpaint 方法负责绘制组件的子组件和边框,默认情况下它不使用双缓冲,这使得重写方法更加危险。
  • 在 Swing GUI 中放入 while (true) 循环而不考虑线程是极其危险的。
  • 将其放入诸如 paint 之类的绘画方法中是 GUI 自杀。永远不要这样做,因为绘画方法是程序感知响应能力的主要决定因素。如果你放慢速度,程序将被认为是缓慢且响应差的,因此它必须尽可能精简和快速,并且你应该在其中包含绘画并且仅绘画代码。<
  • 您的paint方法中有程序逻辑,这也是不应该做的。您无法完全控制是否调用绘画方法,甚至是否调用绘画方法,因此程序逻辑永远不应该放置在其中之一中。
  • 正如 MadProgrammer 所指出的,不要使用图像的 src 路径,因为一旦将程序构建到 jar 文件中,该路径将不存在。最好在 jar 文件中创建一个资源目录,并将图像作为资源而不是文件引用。

关于java - 更改 JFrame 上图像的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27161375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com