0

我试图创建这个对象正在工作的图形对象 timer/timerTask。我可以为单个对象工作,但如果我创建新对象,它就不能按我想要的那样工作。我想像俄罗斯方块一样工作。

这是我的代码

那是我的第一级散文

import javax.swing.*;
import java.awt.*;
import java.util.Timer;  
import java.util.*;


public class prosek extends JPanel{
public static int boyut;  // size 
public static  int koordinatx; //coordx
public static  int koordinaty; //coordy
public static boolean tekrar; //repeat
public static int yukseklik;  //height
private Timer timer; 
private LinkedList<TimerTask> taskList=new LinkedList<TimerTask>();



public prosek(){
    yukseklik=140;
    tekrar=false;
    koordinatx=0;
    koordinaty=-20;
    boyut=20;
    startSampling();

}




@Override
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2d=(Graphics2D)g;
   g2d.setColor(Color.RED);

   g2d.fillRect(koordinatx, koordinaty, boyut+1, boyut+1);
   g2d.dispose();



}

public void moveRectangle()
{ 
    if(yukseklik<=140)
    {

     if(koordinaty<140) koordinaty=koordinaty+boyut;
       else 
       { 
           yukseklik-=20;
           stopSampling();
         }
     }
}


void startSampling(){
    TimerTask task=new TimerTask(){
        public void run() {
                moveRectangle();
                repaint();

            }
            };
        Timer timer=new Timer();
        timer.scheduleAtFixedRate(task,0, 500);
        taskList.add(task);

        }




void stopSampling(){
    if(taskList.isEmpty()){
        return;
    }
    taskList.removeFirst().cancel();

}

   }


     ------------
     That's my second class prosekt

     import javax.swing.*;
     import java.awt.*;



     public class prosekt extends JFrame {

     public prosekt(){

    Container container=getContentPane();
    container.setLayout(new GridLayout(0,1));
    container.add(new prosek());


    setSize(100,200);
    setVisible(true);
    add(container);


    }


    public static void main(String args[])
   {
prosekt p=new prosekt();

    }

    }
4

1 回答 1

0

我对您的代码进行了一些更改。

我做的第一件事是定义一个DropObject类。这使得拥有多个放置对象变得更容易。

这是该DropObject课程的代码。这是一个基本的 getter / setter 模型类。我使用 Rectangle 来保存放置对象的坐标和大小。

DropObject 类

import java.awt.Rectangle;

public class DropObject {

    private int         yukseklik;  // height
    private Rectangle   object;

    public DropObject(int yukseklik, int x, int y, int size) {
        this.yukseklik = yukseklik;
        this.object = new Rectangle(x, y, size, size);
    }

    public int getYukseklik() {
        return yukseklik;
    }

    public void setYukseklik(int yukseklik) {
        this.yukseklik = yukseklik;
    }

    public Rectangle getObject() {
        return object;
    }

    public void setObject(Rectangle object) {
        this.object = object;
    }

}

我修改了您的JPanel课程以使用该DropObject课程。我也大写Prosek了,因为 Java 类名应该以大写字母开头。

我在方法中添加了新对象moveRectangle,这样您就会看到不止一个对象掉落。添加新对象需要在其他地方进行。

散文类

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JPanel;

public class Prosek extends JPanel {
    private static final int        yukseklik   = 140;
    private DropObject              koordinat;
    private List<DropObject>        objects     = new LinkedList<DropObject>();
    private LinkedList<TimerTask>   taskList    = new LinkedList<TimerTask>();

    public Prosek() {
        koordinat = new DropObject(yukseklik, 0, -20, 20);
        objects.add(koordinat);
        startSampling();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.RED);
        for (DropObject o : objects) {
            Rectangle r = o.getObject();
            g2d.fillRect(r.x, r.y, r.width, r.height);
        }
    }

    public void moveRectangle() {
        for (int i = (objects.size() - 1); i >= 0; i--) {
            DropObject o = objects.get(i);
            Rectangle r = o.getObject();
            if (o.getYukseklik() <= 140) {
                if (r.y < 140) {
                    r.y += r.height;
                    o.setObject(r);
                } else {
                    o.setYukseklik(o.getYukseklik() - r.height);
                    // stopSampling();
                }
            }
            if ((r.y >= 70) && (r.y <= 80)) {
                objects.add(new DropObject(yukseklik, 0, -20, 20));
            }
        }
    }

    void startSampling() {
        TimerTask task = new TimerTask() {
            public void run() {
                moveRectangle();
                repaint();

            }
        };
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(task, 0, 500);
        taskList.add(task);

    }

    void stopSampling() {
        if (taskList.isEmpty()) {
            return;
        }
        taskList.removeFirst().cancel();

    }

}

我对您的主要课程进行了一些更改。我调用了该SwingUtilities invokeLater方法以确保您的 Swing 组件已在 Event Dispatch 线程上创建和使用。我使用 aJFrame而不是扩展JFrame. 只应在覆盖组件方法之一时扩展 Swing 组件。

组合优于继承

专业课

import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Prosekt implements Runnable {

    @Override
    public void run() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container container = frame.getContentPane();
        container.setLayout(new GridLayout(0, 1));
        container.add(new Prosek());

        frame.setSize(100, 200);
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Prosekt());
    }

}
于 2013-05-31T17:00:31.287 回答