Graphics2D动画,无法理解为什么paintComponent只绘制一次形状然后停止。我也无法理解为什么我的计时器计数器变量没有递增。我一直在敲打我的脑袋一个多小时,有人能把我推向正确的方向吗?
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;
}
}
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);
}
});
}
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);
}
}
}
}
答案 0 :(得分:1)
我有一系列的东西,但让我们从最明显的开始。
你&#34;运动&#34;计算结束了。
int yPixel = yc * 50;
int xPixel = xc * 50;
这导致你的形状跳跃&#34;跳跃&#34;期望更远的距离...实际上,我找不到任何理由让你去修改&#34;这些价值观。只需将它们读作......
int yPixel = yc;
int xPixel = xc;
让形状移动得很好......
代码审核
Graphics2D g2d = (Graphics2D) g.create();
),那么当您完成它时,您有责任处置它(g2d.dispose()
)。否则你将慢慢消耗系统资源,直到你的应用程序崩溃(从经验谈起 - 它是一个难以找到的bug;)。)你似乎正在艰难地处理这个过程。您应该根据知道如何&#34;绘制&#34;的Shape
为每个Tetri
创建一个类。本身。这样你就可以在绘制循环中简单地将图形上下文传递给它,而不关心。这使得随着时间的推移更容易添加新作品。
如果你将每个部分都放在java.awt.Shape
之外,那么转换形状并旋转它们会变得非常容易。