Draw chat bubble

1

I found this code on the stackOverflow page in English.

protected void paintComponent(final Graphics g) {
 final Graphics2D graphics2D = (Graphics2D) g;

 RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
 graphics2D.setRenderingHints(qualityHints);
 graphics2D.setPaint(new Color(80, 150, 180));

 int width = getWidth();
 int height = getHeight();

 GeneralPath path = new GeneralPath();

 path.moveTo(5, 10);
 path.curveTo(5, 10, 7, 5, 0, 0);
 path.curveTo(0, 0, 12, 0, 12, 5);
 path.curveTo(12, 5, 12, 0, 20, 0);
 path.lineTo(width - 10, 0);
 path.curveTo(width - 10, 0, width, 0, width, 10);
 path.lineTo(width, height - 10);
 path.curveTo(width, height - 10, width, height, width - 10, height);
 path.lineTo(15, height);
 path.curveTo(15, height, 5, height, 5, height - 10);
 path.lineTo(5, 15);
 path.closePath();
 graphics2D.fill(path);
}

The previous code creates a bubble like this.

link

My question is how to do the same but in the following way.

link

So that the pointing arrow is down ...

Thank you and excuse my ignorance

    
asked by 11.12.2016 в 17:35
source

1 answer

0

Class GeneralPath uses 2 methods for your drawings:

  • lineTo(x,y) Move in a straight line with your position in mind
  • curveTo(x1,y1,xmedio,ymedio,xfinal,yfinal) It generates a curve taking into account the three coordinates given.
  • The following code generates a form similar to the one you require:

    int width = 150;
    int height = 50;
    
    // starting point
    int x = 20;
    int y = 20;
    
    path.moveTo(x,y);
    
    // inicio imágen
    path.curveTo(x, y, x + 2, y - 2, x + 4, y - 4);
    path.lineTo(width-4, y-4);
    path.curveTo(width-4, y-4,width+2, y, width, y+4);
    path.lineTo(width, height-4);
    path.curveTo(width, height-4, width+2, height, width+4, height+4);
    path.curveTo(width+4, height+4, width, height, width-4, height-4);
    path.curveTo(width-4, height-2, width-6, height+2, width-8, height);
    path.lineTo(x+4, height);
    path.curveTo(x+4, height, x-2, height-2, x, height-4);
    path.lineTo(x, y);
    

    The following image has several windows; each window represents a line of code from the first path.curveTo ..

        
    answered by 11.12.2016 в 18:49