我遇到的问题是当我移动我的“碎片”时,其中一些失灵(我的意思是他们的x位置搞砸了)我无法找出原因。我已经尝试调整该块的大小,尝试更改Piece类中的fill方法,并尝试调整JFrame的大小。我将提供一张之前,期间和之后的图片(抱歉,我还无法嵌入图片)和所有代码。
Before,During,After。正如你所看到的那样,“碎片”的x位置是关闭的。我能看到的主要问题是:通过paintComponent方法中的Piece对象的ArrayList更新,我在其fill方法中移动Piece对象的方式这部分我尝试过更改 - &gt; (如果(x <-100)),actionTimer延迟2ms,我也尝试改变无效,或者我创建和添加“碎片”的方式。我还会提供我的所有代码here以防有可能帮助(并且您可以自己运行)。
我的轮盘小组类:(我正在删除一些不重要的代码)。
public class RoulettePanel extends JPanel implements ActionListener {
public ArrayList<Piece> pieces = new ArrayList<Piece>();
private Timer actionTimer, rollerTimer;
private int yValue = 70;
private int actionCount = 0;
private int rollerCount = 0;
private int waitSeconds = 5;
public RoulettePanel() {
start = new JButton("Start");
start.addActionListener(this);
add(start);
displayField = new JTextField(20);
displayField.setEditable(false);
add(displayField);
addPieces();
rollerTimer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
displayField.setText("Rolling in " + (waitSeconds-rollerCount) + "...");
if(rollerCount==waitSeconds) {
displayField.setText("Rolling...");
rollerTimer.stop();
playRound();
}
rollerCount++;
}
});
actionTimer = new Timer(2, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
actionCount++;
int velocity = 1;
if(actionCount < 400) {
velocity = 3;
} else if(actionCount < 1600) {
velocity = 2;
}
for(int i=0; i<pieces.size(); i++) {
pieces.get(i).move(-1*velocity);
}
if(actionCount == 1900) {
actionTimer.stop();
determineWinner();
}
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Color temp = g.getColor();
for(int i=0; i<pieces.size(); i++) {
pieces.get(i).fill(g);
}
g.setColor(Color.WHITE);
g.fillRect(getWidth()/2, yValue-10, 2, 50);
g.setColor(temp);
repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source.equals(start)) {
rollerCount=0;
rollerTimer.start();
} else if(source.equals(stop)) {
actionTimer.stop();
determineWinner();
}
}
private void playRound() {
actionCount=0;
actionCount -= rand.nextInt(601);
actionTimer.start();
}
private void determineWinner() {
for(int i=0; i<pieces.size(); i++) {
if(pieces.get(i).containsPoint(getWidth()/2)) {
displayField.setText(pieces.get(i).toString());
break;
}
}
}
private void addPieces() {
int width = Piece.width;
System.out.println(width);
System.out.println(Main.GUI.getWidth()/15);
pieces.add(new Piece(0, yValue, Color.GREEN));
pieces.add(new Piece(width, yValue, Color.BLACK));
pieces.add(new Piece(width*2, yValue, Color.RED));
pieces.add(new Piece(width*3, yValue, Color.BLACK));
pieces.add(new Piece(width*4, yValue, Color.RED));
pieces.add(new Piece(width*5, yValue, Color.BLACK));
pieces.add(new Piece(width*6, yValue, Color.RED));
pieces.add(new Piece(width*7, yValue, Color.BLACK));
pieces.add(new Piece(width*8, yValue, Color.RED));
pieces.add(new Piece(width*9, yValue, Color.BLACK));
pieces.add(new Piece(width*10, yValue, Color.RED));
pieces.add(new Piece(width*11, yValue, Color.BLACK));
pieces.add(new Piece(width*12, yValue, Color.RED));
pieces.add(new Piece(width*13, yValue, Color.BLACK));
pieces.add(new Piece(width*14, yValue, Color.RED));
}}
我的作品类:
public class Piece {
public int x;
public int y;
public static int width = Main.GUI.getWidth()/15+6;
public Color color;
public Piece(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}
public void fill(Graphics g) {
Color temp = g.getColor();
g.setColor(color);
if(x<-100) {
x=Main.roulettePanel.getWidth();
}
g.fillRect(x, y, width, 30);
g.setColor(temp);
}
public void move(int xAmount) {
x += xAmount;
}
public boolean containsPoint(int x) {
if(x>=this.x && x<=this.x+width) {
return true;
}
return false;
}
public String toString() {
if(color.equals(Color.BLACK)) {
return "Black";
} else if(color.equals(Color.RED)) {
return "Red";
} else if(color.equals(Color.GREEN)) {
return "Green";
}
return null;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}}
我的主要课程:
public class Main {
public static JFrame GUI;
public static RoulettePanel roulettePanel;
public static Container pane;
public static void main(String[] args) {
GUI = new JFrame("Roulette");
GUI.setSize(1005, 800);
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane = GUI.getContentPane();
roulettePanel = new RoulettePanel();
pane.add(roulettePanel);
GUI.setVisible(true);
}}