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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:21:19+00:00 2026-05-20T18:21:19+00:00

I am creating a simple Mahjong game for a final project at school and

  • 0

I am creating a simple Mahjong game for a final project at school and seem to be having some troubles with the drawString() method on the Graphics/Graphics2D object. I call the drawString method and see nothing written to the screen.

In my scenario I have extended the JFrame class and overridden the paintComponent() method to create a custom graphical object, specifically a mahjong tile. Using polygons described in various arrays I create a faux 3D view of a tile drawing the face, right side, and bottom of the tile. These polygons are filled using GradientPaint objects to give the tile a better appearance. Here is what it looks like:

Rendered Tile

My Tile class looks like this (note: some code was omitted for brevity):

public class Tile extends JPanel
{
    private int _width;
    private int _height;
    private int _depth;

    /**
     * Accessor to get the center of the face of the rendered
     * mahjong tile.
     *
     * @return A point containing the center of the face of the tile.
     */
    public Point getCenter()
    {
        return new Point(_width / 2, _height / 2);
    }

    /**
     * The default constructor creates a tile that is proportionally
     * calculated to 80 pixels wide.
     */
    public Tile()
    {
        this(80);
    }

    /**
     * Given the width parameter a mahjong tile is drawn according to
     * the proportions of a size 8 mahjong tile.
     *
     * @param width The width of the tile to be rendered.
     */
    public Tile(int width)
    {
        _width = width;
        _height = (int)(width * 1.23);
        _depth = (int)((width * 0.3) / 2);

        setPreferredSize(new Dimension((_width + _depth) + 1, (_height + _depth) + 1));
    }

    @Override public void paintComponent(Graphics g)
    {
        // ... setup polygon arrays ...

        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;

        // Turn on anti-aliasing.
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // ... setup various gradients ...

        // Fill the face of the tile.
        g2d.setPaint(faceGradient);
        g2d.fillPolygon(faceXPolyPoints, faceYPolyPoints, faceXPolyPoints.length);

        // Fill the right side of the top portion of the tile.
        g2d.setPaint(ivoryRightSideGradient);
        g2d.fillPolygon(ivoryRightSideXPolyPoints, ivoryRightSideYPolyPoints, ivoryRightSideXPolyPoints.length);

        // Fill the bottom side of the top portion of the tile.
        g2d.setPaint(ivoryBottomGradient);
        g2d.fillPolygon(ivoryBottomXPolyPoints, ivoryBottomYPolyPoints, ivoryBottomXPolyPoints.length);

        // Fill the right side of the bottom portion of the tile.
        g2d.setPaint(jadeRightSideGradient);
        g2d.fillPolygon(jadeRightSideXPolyPoints, jadeRightSideYPolyPoints, jadeRightSideXPolyPoints.length);

        // Fill the bottom side of the bottom portion of the tile.
        g2d.setPaint(jadeBottomGradient);
        g2d.fillPolygon(jadeBottomXPolyPoints, jadeBottomYPolyPoints, jadeBottomXPolyPoints.length);

        // Draw the outlines for the tile.
        g2d.setPaint(Color.BLACK);
        g2d.drawPolygon(faceXPolyPoints, faceYPolyPoints, faceXPolyPoints.length);
        g2d.drawPolygon(ivoryRightSideXPolyPoints, ivoryRightSideYPolyPoints, ivoryRightSideXPolyPoints.length);
        g2d.drawPolygon(ivoryBottomXPolyPoints, ivoryBottomYPolyPoints, ivoryBottomXPolyPoints.length);
        g2d.drawPolygon(jadeBottomXPolyPoints, jadeBottomYPolyPoints, jadeBottomXPolyPoints.length);
        g2d.drawPolygon(jadeRightSideXPolyPoints, jadeRightSideYPolyPoints, jadeRightSideXPolyPoints.length);
    }
}

As you may know, there are many different types of mahjong tiles one being a character tile that displays various Chinese characters on the tile face. So my Tile class sets up the basic drawing of a tile and I create a new CharacterTile class that extends/inherits the Tile class. The code for that class follows:

public class CharacterTile extends Tile
{
    private Character _character;

    public CharacterTile(Character character)
    {
        super();

        _character = character;
    }

    @Override public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        String charToWrite;


        switch (_character)
        {
            case ONE:
                charToWrite = "\u4E00";
                break;
            case TWO:
                charToWrite = "\u4E8C";
                break;
            case THREE:
                charToWrite = "\u4E09";
                break;
            case FOUR:
                charToWrite = "\u56DB";
                break;
            case FIVE:
                charToWrite = "\u4E94";
                break;
            case SIX:
                charToWrite = "\u516D";
                break;
            case SEVEN:
                charToWrite = "\u4E03";
                break;
            case EIGHT:
                charToWrite = "\u516B";
                break;
            case NINE:
                charToWrite = "\u4E5D";
                break;
            case NORTH:
                charToWrite = "\u5317";
                break;
            case EAST:
                charToWrite = "\u6771";
                break;
            case WEST:
                charToWrite = "\u897F";
                break;
            case SOUTH:
                charToWrite = "\u5357";
                break;
            case RED:
                charToWrite = "\u4E2D";
                break;
            case GREEN:
                charToWrite = "\u767C";
                break;
            case WAN:
                charToWrite = "\u842C";
                break;
            default:
                charToWrite = "?";
                break;
        }

        g2d.drawString(charToWrite, 0, 0);
    }
}

As you can see the default constructor for the CharacterTile class accepts an enum indicating the desired face value. Inside the overridden paintComponent I have a switch statement that sets the appropriate character to write and then I call the g2d.drawString() method to write the character in the upper left corner. The problem? It doesn’t write anything. What am I doing wrong?

  • 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-20T18:21:20+00:00Added an answer on May 20, 2026 at 6:21 pm
    g2d.drawString(charToWrite, 0, 0); 
    

    You need to specify the bottom/left coordinate when you draw a string. Try something like:

    g2d.drawString(charToWrite, 0, 10); 
    

    Also, you need to make sure the component has a valid size. By default the size is (0, 0), which means there is nothing to paint. This is generally done by specifying a preferred size and then let the layout manager set the size.

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

Sidebar

Related Questions

Creating some simple html pages specific for the iphone, and would like to use
I'm creating a simple jQuery editor, nothing complicated, and just can't seem to find
I am creating a simple 2D OpenGL game, and I need to know when
Creating simple app using GAE / Django-nonrel (I don't think the problem is specific
As in the question Creating simple c++.net wrapper. Step-by-step I am tring to use
I'm creating a simple GUI form with one button in IntelliJ IDEA 9. The
I am creating a simple android app to view a comic book. The pages
I'm creating a simple application with xcode and objc and I need to load
I am creating a simple HTTP client/server application on my local machine but I
I tried to make a file choose following the below link http://www.dreamincode.net/forums/topic/190013-creating-simple-file-chooser/ However, 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.