开发者

Graphics.drawString() Isn't Drawing

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:

Graphics.drawString() Isn't Drawing

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;
        开发者_StackOverflow}

        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?


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.


Try

String fontString = "MS Gothic";
Font font = new Font(fontString, Font.PLAIN, 24);
g2d.setFont(font);

before drawString()

try Apple Gothic if you are on OS X.


With what color are you calling g2d.drawString()?

For example:

    //add this line before next
    g2d.setBackground(Color.BLACK); //or 'g2d.setPaint(Color.BLACK);'
    g2d.drawString(charToWrite, 0, 0);

Are you sure, that current font has the symbols you are trying to paint? - Change the font.


I am glad I am not the only one having this issue for class. I haven't tested this yet but according to this site http://www.herongyang.com/Swing/JFrame-Draw-Chinese-Character-on-Frame.html this font should work for creating the font that you need to create the characters.

g2d.setFont(new Font("SimSun",Font.PLAIN, 12));
g2d.drawString(charToWrite, 0, 0);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