这是一个未完成的俄罗斯方块程序
不知为什么不能运行,红字处的总是报错,不知道为什么呀???
说是无法从静态上下文中引用非静态变量this...不知道为什么呀,,请大虾们指教!谢谢了package elsfkgame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.util.Iterator;
import java.util.ArrayList;
import java.awt.geom.*;
/**
*
* @author Administrator
*/
public class elsfkGame{
public static void main(String[] args) {
// TODO code application logic here
youxiquanjuJFrame frame=new youxiquanjuJFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class threadRunnable implements Runnable{
GaojiJPanel gaojiJPanel=new GaojiJPanel();
public threadRunnable (fangkuaimove afangkuaimove ,Component aComponent){
fangkuaimove=afangkuaimove;
component=aComponent;
}
public void run(){
try{
for(int a2=0;a2<=15;a2++){
fangkuaimove.move();
component.repaint();
Thread.sleep(50);
}
}
catch(InterruptedException e)
{
}
}
private Component component;
private fangkuaimove fangkuaimove;
}
class GaojiJPanel extends JPanel {
public ArrayList huafangkuai=new ArrayList();
public ArrayList huafangkuai2=new ArrayList();
public ArrayList huituArrayList=new ArrayList();
public boolean[][] fangkuai=new boolean[15][15];
public void paintComponent(Graphics g){
super.paintComponent(g);
Iterator huafangkuaiIterator=huafangkuai.iterator();
Iterator huafangkuaiIterator2=huafangkuai2.iterator();
huiTu huatu,huatu2;
while(huafangkuaiIterator.hasNext()){
huatu=(huiTu)huafangkuaiIterator.next();
huatu.draw(g);
}
huafangkuai.clear();
while(huafangkuaiIterator2.hasNext()){
huatu2=(huiTu)huafangkuaiIterator2.next();
huatu2.draw(g);
}
huafangkuai2.clear();
}
}
class fangkuaimove {
GaojiJPanel gaojiJPanel2=new GaojiJPanel();
public void move()
{
for (int a2=0;a2<=15;a2++){
for(int b2=0;b2<=15;b2++){
if(gaojiJPanel2.fangkuai[a2][b2]==true){
gaojiJPanel2.fangkuai[a2][b2]=false;
gaojiJPanel2.fangkuai[a2][b2+1]=true;
}
}
}
for(int a=0;a<=15;a++){
for(int b=0;b<=15;b++){
if(gaojiJPanel2.fangkuai[a][b]==true){
huiTu fangkuai1=new huiTu(a*3,b*3,2,2,Color.YELLOW);
gaojiJPanel.huafangkuai.add(fangkuai1);
}
else
{
huiTu fangkuai2=new huiTu(a*3,b*3,2,2,Color.BLACK);
gaojiJPanel.huafangkuai2.add(fangkuai2);
}
}
}
}
}
/**
* @param args the command line arguments
*/
class huiTu extends Rectangle{ private Color fillColor;
public huiTu(int xValue,int yValue,int widthValue,int heightValue,Color colorValue){
super(xValue,yValue,widthValue,heightValue);
setFillColor(colorValue);
}
public void setFillColor(Color colorValue){
fillColor=colorValue;
}
public Color getFillColor(){
return fillColor;
}
public void draw(Graphics g){
g.setColor(fillColor);
g.fillRect(x,y,width,height);
}
}
class youxiquanjuJFrame extends JFrame{
public youxiquanjuJFrame(){
setTitle("俄罗斯方块");
setSize(WIDTH,HEIGHT);
gaojiJPanel=new GaojiJPanel();
add(gaojiJPanel);
JPanel buttonPanel=new JPanel();
addButton(buttonPanel,"START",
new ActionListener(){
public void actionPerformed(ActionEvent event){
startJButtonActionPerformed(event);
}
}
);
addButton(buttonPanel,"LEFT",
new ActionListener()
{
public void actionPerformed(ActionEvent event){
leftJButtonActionPerformed(event);
}
}
) ;
addButton(buttonPanel,"RIGHT",
new ActionListener()
{
public void actionPerformed(ActionEvent event){
rightJButtonActionPerformed(event);
}
}
);
addButton(buttonPanel,"STOP",
new ActionListener()
{
public void actionPerformed(ActionEvent event){
stopJButtonActionPerformed(event);
}
}
);
addButton(buttonPanel,"TURN",
new ActionListener()
{
public void actionPerformed(ActionEvent event){
turnJButtonActionPerformed(event);
}
});
}
public void addButton(Container c,String title,ActionListener listener){
JButton button=new JButton(title);
c.add(button);
button.addActionListener(listener);
}
public void startJButtonActionPerformed(ActionEvent event){
gaojiJPanel.fangkuai[0][10]=true;
gaojiJPanel.fangkuai[0][11]=true;
gaojiJPanel.fangkuai[0][12]=true;
gaojiJPanel.fangkuai[0][13]=true;
fangkuaimove b=new fangkuaimove();
Runnable r=new threadRunnable(b,gaojiJPanel);
Thread t=new Thread(r);
t.start();
}
public void stopJButtonActionPerformed(ActionEvent event){
}
public void leftJButtonActionPerformed(ActionEvent event){
GaojiJPanel gaojiJPanel2=new GaojiJPanel();
for (int a2=0;a2<=15;a2++){
for(int b2=0;b2<=15;b2++){
if(gaojiJPanel2.fangkuai[a2][b2]==true){
gaojiJPanel2.fangkuai[a2][b2]=false;
gaojiJPanel2.fangkuai[a2-1][b2]=true;
}
}
}
for(int a=0;a<=15;a++){
for(int b=0;b<=15;b++){
if(gaojiJPanel2.fangkuai[a][b]==true){
huiTu fangkuai1=new huiTu(a*3,b*3,2,2,Color.YELLOW);
gaojiJPanel.huafangkuai.add(fangkuai1);
}
else
{
huiTu fangkuai2=new huiTu(a*3,b*3,2,2,Color.BLACK);
gaojiJPanel.huafangkuai2.add(fangkuai2);
}
}
}
}
public void turnJButtonActionPerformed(ActionEvent event){
}
public void rightJButtonActionPerformed(ActionEvent event){
}
}
public JButton leftJButton;
public JButton rightJButton;
public JButton turnJButton;
public JButton stopJButton;
public JButton startJButton;
public GaojiJPanel gaojiJPanel;
public static final int WIDTH=450;
public static final int HEIGHT=250;
}
不知为什么不能运行,红字处的总是报错,不知道为什么呀???
说是无法从静态上下文中引用非静态变量this...不知道为什么呀,,请大虾们指教!谢谢了package elsfkgame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.util.Iterator;
import java.util.ArrayList;
import java.awt.geom.*;
/**
*
* @author Administrator
*/
public class elsfkGame{
public static void main(String[] args) {
// TODO code application logic here
youxiquanjuJFrame frame=new youxiquanjuJFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class threadRunnable implements Runnable{
GaojiJPanel gaojiJPanel=new GaojiJPanel();
public threadRunnable (fangkuaimove afangkuaimove ,Component aComponent){
fangkuaimove=afangkuaimove;
component=aComponent;
}
public void run(){
try{
for(int a2=0;a2<=15;a2++){
fangkuaimove.move();
component.repaint();
Thread.sleep(50);
}
}
catch(InterruptedException e)
{
}
}
private Component component;
private fangkuaimove fangkuaimove;
}
class GaojiJPanel extends JPanel {
public ArrayList huafangkuai=new ArrayList();
public ArrayList huafangkuai2=new ArrayList();
public ArrayList huituArrayList=new ArrayList();
public boolean[][] fangkuai=new boolean[15][15];
public void paintComponent(Graphics g){
super.paintComponent(g);
Iterator huafangkuaiIterator=huafangkuai.iterator();
Iterator huafangkuaiIterator2=huafangkuai2.iterator();
huiTu huatu,huatu2;
while(huafangkuaiIterator.hasNext()){
huatu=(huiTu)huafangkuaiIterator.next();
huatu.draw(g);
}
huafangkuai.clear();
while(huafangkuaiIterator2.hasNext()){
huatu2=(huiTu)huafangkuaiIterator2.next();
huatu2.draw(g);
}
huafangkuai2.clear();
}
}
class fangkuaimove {
GaojiJPanel gaojiJPanel2=new GaojiJPanel();
public void move()
{
for (int a2=0;a2<=15;a2++){
for(int b2=0;b2<=15;b2++){
if(gaojiJPanel2.fangkuai[a2][b2]==true){
gaojiJPanel2.fangkuai[a2][b2]=false;
gaojiJPanel2.fangkuai[a2][b2+1]=true;
}
}
}
for(int a=0;a<=15;a++){
for(int b=0;b<=15;b++){
if(gaojiJPanel2.fangkuai[a][b]==true){
huiTu fangkuai1=new huiTu(a*3,b*3,2,2,Color.YELLOW);
gaojiJPanel.huafangkuai.add(fangkuai1);
}
else
{
huiTu fangkuai2=new huiTu(a*3,b*3,2,2,Color.BLACK);
gaojiJPanel.huafangkuai2.add(fangkuai2);
}
}
}
}
}
/**
* @param args the command line arguments
*/
class huiTu extends Rectangle{ private Color fillColor;
public huiTu(int xValue,int yValue,int widthValue,int heightValue,Color colorValue){
super(xValue,yValue,widthValue,heightValue);
setFillColor(colorValue);
}
public void setFillColor(Color colorValue){
fillColor=colorValue;
}
public Color getFillColor(){
return fillColor;
}
public void draw(Graphics g){
g.setColor(fillColor);
g.fillRect(x,y,width,height);
}
}
class youxiquanjuJFrame extends JFrame{
public youxiquanjuJFrame(){
setTitle("俄罗斯方块");
setSize(WIDTH,HEIGHT);
gaojiJPanel=new GaojiJPanel();
add(gaojiJPanel);
JPanel buttonPanel=new JPanel();
addButton(buttonPanel,"START",
new ActionListener(){
public void actionPerformed(ActionEvent event){
startJButtonActionPerformed(event);
}
}
);
addButton(buttonPanel,"LEFT",
new ActionListener()
{
public void actionPerformed(ActionEvent event){
leftJButtonActionPerformed(event);
}
}
) ;
addButton(buttonPanel,"RIGHT",
new ActionListener()
{
public void actionPerformed(ActionEvent event){
rightJButtonActionPerformed(event);
}
}
);
addButton(buttonPanel,"STOP",
new ActionListener()
{
public void actionPerformed(ActionEvent event){
stopJButtonActionPerformed(event);
}
}
);
addButton(buttonPanel,"TURN",
new ActionListener()
{
public void actionPerformed(ActionEvent event){
turnJButtonActionPerformed(event);
}
});
}
public void addButton(Container c,String title,ActionListener listener){
JButton button=new JButton(title);
c.add(button);
button.addActionListener(listener);
}
public void startJButtonActionPerformed(ActionEvent event){
gaojiJPanel.fangkuai[0][10]=true;
gaojiJPanel.fangkuai[0][11]=true;
gaojiJPanel.fangkuai[0][12]=true;
gaojiJPanel.fangkuai[0][13]=true;
fangkuaimove b=new fangkuaimove();
Runnable r=new threadRunnable(b,gaojiJPanel);
Thread t=new Thread(r);
t.start();
}
public void stopJButtonActionPerformed(ActionEvent event){
}
public void leftJButtonActionPerformed(ActionEvent event){
GaojiJPanel gaojiJPanel2=new GaojiJPanel();
for (int a2=0;a2<=15;a2++){
for(int b2=0;b2<=15;b2++){
if(gaojiJPanel2.fangkuai[a2][b2]==true){
gaojiJPanel2.fangkuai[a2][b2]=false;
gaojiJPanel2.fangkuai[a2-1][b2]=true;
}
}
}
for(int a=0;a<=15;a++){
for(int b=0;b<=15;b++){
if(gaojiJPanel2.fangkuai[a][b]==true){
huiTu fangkuai1=new huiTu(a*3,b*3,2,2,Color.YELLOW);
gaojiJPanel.huafangkuai.add(fangkuai1);
}
else
{
huiTu fangkuai2=new huiTu(a*3,b*3,2,2,Color.BLACK);
gaojiJPanel.huafangkuai2.add(fangkuai2);
}
}
}
}
public void turnJButtonActionPerformed(ActionEvent event){
}
public void rightJButtonActionPerformed(ActionEvent event){
}
}
public JButton leftJButton;
public JButton rightJButton;
public JButton turnJButton;
public JButton stopJButton;
public JButton startJButton;
public GaojiJPanel gaojiJPanel;
public static final int WIDTH=450;
public static final int HEIGHT=250;
}
解决方案 »
- Java远程桌面调用失败
- 如何共享无线上网?
- mybatis oracle 分页
- java里简单的数字帮忙解释下0xFFFFFFF1
- 为什么静态方法中不能用this和super关键字
- 【紧急求助】是否能判断字符串中的某个字符是汉字?
- 关于两个类的问题?
- ★★可否用java代码自动生成UML图和项目文档(javadoc除外)? 用什么工具好?★★
- 各位兄弟姐妹们,小弟给各位先鞠个躬!谁有Oracle Jdbc Drivers,给我发一份可以吗?
- compare(Student s1, Student s2) 这里为什么要传两个值啊?求大神详解。
- ibatis 中刷新缓存的问题
- 如何请求OS 管理员身份运行程序?
this.new youxiquanjuJFrame();
这里使用了this,我们知道静态函数里是不能使用this的。
可以改为
youxiquanjuJFrame frame=new elsfkGame().new youxiquanjuJFrame();