简体   繁体   中英

Java Swing Die drawing

I am drawing a die that simulates a new random roll when you click on it and redraws it. I have a class Die with static final fields middleX and middleY and i am calculating the coordinates of the other dots from those fields.

My first question is what is an even more efficient way of drawing the dots because i am switching the newly generated number and there is a lot of code duplication. Finally my last question is if there is a way to invoke a method that is in the Die class in a mouseListener without using the die object directly. I want the code in the mouseListener to be able to handle clicks no matter which die is clicked.

private class MyAdapter extends MouseAdapter
{       
    public void mouseClicked(MouseEvent event)
    {       
        die.updateVal((int) Math.floor(Math.random()*6) +1);
        die.repaint();
    }
}

..what is an even more efficient way of drawing the dots..

Use existing Unicode characters that represent Die Faces. They start at codepoint 9856. EG

import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class DieRoll {

    private JComponent ui = null;
    int dieStart = 9856;
    JLabel dieLabel = new JLabel();
    Random r = new Random();

    DieRoll() {
        initUI();
    }

    public void initUI() {
        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        Font dieFont = null;
        Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
        String firstDie = new String(Character.toChars(dieStart));
        for (Font font : fonts) {
            if (font.canDisplayUpTo(firstDie)<0) {
                // the first font that will render the Die Faces
                dieFont = font;
                break;
            }
        }
        dieLabel.setFont(dieFont.deriveFont(200f));
        ui.add(dieLabel);

        setDie();

        Action rollDie = new AbstractAction("Roll the Die") {

            @Override
            public void actionPerformed(ActionEvent e) {
                setDie();
            }
        };
        ui.add(new JButton(rollDie), BorderLayout.PAGE_START);
    }

    private void setDie() {
        StringBuilder sb = new StringBuilder("<html><body>");
        // convert the numbers to HTML Unicode characters
        sb.append(String.format("&#%1s; ", dieStart + r.nextInt(6)));
        sb.append(String.format("&#%1s; ", dieStart + r.nextInt(6)));

        dieLabel.setText(sb.toString());
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                DieRoll o = new DieRoll();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM