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

  • Home
  • SEARCH
  • 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 6625611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:47:42+00:00 2026-05-25T21:47:42+00:00

I’m working on a uni project which is to create a dice face using

  • 0

I’m working on a uni project which is to create a dice face using 2d graphics shapes. I have got that all done but I have a problem: I want my shape to change size when I adjust the window size, instead of just staying still also so that it stays in the middle.

I thought I could set the position so that is was center tried this but didn’t work. I’m not sure but would I have to write co-ordinates so that its changes size with the window? Any help with both problems would be great.

GUI setup code

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class DiceSimulator {

    public static void main(String[] args) {
        JFrame frame = new JFrame("DiceSimulator");
        frame.setVisible(true);
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        draw object = new draw();
        frame.add(object);
        frame.setLocationRelativeTo(null);
        object.drawing();
    }
}

Painting code

import javax.swing.*;
import java.awt.*;
//import java.util.Random;
public class draw extends JComponent {

    public void drawing() {
        repaint();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //Random1 random = new Random1();

        Graphics2D g2 = (Graphics2D) g;
        g.setColor(Color.BLACK);
        Rectangle box = new Rectangle(115, 60, 150, 150);
        g2.fill(box);

        g.setColor(Color.WHITE);
        g.fillOval(145, 75, 30, 30);

        g.setColor(Color.WHITE);
        g.fillOval(205, 75, 30, 30);

        g.setColor(Color.WHITE);
        g.fillOval(145, 115, 30, 30);

        g.setColor(Color.WHITE);
        g.fillOval(205, 115, 30, 30);

        g.setColor(Color.WHITE);
        g.fillOval(145, 155, 30, 30);

        g.setColor(Color.WHITE);
        g.fillOval(205, 155, 30, 30);
    }
}
  • 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-05-25T21:47:42+00:00Added an answer on May 25, 2026 at 9:47 pm

    In the paintComponent() method you need to use

    int width = getSize().width;
    int height = getSize().height;
    

    to get the current size of the component as it changes size when the frame changes size. Then based on this current size you can draw your components. This means that you can’t hardcode the values in your drawing methods.

    If you want to shift all the drawing coordinates with one command then you can use:

    g.translate(5, 5);
    

    at the top of the method. Then all hardcoded (x, y) values will be adjusted by 5 pixels each. This will allow you to change the centering of the drawing.

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

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.