我正在使用这个类使图像具有不同的状态。首先,图像是深灰色(所以你看不到它)。我希望图像可以点击,一旦点击,就会出现一个数学公式。一旦方程得到回答,就会出现完整的图像。另外,我有另一个类将图像分成不同的部分。所以基本上所有这些单独的部分都继承了这个方法。所以它是一张被分成单独图像的图片(比方说4张图片,这意味着4个数学问题)。我该怎么做呢我已经尝试了多种方法(我没有将其包括在内,因为它看起来像是一只老鼠窝),但却无法让它发挥作用。我已尝试实施mouseListener
和OverlayLayout
,但尚未将其付诸实施。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
* Makes the image have different states
*/
public class GPanel extends JPanel {
private Image img;
private boolean answered;
private boolean working;
private int w;
private int h;
/**
*
*/
private static final long serialVersionUID = 1L;
public GPanel(Image img) {
this.img = img;
w = img.getWidth(this);
h = img.getHeight(this);
setPreferredSize(new Dimension(w, h));
answered = false;
working = false;
}
public void setAnswered() {
answered = true;
}
public boolean getAnswered() {
return answered;
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
if (!answered) {
if (working){
System.out.println("Run third");
g2.setColor(Color.lightGray);
g2.fillRect(0, 0, w, h);
g2.setPaint(Color.yellow);
g2.setFont(new Font("Sans-serif", Font.BOLD, 20));
g2.drawString("Testing, one, two, three.", w/3, h/2);
((JFrame)SwingUtilities.getRoot(this)).setTitle("Testing, one, two, three.");
}
else{
System.out.println("Run first");
g2.setColor(Color.darkGray);
g2.fillRect(0, 0, w, h);
}
working = !working; // toggles on and off
} else {
System.out.println("Run second");
g2.drawImage(img, 0, 0, this);
((JFrame)SwingUtilities.getRoot(this)).setTitle("Testing GPanel");
}
answered = !answered; // toggles on and off
}
}
答案 0 :(得分:1)
如果你需要做的就是交换图像,那么最简单的方法就是在JLabel中将图像显示为ImageIcons。要交换JLabel的图标,所有人只需在JLabel上调用setIcon(newIcon)
即可。例如,假设我们在一个ArrayList of Icons(ArrayList<Icon>
)中有四个图像,并且这个ArrayList变量被称为图标,并且说你的程序有一个名为iconIndex的int变量,它保存当前图像的索引号。显示,然后通过递增索引变量并使用它来获取列表中的下一个图标来交换图像很简单。类似的东西:
iconIndex++; // increment the index variable
iconIndex %= icons.size(); // if larger than size of list, set to 0
Icon icon = icons.get(iconIndex); // get icon from list
imageLabel.setIcon(icon); // set the JLabel's icon with it
此代码可以在MouseListener中,一个添加到JLabel,然后就完成了。请注意,逻辑代码位于MouseListener中。因此,如果用户在切换图像之前必须执行任何其他操作,则会在同一MouseListener
中进行检查例如:
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
@SuppressWarnings("serial")
public class DifferentImages extends JPanel {
private static final int IMG_W = 400;
private static final int IMG_H = IMG_W;
private static final Font TEXT_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 40);
private JLabel imageLabel = new JLabel();
private List<Icon> icons = new ArrayList<>();
private int iconIndex = 0;
public DifferentImages() {
// create images and icons
Icon icon = getIcon(Color.DARK_GRAY, Color.LIGHT_GRAY, "First Image");
icons.add(icon);
icon = getIcon(Color.BLUE, new Color(137, 207, 240), "Second Image");
icons.add(icon);
icon = getIcon(Color.RED, Color.PINK, "Third Image");
icons.add(icon);
icon = getIcon(Color.YELLOW, Color.ORANGE, "Fourth Image");
icons.add(icon);
imageLabel.setIcon(icons.get(iconIndex));
add(imageLabel);
imageLabel.addMouseListener(new MyMouse());
}
// MouseListener that is added to JLabel
private class MyMouse extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
// code here to check if it is OK to swap images
// and if so, then swap them:
iconIndex++; // increment the index variable
iconIndex %= icons.size(); // if larger than size of list, set to 0
Icon icon = icons.get(iconIndex); // get icon from list
imageLabel.setIcon(icon); // set the JLabel's icon with it
}
}
// just creates an image icon for demo purposes, one with color and text
private Icon getIcon(Color bg, Color fb, String text) {
BufferedImage img = new BufferedImage(IMG_W, IMG_H, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
g2.setColor(bg); // first a dark image
g2.fillRect(0, 0, IMG_W, IMG_H);
g2.setColor(fb);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setFont(TEXT_FONT);
FontMetrics fontMetrics = g2.getFontMetrics(TEXT_FONT);
int textWidth = fontMetrics.stringWidth(text);
int x = (IMG_W - textWidth) / 2;
int y = (IMG_H) / 2;
g2.drawString(text, x, y);
g2.dispose();
Icon icon = new ImageIcon(img);
return icon;
}
private static void createAndShowGui() {
JFrame frame = new JFrame("DifferentImages");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new DifferentImages());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}