我会避免将数据与代码混合,换句话说,避免“硬编码”你的数据,这是你想要做的。相反,我会,...
- 创建一个新类,该类包含一个 Trick,它的 Pace,无论它是否带轭。
- 您可能会在程序模型中拥有此类对象的集合
- 使用此类的当前对象的状态来确定单个 ActionListener 的行为方式。请注意,ActionListener 可以根据对象的状态调用不同的方法。
编辑 您的评论/我的回复
我实际上已经有一个包含所有这些信息的类,称为 TrickSettings。我是否应该让这个类也包含关于它显示了多少图像/如果它完成的信息?在上面的代码中,trickSettings 是一个 TrickSettings 数组。
是的。它需要做的就是返回该size()
技巧的图像集合。
我对您的第三点的担忧是,我不希望我的 ActionListener 以相同的时间触发所有技巧。
所以让计时器使用它从它正在迭代的当前 TrickSettings 对象中获得的时间。没什么大不了。
例如,节奏为“Yoked Pace”的技巧从文件中读取时间,或者更不同的是节奏为“Self Pace”的技巧,用户单击以推进它们。我以前使用 MouseAdapter 类代替了这些...
这可以很容易地通过程序的逻辑来适应。
编辑
很高兴你的工作正常。既然您有解决方案,我现在将发布我的解决方案,这是我昨天创建的一个来测试我的想法。也许它可以给你一些想法:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
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 java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.swing.*;
public class TimerSpeedEg extends JPanel {
private static final int BI_W = 150;
private static final int BI_H = BI_W;
private static final int SHOW_TRICKS_COUNT = 3;
private List<Icon> icons;
private Random random = new Random();
private List<ShowTricks> showTricksList = new ArrayList<>();
public TimerSpeedEg() {
icons = createIcons();
setLayout(new GridLayout(1, 0));
add(createShowTricksPanel(BI_W, BI_H, "Fast", 100, false));
add(createShowTricksPanel(BI_W, BI_H, "Slow", 1000, false));
add(createShowTricksPanel(BI_W, BI_H, "Self-Paced", 0, true));
}
private JPanel createShowTricksPanel(int w, int h, String title,
int speed, boolean selfPaced) {
final ShowTricks showTricks = new ShowTricks(w, h);
TrickSettings trickSettings = new TrickSettings(speed, selfPaced);
for (Icon icon : icons) {
trickSettings.addIcon(icon);
}
showTricks.setTrickSettings(trickSettings );
JButton showButton = new JButton(new AbstractAction("Show") {
@Override
public void actionPerformed(ActionEvent arg0) {
showTricks.show();
}
});
JPanel btnPanel = new JPanel();
btnPanel.add(showButton);
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createTitledBorder(title));
panel.add(showTricks.getTrickLabel(), BorderLayout.CENTER);
panel.add(btnPanel, BorderLayout.SOUTH);
return panel;
}
private List<Icon> createIcons() {
List<Icon> iconList = new ArrayList<Icon>();
String[] strings = { "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten" };
for (int i = 0; i < strings.length; i++) {
JLabel label = new JLabel(strings[i], SwingConstants.CENTER);
label.setSize(BI_W, BI_H);
label.setOpaque(true);
Color color = new Color(random.nextInt(127) + 127,
random.nextInt(127) + 127, random.nextInt(127) + 127);
Color borderColor = new Color(color.getRed() - 100,
color.getGreen() - 100, color.getBlue() - 100);
label.setBackground(color);
label.setBorder(BorderFactory.createLineBorder(borderColor, 20));
BufferedImage bImg = new BufferedImage(BI_W, BI_H,
BufferedImage.TYPE_INT_ARGB);
Graphics g = bImg.getGraphics();
label.paint(g);
g.dispose();
iconList.add(new ImageIcon(bImg));
}
return iconList;
}
private static void createAndShowGui() {
TimerSpeedEg mainPanel = new TimerSpeedEg();
JFrame frame = new JFrame("TimerSpeedEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class ShowTricks {
int prefW;
int prefH;
private JLabel trickLabel = new JLabel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(prefW, prefH);
}
};
public ShowTricks(int prefW, int prefH) {
this.prefW = prefW;
this.prefH = prefH;
}
private TrickSettings trickSettings;
public JLabel getTrickLabel() {
return trickLabel;
}
public void setTrickSettings(TrickSettings trickSettings) {
this.trickSettings = trickSettings;
}
public void show() {
if (trickSettings == null) {
return;
}
if (trickSettings.isSelfPaced()) {
showSelfPaced();
} else {
showWithTimer();
}
}
private void showWithTimer() {
Timer timer = new Timer(trickSettings.getMiliseconds(), new TimerListener());
timer.setInitialDelay(0);
timer.start();
}
private void showSelfPaced() {
trickLabel.setIcon(trickSettings.getIcon(0));
trickLabel.addMouseListener(new MouseAdapter() {
private int count = 1;
@Override
public void mousePressed(MouseEvent e) {
if (count < trickSettings.getSize()) {
trickLabel.setIcon(trickSettings.getIcon(count));
count++;
} else {
trickLabel.removeMouseListener(this);
}
}
});
}
private class TimerListener implements ActionListener {
private int count = 0;
@Override
public void actionPerformed(ActionEvent evt) {
if (count < trickSettings.getSize()) {
trickLabel.setIcon(trickSettings.getIcon(count));
count++;
} else {
((Timer) evt.getSource()).stop();
}
}
}
}
class TrickSettings implements Iterable<Icon> {
private List<Icon> icons = new ArrayList<Icon>();
private int miliseconds;
private boolean selfPaced;
public TrickSettings(int miliseconds, boolean selfPaced) {
this.miliseconds = miliseconds;
this.selfPaced = selfPaced;
}
public int getSize() {
return icons.size();
}
public int getMiliseconds() {
return miliseconds;
}
public boolean isSelfPaced() {
return selfPaced;
}
@Override
public Iterator<Icon> iterator() {
return icons.iterator();
}
public Icon getIcon(int index) {
return icons.get(index);
}
public void addIcon(Icon icon) {
icons.add(icon);
}
}