图形组件Java2D的Mouselistener

时间:2019-01-22 00:45:45

标签: java swing graphics graphics2d

为此,我正在使用Graphics2D创建轮盘游戏(赌场游戏)。我已经有轮盘赌了,所以我正在董事会中,我正在用矩形创建数字所在的位置。

我的下面的代码

package roulette;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;

public class RouletteInterface extends JPanel{

    private double spinValue = 0;
    private int click = 0;
    private Timer timer;

    public RouletteInterface() {
        spin();

    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 300);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        Graphics2D g3d = (Graphics2D) g.create();
        paintRoulette(g2d);
        paintBoard(g3d);

        g2d.dispose();
        g3d.dispose();
    }

    protected void spin() {
        if (timer != null && timer.isRunning()) {
            return;
        }
        timer = new Timer(5, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                spinValue += 0.02;
                repaint();
            }
        });
        timer.start();
    }

    protected void pause() {
        if(click == 0) {
            JOptionPane.showMessageDialog(this, spinValue);
        }

    }

    protected void paintRoulette(Graphics2D g2d) {
        RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHints(hints);

        int width = getWidth() / 2;
        int height = getHeight();

        int dimeter = Math.min(width, height);

        AffineTransform at = AffineTransform.getRotateInstance(spinValue, dimeter / 2, dimeter / 2);
        g2d.transform(at);

        double angle = 360 / 36.9;
        double startAngle = 0;
        int color = 0;

        for (int i = 0; i < 37; i++) {
            if (i == 0) {
                g2d.setColor(Color.GREEN);
            } else {
                if (color == 0) {
                    g2d.setColor(Color.BLACK);
                    color = 1;
                } else {
                    g2d.setColor(Color.RED);
                    color = 0;
                }
            }
            Arc2D arco = new Arc2D.Double(0, 0, dimeter, dimeter, startAngle, angle, Arc2D.PIE);
            g2d.fill(arco);
            startAngle += angle;


        }


    }

    public void paintBoard(Graphics2D g3d) {
        int x = 250;
        int y = 10;
        int width = 20;
        int height = 20;
        Rectangle2D rect = new Rectangle2D.Double(x, y, width, height);
        g3d.setColor(Color.RED);
        g3d.fill(rect);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (rect.contains(e.getX(), e.getY())) {
                    Rectangle2D rect1 = new Rectangle2D.Double(250, 40, 20, 20);
                    g3d.setColor(Color.BLACK);
                    g3d.fill(rect1);
                    repaint();
                    System.out.println("Square")
                }
            }
            public void mouseExited(MouseEvent e) {
                return;
            }
        });
    }

}

Paint board是板的创建者,作为一个测试,我创建了一个红色正方形,添加了带有创建一个新正方形(仅作为测试)并打印“ Square”的功能的鼠标侦听器,我的第一个问题是创建新的方块永远不会发生,其次,它会打印“ Square”(方形),但不符合预期,因为它会多次打印单词。我在做什么错了?

谢谢。

0 个答案:

没有答案