Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8764741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:06:38+00:00 2026-06-13T16:06:38+00:00

Hello I have created a die with the help of a switch statement to

  • 0

Hello I have created a die with the help of a switch statement to show the right face of the die, now I was planning to rewrite this into an array but I don;t know how this should look like.. ( I am new to Java and it’s just for learning purposes )
Can someone show me a little example of how it must look like?

here is the class that contains the switch statement:

package h05Dobbelsteen;

import java.awt.*;

import javax.swing.JPanel;

public class DobbelSteen extends JPanel {

private final static int SPOT_DIAMETER = 40; // diameter dobbelsteen rondjes
private int faceValue; // getoonde waarde op scherm

public DobbelSteen() {

}

/*
 * roll de dobbelsteen
 */
public int roll() {

    int val = (int) (6*Math.random() + 1); // bepaal getal tussen 1 - 6
    setValue(val);
    System.out.println(val);
    return val;

}

/*
 * set de waarde van de roll
 */
public void setValue(int spots) {
    faceValue = spots;
    repaint();
}

/*
 * get de waarde van de roll
 */
public int getValue() {
    return faceValue;
}

/*
 * teken de view van de dobbelsteen
 */
public void paintComponent(Graphics g) {

    int w = getWidth();  // Get height and width
    int h = getHeight();

    // Graphics naar 2d
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

    //... Paint background
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, w, h);
    g2.setColor(Color.BLACK);

    g2.drawRect(0, 0, w-1, h-1);  // Draw border

    switch (faceValue) {
        case 1:
            drawSpot(g2, w/2, h/2);
            break;
        case 3:
            drawSpot(g2, w/2, h/2);

        case 2:
            drawSpot(g2, w/4, h/4);
            drawSpot(g2, 3*w/4, 3*h/4);
            break;
        case 5:
            drawSpot(g2, w/2, h/2);
        case 4:
            drawSpot(g2, w/4, h/4);
            drawSpot(g2, 3*w/4, 3*h/4);
            drawSpot(g2, 3*w/4, h/4);
            drawSpot(g2, w/4, 3*h/4);
            break;
        case 6:
            drawSpot(g2, w/4, h/4);
            drawSpot(g2, 3*w/4, 3*h/4);
            drawSpot(g2, 3*w/4, h/4);
            drawSpot(g2, w/4, 3*h/4);
            drawSpot(g2, w/4, h/2);
            drawSpot(g2, 3*w/4, h/2);
            break;
    }
}

/*
 * Teken de spots
 */
private void drawSpot(Graphics2D g2, int x, int y) {
    g2.fillOval(x-SPOT_DIAMETER/2, y-SPOT_DIAMETER/2, SPOT_DIAMETER, SPOT_DIAMETER);
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:06:39+00:00Added an answer on June 13, 2026 at 4:06 pm

    something like:

    private class Point {
      int x;
      int y;
      Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    
    Point[][] pointSpecs = new Point[][] { {new Point( .5, .5) }, 
                                           {new Point(.25, .25), new Point(.75, .75)},
                                      ...};
    

    This is a declaration of an array of arrays. The first index is the die value-1 (since java arrays are zero-indexed). At each position is an array of Points that need to be drawn. Just the multipliers are there which you would need to multiply by your width and height.

    To use:

    public void paintComponent(Graphics g) {
    
    int w = getWidth();  // Get height and width
    int h = getHeight();
    
    // Graphics naar 2d
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    
    //... Paint background
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, w, h);
    g2.setColor(Color.BLACK);
    
    g2.drawRect(0, 0, w-1, h-1);  // Draw border
    
    Point[] points = pointSpecs[faceValue-1];
    for (Point point : points) {
      drawSpot(g2, w*point.x, h*point.y);
    }
    

    You’ll need to fill in the rest of the point values…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello everyone i have created a multi threaded chat server that looks like this:
I have created a service which would show Hello World message when it starts..
Hello i have created 2 frames, and when I run this program it will
hello i have created one windows application in c# .net and its working fine
Hello StackOverflow :) I have created some code inside my onStart(); method to ensure
I have basic hello word example of Prime Faces. I have created dynamic web
I have created a Hello World web part. When I pressed F5 in Visual
I have created button 2 below: <input id=Button1 type=button value=Stop onclick=alert('hello world');/> <input id=Button2
I have created an enum as so: enum Types { hi, hello, bye }
I have followed this guide: http://maven.apache.org/guides/plugin/guide-java-plugin-development.html I have created a maven-plugin project hello-maven-plugin with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.