-1
我在我的x
和y
它們是其中i點擊我的鼠標的座標值加上使用隊列。但是,當我嘗試使用Iterator
訪問值時出現錯誤。有人能幫助我嗎?我如何以正確的方式做到這一點?另外,我不知道Timer
和Actionlistener
對我的代碼有什麼作用(它們是否需要或者我必須刪除它?),所以稍加說明對我來說就非常有用。我希望你能幫我修復和改進這些代碼。謝謝! :)如何正確使用Java中的迭代器從隊列中獲取價值?
public class Canvas extends JPanel implements MouseListener, ActionListener{
Timer time;
int numberOfPoints;
Queue<Integer> x = new LinkedList<Integer>();
Queue<Integer> y = new LinkedList<Integer>();
Iterator itX = x.iterator();
Iterator itY = y.iterator();
public Canvas(){
addMouseListener(this);
time = new Timer(1, this);
time.start();
numberOfPoints = 5;
}
public void paint (Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setBackground(Color.black);
g2d.drawString("Left: "+ numberOfPoints, 10, 20);
g2d.fillOval(220, 170, 30, 30);
g2d.setColor(Color.red);
while(itX.hasNext()){
Integer xx = (Integer) itX.next();
Integer yy = (Integer) itY.next();
g2d.fillOval(xx, yy, 30, 30);
}
}
public void mousePressed(MouseEvent e) {
if(numberOfPoints>0){
numberOfPoints--;
x.add(e.getX()-15);
y.add(e.getY()-15);
repaint();
}
}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
@Override
public void actionPerformed(ActionEvent arg0) {
repaint();
}
}
總之,這裏的的情況下,要運行它在我的主類。
public class ClikkPanikk extends JFrame{
public ClikkPanikk(){
short width = 500;
short height = 400;
add(new Canvas());
setTitle("ClikkPanikk");
setVisible(true);
setSize(width, height);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ClikkPanikk();
}
}
救了我。謝啦! :) –