绘制其方法在另一个类中的形状

时间:2015-11-16 12:42:48

标签: java swing user-interface 2d

我需要为游戏创建一个GUI,我需要在屏幕上显示武器和盾牌。我需要修改程序,以便显示武器和盾牌,但是它们的属性在另一个类中。我知道我将不得不扩展JPanel类并覆盖其paintComponent()方法。

public class GraphicsUtil {

    private static final int MUZZLE_FRACTION = 3;
    private static final Color MUZZLE_COLOR = Color.MAGENTA;

    public static void drawShield(IShield shield, Graphics g, GameSpace gameSpace, double origHealth) {

        drawPiece(shield, g, gameSpace, origHealth);
    }

    public static void drawWeapon(IWeapon weapon, Graphics g,
            GameSpace gameSpace, double origHealth) {
        Color oldColor = g.getColor();
        drawPiece(weapon, g, gameSpace, origHealth);

        double orientation = weapon.getOrientation();
        int x1 = gameSpace.convertToScreenX(weapon.getXPos());
        int y1 = gameSpace.convertToScreenY(weapon.getYPos());
        int radius = gameSpace.convertToScreenDistance(weapon.getRadius());

        int x2 = gameSpace.convertToScreenX(weapon.getXPos()
                + weapon.getRadius() * Math.cos(weapon.getOrientation()));
        int y2 = gameSpace.convertToScreenY(weapon.getYPos()
                + weapon.getRadius() * Math.sin(weapon.getOrientation()));

        double frac = 1 - 1f / MUZZLE_FRACTION;

        int muzzleCentreX
                = x1 + (int) Math.round(frac * radius * Math.cos(orientation));
        int muzzleCentreY
                = y1 - (int) Math.round(frac * radius * Math.sin(orientation));
        int muzzleRadius = radius / MUZZLE_FRACTION;
        int topMuzzleX = muzzleCentreX - muzzleRadius;
        int topMuzzleY = muzzleCentreY - muzzleRadius;
        g.setColor(MUZZLE_COLOR);
        g.fillOval(topMuzzleX, topMuzzleY, 2 * muzzleRadius, 2 * muzzleRadius);

        g.setColor(Color.BLACK);
        g.drawLine(x1, y1, x2, y2);

        g.setColor(oldColor);
    }


    public static boolean isInMuzzle(IPiece piece, double x, double y) {
        boolean result;
        if (piece instanceof IWeapon) {
            IWeapon weapon = (IWeapon) piece;
            double radius = weapon.getRadius();
            double orientation = weapon.getOrientation();
            double frac = 1 - 1f / MUZZLE_FRACTION;
            double cx = piece.getXPos() + frac * radius * Math.cos(orientation);
            double cy = piece.getYPos() + frac * radius * Math.sin(orientation);
            double dx = x - cx;
            double dy = y - cy;
            double dist = Math.sqrt(dx * dx + dy * dy);
            result = dist <= radius / MUZZLE_FRACTION;


        } else {
            result = false;
        }
        return result;
    }


    public static double getAngle(double x1, double y1, double x2, double y2) {
        double dx = x2 - x1;
        double dy = y2 - y1;
        double hypotenuse = Math.sqrt(dx * dx + dy * dy);
        double angle;
        if (dy > 0) {
            angle = Math.acos(dx / hypotenuse);
        } else {
            angle = 2 * Math.PI - Math.acos(dx / hypotenuse);
        }
        return angle;
    }

    private static void drawPiece(IPiece piece, Graphics g, GameSpace gameSpace,
            double maxHealth) {
        Color oldColor = g.getColor();
        int centreX = gameSpace.convertToScreenX(piece.getXPos());
        int centreY = gameSpace.convertToScreenY(piece.getYPos());

        int displayRadius = gameSpace.convertToScreenDistance(piece.getRadius());

        int topX = centreX - displayRadius;
        int topY = centreY - displayRadius;

        if (piece.getOwner() == 0) {
            g.setColor(Color.CYAN);
        } else {
            g.setColor(Color.PINK);
        }

        g.drawOval(topX, topY, 2 * displayRadius, 2 * displayRadius);

        int healthRadius
                = (int) Math.round(displayRadius * piece.getHealth() / maxHealth);
        int topHealthX = centreX - healthRadius;
        int topHealthY = centreY - healthRadius;
        g.fillOval(topHealthX, topHealthY, 2 * healthRadius, 2 * healthRadius);

        String name = piece.getName();
        if (name != null) {
            g.setColor(Color.BLACK);
            g.drawString(name, centreX, centreY);
        }
        g.setColor(oldColor);
    }
}

这是我的主要方法:

public class Main extends JPanel {

        public Main(){

            setSize(new Dimension(400,400));
            setPreferredSize(new Dimension(400,400));
            GraphicsUtil object = new GraphicsUtil();

        }

        @Override
        public void paintComponent(Graphics g){

            super.paintComponent(g);



        }
        public static void main(String[] args) {

            Main window = new Main();
            JFrame frame = new JFrame("RICOCHET");
            frame.add(window); 
            frame.pack();
            frame.setResizable(false);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            frame.setLocationRelativeTo(null); 

            frame.setVisible(true); 

    }


}

如何调用这些方法?为了让它们出现在我的框架中,我需要做什么?谢谢。

1 个答案:

答案 0 :(得分:0)

据推测,你创造了一件或多件武器或碎片。在paintComponent()方法中,只需致电

即可
GraphicsUtil.drawWeapon( myWeapon, g, ... );