从同一个班级射击多发子弹

时间:2014-12-31 01:34:24

标签: java android swing

我创建的以下代码片段中有一个射击子弹的矩形。我希望每当有新的子弹时召唤子弹类#34;召唤"在空格键旁边,它独立于最后一颗子弹。什么是一次在屏幕上创建多个子弹实例的第一步?

drawingComponent类:

package scratch;


import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;
import javax.swing.Timer;




import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.*;

public class drawingComponent extends JComponent implements KeyListener {
    private Timer timer;
    public int count = 0;
    public Rectangle hello = new Rectangle(100, 300, 50, 50);
    Integer x1 = hello.x;
    Integer y1 = hello.y;
    public Bullet bullet = new Bullet(hello.x, hello.y);

    boolean fired = false;




    public drawingComponent() {
        addKeyListener(this);



    }


    public class TimerListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {


            count++;



            bullet.fired();
            repaint();

            System.out.println(count + "seconds.");
        }

    }


    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (fired == true) {

            bullet.drawBullet(g);
        }
        Graphics2D g2 = (Graphics2D) g;

        g2.setColor(new Color(255, 25, 0));
        g2.setFont(new Font("monospace", Font.BOLD + Font.ITALIC, 30));
        g2.drawString("nothing yet", 300, 320);
        g2.fill(hello);



        setFocusable(true);
        requestFocus();

    }






    @Override
    public void keyPressed(KeyEvent e) {

        if (e.getKeyCode() == KeyEvent.VK_W) {

            hello.y = hello.y - 1;
            hello.setLocation(hello.x, hello.y);

            repaint();
            System.out.println(hello.y);
        }
        if (e.getKeyCode() == KeyEvent.VK_S) {
            hello.y = hello.y + 1;
            hello.setLocation(hello.x, hello.y);
            repaint();

        }
        if (e.getKeyCode() == KeyEvent.VK_A) {
            hello.x = hello.x - 1;
            hello.setLocation(hello.x, hello.y);
            repaint();

        }
        if (e.getKeyCode() == KeyEvent.VK_D) {
            hello.x = hello.x + 1;
            hello.setLocation(hello.x, hello.y);
            repaint();

        }
        if (e.getKeyCode() == KeyEvent.VK_SPACE) {

            bullet.update(hello.x, hello.y);
            fired = true;
            if (count > 5) {
                timer.stop();
            }
            timer = new Timer(50, new TimerListener());
            timer.start();


            repaint();

        }





    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_SPACE) {


            //  fired = false;



            repaint();

        }


    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

}   

子弹课程:

package scratch;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JComponent;

public class Bullet {
    Rectangle BulletRect = new Rectangle(20, 20, 20, 20);

    public Bullet(int x, int y2) {

        BulletRect.y = y2;
        BulletRect.x = x;


    }
    public void fired() {

        BulletRect.y = BulletRect.y + 50;


    }

    public void update(int x1, int y4) {

        BulletRect.x = x1;
        BulletRect.y = y4;
    }

    public void drawBullet(Graphics g) {


        Graphics2D g2 = (Graphics2D) g;


        g2.fill(BulletRect);
    }
    // get bullets position

    //increment bullets position every so often

    //when bullet goes off screen, clear it from game





}

另外,如果我可以提出另一个问题,如果我想开始从swing学习android,最好走哪条路?谢谢。

1 个答案:

答案 0 :(得分:2)

以下是一些指示(我假设您使用的是Java 8)。

  • 而不是public Bullet bullet = new Bullet(hello.x, hello.y);使用private List<Bullet> bullets = new ArrayList<>();
  • 创建新项目符号时(检测空格键时)将其添加到列表中:bullets.add(new Bullet(x, y));
  • 当您需要在paintComponent中绘制所有项目符号时,可以使用bullets.stream().forEach(bullet -> bullet.drawBullet(g));
  • 不再需要fired字段:空的子弹列表与未触发相同。
  • 您需要删除不在屏幕上的项目符号。这可以通过以下方式完成:bullets.removeIf(Bullet::isOffScreen);假设您实现了isOffScreen方法。