package pl.wroc.pwr.imagechannel.draw; import pl.wroc.pwr.IOperator2P; import pl.wroc.pwr.imagechannel.IImageChannel; public class DrawRectangle implements IOperator2P { private float value; private boolean fill; private int thickness; public DrawRectangle(float value, boolean fill) { this.value = value; this.fill = fill; this.thickness = 3; } public DrawRectangle(float value, int thickness) { this.value = value; this.fill = false; this.thickness = thickness; } public IImageChannel apply(IImageChannel channel, int[] box) { if (this.fill) { for (int dx = box[0]; dx <= box[2]; dx++) { for (int dy = box[1]; dy <= box[3]; dy++) { if (channel.inRange(dx, dy)) { channel.setValue(dx, dy, this.value); } } } } else { for (int dx = box[0]; dx <= box[2]; dx++) { for (int i = 0; i < this.thickness; i++) { if (channel.inRange(dx, box[1] + i)) { channel.setValue(dx, box[1] + i, this.value); } if (channel.inRange(dx, box[3] - i)) { channel.setValue(dx, box[3] - i, this.value); } } } for (int dy = box[1]; dy <= box[3]; dy++) { for (int i = 0; i < this.thickness; i++) { if (channel.inRange(box[0] + i, dy)) { channel.setValue(box[0] + i, dy, this.value); } if (channel.inRange(box[2] - i, dy)) { channel.setValue(box[2] - i, dy, this.value); } } } } return channel; } }