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 7872265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:09:14+00:00 2026-06-03T02:09:14+00:00

This is my main class that i want to implement the mouse button. I

  • 0

This is my main class that i want to implement the mouse button. I am having trouble with the code in the actionPerformed area to have the die get a new value and repaint itself in the same location on the button click


import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Game 
{
private static Die die;
public static void main(String[] agrs)
{
    JFrame frame = new JFrame();

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());

    JButton roll = new JButton("roll");
    class RollListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
          die.roll();
                            //This is where it is just replacing the first die
                      die2.roll2();
        }
    }
    RollListener listener = new RollListener();
    roll.addActionListener(listener);
    panel.add(roll,BorderLayout.NORTH);

    die = new Die(20,20, Roll.die1());
    panel.add(die, BorderLayout.CENTER);

                //This im pretty sure wont add correctly its just overlapping the first die
                //but from what iv seen if i use different layouts it draws different and sometimes not at all.
                die2 = new Die(75,20,Roll.die2());
                panel.add(die2, BorderLayout.CENTER);

    frame.add(panel);

    //When program closed it terminates program execution
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Makes frame visible
    frame.setVisible(true);
}
}

This is my die class that creates the die


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;

import javax.swing.JComponent;


public class Die extends JComponent
{
private static final long serialVersionUID = 1L;

//Variable for creating the die background
private Rectangle dieBorder;
//private int number = Roll.die1();
//Variable for creating the number of circles on die
private Ellipse2D numCircle;
private Ellipse2D numCircle2;
private Ellipse2D numCircle3;
private Ellipse2D numCircle4;
private Ellipse2D numCircle5;
private Ellipse2D numCircle6;
private Ellipse2D numCircle7;

      private int width = 50;
      private int height = 50;

/*
 * This constructs the DieComponent
 * with @param number. 
 */
public Die(int xPos, int yPos, int number1)
{
    number = number1;
    dieBorder = new Rectangle(xPos, yPos, width, height);

    numCircle = new Ellipse2D.Double(locationX(xPos, 2, 0), locationY(yPos, 2, 0), 8, 8);
    numCircle2 = new Ellipse2D.Double(locationX(xPos, 1, -7), locationY(yPos, 1, -7), 8, 8);
    numCircle3 = new Ellipse2D.Double(locationX(xPos, 1, -43), locationY(yPos, 1, -43), 8, 8);
    numCircle4 = new Ellipse2D.Double(locationX(xPos, 1, -7), locationY(yPos, 1, -43), 8, 8);
    numCircle5 = new Ellipse2D.Double(locationX(xPos, 1, -43), locationY(yPos, 1, -7), 8, 8);
    numCircle7 = new Ellipse2D.Double(locationX(xPos, 2, -18), locationY(yPos, 2, 0), 8, 8);
    numCircle6 = new Ellipse2D.Double(locationX(xPos, 2, 18), locationY(yPos, 2, 0), 8, 8);
}

/*
 * This will create the die graphics for 
 * the game. With @param g.
 */
public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;
    super.paintComponent(g2);

    //Sets color of field
    g2.setColor(Color.RED);
    //Draws the die background
    g2.draw(dieBorder);
    //Fills in the background
    g2.fill(dieBorder);

    if(number == 1)
    {
    //Sets color to draw the circle with
    g2.setColor(Color.WHITE);
    //Draws the circle
    g2.draw(numCircle);
    //Fills in the circle
    g2.fill(numCircle);
    }

    if(number == 2)
    {
        g2.setColor(Color.WHITE);
        g2.draw(numCircle2);
        g2.draw(numCircle3);
        g2.fill(numCircle3);
        g2.fill(numCircle2);
    }

    if(number == 3)
    {
        g2.setColor(Color.WHITE);
        g2.draw(numCircle2);
        g2.draw(numCircle3);
        g2.draw(numCircle);
        g2.fill(numCircle3);
        g2.fill(numCircle2);
        g2.fill(numCircle);
    }

    if(number == 4)
    {
        g2.setColor(Color.WHITE);
        g2.draw(numCircle2);
        g2.draw(numCircle3);
        g2.draw(numCircle4);
        g2.draw(numCircle5);
        g2.fill(numCircle3);
        g2.fill(numCircle2);
        g2.fill(numCircle4);
        g2.fill(numCircle5);

    }

