Java中的透明循环对话框

时间:2011-05-13 11:39:46

标签: java graphics 2d

我正在用Java构建一个Poker Odds Calc应用程序。我想通过点击卡片的占位符来选择一张新卡片,这是一个扩展的JPanel,我“画”了卡片的面孔,并且有一个mouseListener。

我想象的是,当我点击卡片时,我希望在鼠标光标周围弹出一个圆形菜单,中间有一个圆圈,每个四分之一,每个套件四分之一,周围环形为了卡的价值减少十三。然后我将选择西装和价值,它会消失。你知道我能做到这一点吗?我研究了一下,我认为可以通过制作透明的JDialog来完成JavaFX,但我不确定。

有没有办法绘制一个完全自定义形状的JComponent,就像每个四分之一圆形的JButton形状等?我有一些Java经验,但没有GUI构建。

提前感谢您的时间。

编辑:使用你的评论并回答了我关于圆形对话框的问题(不知道这是否是最好的方法,但现在可以使用)。现在,无论如何我知道点击属于哪个区域(如果点击位于有用区域)而没有硬编码坐标?

2 个答案:

答案 0 :(得分:4)

我建议做自定义图形,而不是尝试自定义JButton等。当您单击JPanel时,您可以使用java.awt.Shape接口及其各种实现(如java.awt.geom.Ellipse2D)绘制圆形等。

这些形状带有contains()方法,可以告诉您一个点是否在Shape中。这样,当用户下次点击JPanel时,您可以通过浏览所有形状并进行检查来确定用户点击的形状。

答案 1 :(得分:0)

创建图形的代码就是这样,以防任何人需要它:

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D.Double;
import javax.swing.JDialog;

/**
 *
 * @author Dimitris Klimis <dnklimis at gmail.com>
 */
public class CardChooser extends JDialog implements MouseListener {

    int sizeX = 140;
    int sizeY = sizeX; //in case I don't want it to be circle
    int x, y;
    Point point;

    public CardChooser(Point point) {
        x = point.x;
        y = point.y;
        this.point = point;
        this.initComponents();
    }

    public static int[] getCard(Point point) {
        int[] output = {0, 0};
        CardChooser chooser = new CardChooser(point);
        return output;
    }

    @Override
    public void paint(Graphics g) {
        if (g instanceof Graphics2D) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            //Drawing the transparent dialog
            g2.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.0f));
            g2.fillRect(0, 0, getWidth(), getHeight());

            //Drawing the circles
            g2.setColor(Color.BLACK);
            drawCircle(g2, 100, new GradientPaint(0.0f, 0.0f, Color.darkGray, (float) getWidth(), (float) getHeight(), Color.lightGray, false));
            drawLines(g2, 13, 100);
            int smallCircle = 38;
            drawCircle(g2, smallCircle + 3, Color.GRAY);
            drawCircle(g2, smallCircle, new GradientPaint((float) (getWidth() * 0.25), (float) (getHeight() * 0.25), Color.lightGray, (float) (getWidth() * 0.75), (float) (getHeight() * 0.75), Color.darkGray, false));
            drawLines(g2, 4, smallCircle);
            drawCircle(g2, 10, Color.LIGHT_GRAY);
            drawSuiteLetters(g2);
            drawCardValues(g2);
            drawClosingX(g2);
        } else {
            super.paint(g);
        }
    }

    private void drawCircle(Graphics2D g2, int percentage, Paint fill) {
        double perc = (double) percentage / 100.0;
        Ellipse2D ellipse = new Ellipse2D.Double(((1 - perc) / 2) * sizeX, ((1 - perc) / 2) * sizeY, perc * sizeX, perc * sizeY);
        g2.setPaint(fill);
        g2.fill(ellipse);
        g2.setColor(Color.BLACK);
        g2.draw(ellipse);
    }

    private void drawLines(Graphics2D g2, int outOf, int percentage) {
        double rads = Math.toRadians(360.0 / outOf);
        double perc = (double) percentage / 100.0;
        Double zeroAxis = new Point.Double(sizeX / 2.0, sizeY / 2.0);
        for (int i = 0; i < outOf; i++) {
            g2.draw(new Line2D.Double(zeroAxis.x, zeroAxis.y, zeroAxis.x + (zeroAxis.x * perc * Math.sin(rads * i)), zeroAxis.y + (zeroAxis.y * perc * Math.cos(rads * i))));
        }
    }

    private void drawSuiteLetters(Graphics2D g2) {
        Double zeroAxis = new Point.Double(sizeX / 2.0, sizeY / 2.0);
        g2.setFont(new Font("Courier New", Font.BOLD, 25));
        g2.drawString("\u2660", (float) zeroAxis.x - 18, (float) zeroAxis.y - 5);//spades
        g2.drawString("\u2663", (float) zeroAxis.x + 3, (float) zeroAxis.y + 20);//clubs
        g2.setColor(Color.RED);
        g2.drawString("\u2665", (float) zeroAxis.x + 3, (float) zeroAxis.y - 3);//hearts
        g2.drawString("\u2666", (float) zeroAxis.x - 18, (float) zeroAxis.y + 19);//diamonds
        g2.setColor(Color.BLACK);
    }

    private void drawCardValues(Graphics2D g2) {
        Double zeroAxis = new Point.Double((sizeX / 2.0) - 8, 21);
        float xx = (float) zeroAxis.x;
        float yy = (float) zeroAxis.y;
        g2.setFont(new Font("Arial", Font.BOLD, 24));
        String[] letters = {"A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"};
        float[] xPosition = {0, 25, 46, 63, 58, 42, 15, -10, -37, -53, -58, -46, -25};
        float[] yPosition = {0, 7, 23, 50, 80, 102, 115, 115, 102, 80, 50, 23, 7};
        for (int i = 0; i < 13; i++) {
            g2.drawString(letters[i], xx + xPosition[i], yy + yPosition[i]);
        }
    }

    private void drawClosingX(Graphics2D g2) {
        Double zeroAxis = new Point.Double(sizeX / 2.0, sizeY / 2.0);
        g2.draw(new Line2D.Double(zeroAxis.x - 5, zeroAxis.y - 5, zeroAxis.x + 5, zeroAxis.y + 5));
        g2.draw(new Line2D.Double(zeroAxis.x - 5, zeroAxis.y + 5, zeroAxis.x + 5, zeroAxis.y - 5));
    }

    private void initComponents() {
        this.addMouseListener(this);
        this.setBounds(x - (sizeX / 2), y - (sizeY / 2), sizeX + 1, sizeX + 1);
        this.setUndecorated(true);
        this.setModal(true);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        this.setVisible(true);
    }

    public void mouseClicked(MouseEvent e) {
        this.dispose();
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }
}

PS。我扩展了JDialog,因为我无法让JPanel出现......