Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create and distribute diagonal stripes on a rectangle?

I would like to be able to create bar charts with JFreeChart that looks similar to the following picture.

the desired result

It is a very basic mono-colored bar chart, but with one "fancy" detail: the diagonal stripes. I was thinking that this could be made possible by overlaying another picture on top of the normal bar. This picture would have the same dimensions as the bar, have diagonal white stripes and a transparent background. I am not quite sure how to do this though, as I have very little GUI experience, but I found a very useful article that deals with overlaying images on top of graphics from JFreeChart, so I am quite certain I should be able to pull that of.

But how should I create the diagonal stripes? I see how I could distribute the lines from the lower left corner to the upper right corner, but not the capped lines in the upper left and lower right corner. Can I somehow paint outside the rectangle (and not have it included in the picture)?

edit: After some searching I cannot see that my suggestion of overlaying an image with a transparent background would work, as I cannot find any examples on how to do this. On the other hand, merely painting the lines on the rectangle is probably easier.

like image 549
oligofren Avatar asked Feb 24 '23 09:02

oligofren


1 Answers

Using a gradient fill to draw lines

On trashgod's tip I tried filling a shape with a gradient that had sharp edges to simulate line drawing. This would prevent a lot of calculations and could potentially be a lot simpler. It worked quite ok for thick lines, but not for thinner lines. Using the following code produces the fill in the first picture:

    rect.setSpace(spaceBetweenLines);
    Color bg = Color.YELLOW;
    Color fg = Color.BLUE;
    rect.setPaint(new LinearGradientPaint(
            (float) startX, (float) startY, (float) (startX + spaceBetweenLines), (float) (startY + spaceBetweenLines),
            new float[] {0,.1f,.1001f}, new Color[] {fg,fg,bg}, MultipleGradientPaint.CycleMethod.REPEAT)
    );

Trying to paint thin lines using a gradient fill

Drawing lines using graphic primitives

Although simpler it did not work in my case. The more elaborate, but to me, more natural way of doing it, is simply drawing lines on top of the shape (rectangle, cirle, ...). The following code was used in producing the second image. Observe the use of the clip(Shape s) to restrict the line drawing to the shape underneath. The reason for not simply drawing a rectangle and using clip() to limit the shape is that the clip() operation is not aliased, thus producing jaggies. Therefore I have to draw the shape first to get smooth edges, then set the clip to prevent overflow in the forthcoming line drawing, and finally draw the lines.

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setPaint(getBackground());
    g2.fill(getShape());
    g2.setClip(getShape());

    // draw diagonal lines
    g2.setPaint(getLineColor());

    for (int x = (int) this.x, y = (int) (this.y); y - height < (this.y + height + getSpace()); ) {
        g2.drawLine(x, y ,  x + (int) width , y  - (int) width);

        y += getSpace();
    }

Just painting lines on top of another shape using clip(Shape s) to limit overflow

like image 178
oligofren Avatar answered Feb 27 '23 13:02

oligofren