0

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();
    }
}
  • It'll be easier to help you with your question if you include the code. Also, you may benefit if you center the attention in the last, more concrete question (mouseListener) since the first one is too broad. See [ask]. – walen Feb 21 '17 at 08:14
  • 2
    Without sample code it is hard to answer this – Lemonov Feb 21 '17 at 08:14
  • [This example](http://stackoverflow.com/questions/21033199/how-do-i-stop-my-paint-method-form-repeating-twice/21033258#21033258) should provide an idea for your first question – MadProgrammer Feb 21 '17 at 08:17
  • *"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"* - Kinda, but not really and it would be complicated and would require more context than your question provides – MadProgrammer Feb 21 '17 at 08:19
  • I didn't add the code because i always do something the wrong way. – Radoslav Todorov Feb 21 '17 at 08:24
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). *"I didn't add the code because i always do something the wrong way."* What do you mean 'the wrong way'? Wrong in the code? Wrong in the way it is posted? Something else? Note that by not posting it, this question has already got one close vote. Is that 'right'? – Andrew Thompson Feb 21 '17 at 09:42
  • *"Finally my last question.."* You seem to be mistaking this Q&A site for a help desk. Separate questions should be asked in separate question threads. – Andrew Thompson Feb 21 '17 at 10:07

1 Answers1

3

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

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

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);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433