How to draw heart using java awt libaray?

后端 未结 2 1011
执念已碎
执念已碎 2020-12-22 09:01

How to draw heart using java awt libaray? I am using Java AWT Libaray and I need to draw a heart for my game. How can I do this using AWT?

Here what I was thinking:

2条回答
  •  星月不相逢
    2020-12-22 09:31

    Had to write this code for something I'm doing and it works well enough. Draws a triangle at x and y with the specified width and height using graphics g.

    // Draw a heart.
    public void drawHeart(Graphics g, int x, int y, int width, int height) {
        int[] triangleX = {
                x - 2*width/18,
                x + width + 2*width/18,
                (x - 2*width/18 + x + width + 2*width/18)/2};
        int[] triangleY = { 
                y + height - 2*height/3, 
                y + height - 2*height/3, 
                y + height };
        g.fillOval(
                x - width/12,
                y, 
                width/2 + width/6, 
                height/2); 
        g.fillOval(
                x + width/2 - width/12,
                y,
                width/2 + width/6,
                height/2);
        g.fillPolygon(triangleX, triangleY, triangleX.length);
    }
    

提交回复
热议问题