    if(number == 5)
    {
        g2.setColor(Color.WHITE);
        g2.draw(numCircle);
        g2.draw(numCircle2);
        g2.draw(numCircle3);
        g2.draw(numCircle4);
        g2.draw(numCircle5);
        g2.fill(numCircle3);
        g2.fill(numCircle2);
        g2.fill(numCircle4);
        g2.fill(numCircle);
        g2.fill(numCircle5);
    }

    if(number == 6)
    {
        g2.setColor(Color.WHITE);
        g2.draw(numCircle2);
        g2.draw(numCircle3);
        g2.draw(numCircle4);
        g2.draw(numCircle5);
        g2.draw(numCircle6);
        g2.draw(numCircle7);
        g2.fill(numCircle3);
        g2.fill(numCircle2);
        g2.fill(numCircle4);
        g2.fill(numCircle5);
        g2.fill(numCircle6);
        g2.fill(numCircle7);
    }
}

public static int locationX(int xPos, int spot, int move)
{
    int xPosition = (xPos + 50 / spot) - 4 + move;

    //Returns the x position of where the circle should be
    return xPosition;
}

/*
 * This method will get the height of 
 * the border of the die and formulate where
 * to place the y position of the numCircle based
 * on value of die and uses spot to determine where
 * to draw circle.
 */
public static int locationY(int yPos, int spot, int move)
{
    //Formula to calculate position of circle
    int yPosition = (yPos + 50 / spot) - 4 + move;

    //Returns the y position of where circle should be
    return yPosition;
}

      public void roll()
      {
         number = Roll.die1();
         repaint();
      }
      //This is where i think im messing up cuz it just replaces the first die
      public void roll2()
      {
        number = Roll.die2();
        repaint();
      }
}

This is my roll class that gets the value for the die


import java.util.Random;

/*
* This class sets up methods that 
* retrieve what random number is to
* be displayed on the die for each 
* one.
*/
public class Roll 
{
//Sets up var for die1
private static int die1;
//Sets up var for die2
private static int die2;

/*
 * This method creates the die1
 * and it assigns a random number
 * between 1-6 to the die.
 */
public static int die1()
{
    //Create random number generator
    Random gen = new Random();

    //Uses generator to assign number
    die1 = gen.nextInt(6) + 1;

    //Returns die1 value
    return die1;
}


/*
 * This method creates die2 and 
 * assigns a random number between
 * 1-6 to the die.
 */
public static int die2()
{
    //Create random number generator
    Random gen2 = new Random();

    //Uses generator to assign number
    die2 = gen2.nextInt(6) + 1;

    //Returns die2 value
    return die2;
}

/*
 * This method gets the value of both
 * die1 and die2 and adds them together.
 * This will allow the game to decide
 * what bets win and what bets lose.
 */
public static int total()
{
    //Gets die1 and die2 and adds them
    int total = die1 + die2;
    //Returns total value
    return total;
}
}
  • 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-03T02:09:16+00:00Added an answer on June 3, 2026 at 2:09 am

    I think that you need to change your Die class and give it a roll() method where it calls Roe’s die1() method and sets the number field to this value and calls repaint. In semi-pseudocode:

    public void roll() {
       Use Roll to get a new int value
       set number field to this new int value
       call repaint on itself
    }
    

    Then in the ActionListener’s actionPerformed(...) call this new method on the Die object that is displayed.

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

Sidebar

Related Questions

I have a main class and several inherited classes that implement a method with
I want to implement a class in c++ that has a callback. So I
I have one main interface and an abstract class implementing all derivable methods (that
I have some classes that all implement a certain interface. I want to store
I want to implement a 'favorite list' and let's assume that I have got
I want to make an application with plugins that also can have plugins (which
Say hypothetically I have a class... class Main { $prop1 = 2; $prop2 =
I have a C++ class Finder that stores a location in containers plus some
I'm writing a custom class that wraps a dict. As such, I want to
Here's the situation. I have a base class which implements a method that accepts

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.