How to draw heart using java awt libaray?

后端 未结 2 1010
执念已碎
执念已碎 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);
    }
    
    0 讨论(0)
  • 2020-12-22 09:48

    //You don't really need AWT in Heart drawing here ://

    public static void main(String[] args) {
           int H=7,W=7;
           for(int i=2;i<=(H+1)/2;i++){
    
          for(int j=0;j<W/2-i;j++){
            System.out.print(" ");
           }
        for(int k=1;k<(i+1)*2+1;k++){
        System.out.print("*");
        }
    
        if(i==2){
        System.out.print(" ");
        }
    
        for(int j=0;j<W/2-i+1;j++){
        System.out.print(" ");
        }
    
        for(int k=1;k<(i+1)*2+1;k++){
        if(i==(H+1)/2&&k>i+3)
        System.out.print("");
        else
        System.out.print("*");
        }
        System.out.println();
    
        }
        int HH=17,WW=17;
        for(int i=1;i<=HH/2;i++){
    
        for(int j=1;j<=i;j++){
        System.out.print(" ");
        }
        int z=WW-2*i;
        for(int k=1;k<=z;k++){
        System.out.print("*");
        }
        System.out.println();
     }
    }
    

    }

    0 讨论(0)
提交回复
热议问题