Graphics2Dアニメーション、paintComponentがシェイプを1回だけ描画してから停止する理由を理解できません。また、タイマーカウンター変数がインクリメントされない理由も理解できません。私は1時間以上これに頭をぶつけてきました、誰かが私を正しい方向に動かすことができますか?
テトリス
package Tetris;
import javax.swing.JLabel;
import java.util.ArrayList;
public class Tetri {
private int xCoords= (int)(Math.random()*10);
private int yCoords=0;
private int shapeType;
private int orientation=0;
public Tetri(int shapeT){
shapeType = shapeT;
}
public int getOrient(){
return orientation;
}
public void setOrient(int orient){
orientation = orient;
}
public void setXCoords(int coords){
xCoords = coords;
}
public int getXCoords(){
return xCoords;
}
public void setYCoords(int coords){
yCoords = coords;
}
public int getYCoords(){
return yCoords;
}
public int getShape(){
return shapeType;
}
}
MovingRectangle
package Tetris;
import javax.swing.JFrame;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.Timer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
public class MovingRectangle {
public static void main(String[] args){
new MovingRectangle();
}
public MovingRectangle() {
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
JFrame frame = new JFrame("Tetris");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(0,1));
frame.add(new TestPane(Color.RED));
frame.setSize(1000,800);
frame.setVisible(true);
}
});
}
TestPane
public class TestPane extends JPanel {
private Graphics g0;
private int unit = 50;
private ArrayList<Tetri> shapeList = new ArrayList<Tetri>();
int timercount =1;
public TestPane(Color foreground){
setForeground(foreground);
this.setBackground(Color.BLUE);
Timer timer = new Timer(2000,new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
System.out.println("timercount :"+timercount);
timercount = timercount++;
shapeList.add(new Tetri((int)(Math.random()*2)));
System.out.println("shapeList size : "+shapeList.size());
repaint();
}
});
timer.start();
}
@Override
public void paintComponent(Graphics g){
g0 = g;
super.paintComponent(g);
if(shapeList.size()>0){
Tetri current = shapeList.get(0);
if(current.getShape()==0){
createShape0(current.getXCoords(),current.getYCoords(),current.getOrient());
}
if(current.getShape()==1){
createShape1(current.getXCoords(),current.getYCoords(),current.getOrient());
}
current.setYCoords(current.getYCoords()+50);
}
else{shapeList.add(new Tetri((int)(Math.random()*2)));}
}
public void createShape0(int xc,int yc,int orien){
int yPixel= yc*50;
int xPixel= xc*50;
Graphics2D g2d = (Graphics2D) g0.create();
g2d.setColor(Color.RED);
if(orien==0){
g2d.drawRect(xPixel, yPixel, unit*4, unit);
}
if(orien==1){
g2d.drawRect(xPixel, yPixel, unit, unit*4);
}
}
public void createShape1(int xc,int yc, int orien){
int yPixel= yc*50;
int xPixel= xc*50;
Graphics2D g2d = (Graphics2D) g0.create();
g2d.setColor(Color.GREEN);
if(orien==0){
g2d.drawRect(xPixel, yPixel, unit*3, unit);
g2d.drawRect(xPixel+50, yPixel+50,unit,unit);
}
if(orien==1){
g2d.drawRect(xPixel+50, yPixel-50, unit, unit*3);
g2d.drawRect(xPixel, yPixel,unit,unit);
}
if(orien==2){
g2d.drawRect(xPixel, yPixel, unit*3, unit);
g2d.drawRect(xPixel+50, yPixel-50,unit,unit);
}
if(orien==3){
g2d.drawRect(xPixel+50, yPixel-50, unit, unit*3);
g2d.drawRect(xPixel+100, yPixel,unit,unit);
}
}
}
}