0

I am creating a java GUI that will show random face of a six-sided die. I have drawn all of the dots I will need (top, mid and bottom left/right as well as a centre dot) and they all appear in my GUI but now I need to break them down into faces, for example the 1-face will only contain the centre dot. I have specified which face will use which graphics in my code as comments in my paintComponent(Graphics g) method.

How would I implement this? Here is my code:

package weekThree;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;

@SuppressWarnings("serial")
public class Die extends JPanel {

    private Color circleColor = Color.BLACK;
    private int circX  = 75;
    private int circY = circX;
    private int circW = 75;
    private int circH = circW;

    protected void paintComponent(Graphics g) {

        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g2.setColor(circleColor);
        g2.fillOval(circX, circY, circW, circH);

        g2.setColor(circleColor);
        g2.drawOval(circX, circY, circW, circH);


        Graphics2D g3 = (Graphics2D) g;
        g3.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g3.setColor(circleColor);
        g3.fillOval(600, circY, circW, circH);

        g3.setColor(circleColor);
        g3.drawOval(600, circY, circW, circH);


        Graphics2D g4 = (Graphics2D) g;
        g4.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g4.setColor(circleColor);
        g4.fillOval(600, 325, circW, circH);

        g4.setColor(circleColor);
        g4.drawOval(600, 325, circW, circH);


        Graphics2D g5 = (Graphics2D) g;
        g5.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g5.setColor(circleColor);
        g5.fillOval(600, 600, circW, circH);

        g5.setColor(circleColor);
        g5.drawOval(600, 600, circW, circH);


        Graphics2D g6 = (Graphics2D) g;
        g6.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g6.setColor(circleColor);
        g6.fillOval(circX, 325, circW, circH);

        g6.setColor(circleColor);
        g6.drawOval(circX, 325, circW, circH);


        Graphics2D g7 = (Graphics2D) g;
        g7.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g7.setColor(circleColor);
        g7.fillOval(circX, 600, circW, circH);

        g7.setColor(circleColor);
        g7.drawOval(circX, 600, circW, circH);


        Graphics2D g8 = (Graphics2D) g;
        g8.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g8.setColor(circleColor);
        g8.fillOval(325, 325, circW, circH);

        g8.setColor(circleColor);
        g8.drawOval(325, 325, circW, circH);

        //1 = g8
        //2 = g2, g5
        //3 = g7, g3, g8
        //4 = g2, g3, g7, g5
        //5 = g2, g3, g8, g7, g5
        //6 = g2, g3, g4, g5, g6, g7

    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("Die Roller");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new Die());

        JButton b1 = new JButton("Roll");

        frame.setSize(800, 800);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

Thanks

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
DaveRamseyFan
  • 45
  • 2
  • 9
  • 3
    You have a lot of redundant code. Might I recommend creating one graphics object and only drawing the necessary circles. Additionally you only need to set the color when you want it to change. It will maintain the color after its been set. – Ryan Jackman Feb 12 '16 at 21:38
  • 3
    Why are you creating all those unnecessary Graphics2D variables? They all reference the very same Graphics object, and so they serve absolutely no purpose other than to make your code much more confusing than it has to be. – Hovercraft Full Of Eels Feb 12 '16 at 21:40
  • 2
    Also your paintComponent method does not call the super's method which breaks the painting chain. Also, why not draw your die faces to 6 BufferedImages and simply display the appropriate image when needed? – Hovercraft Full Of Eels Feb 12 '16 at 21:41
  • Thanks guys I have trimmed down my code and will look into buffered images – DaveRamseyFan Feb 12 '16 at 21:48
  • 1
    Maybe something like [this](http://stackoverflow.com/questions/21033199/how-do-i-stop-my-paint-method-form-repeating-twice/21033258#21033258)? – MadProgrammer Feb 12 '16 at 22:41

0 Answers0