关于用java2D绘制的问题

时间:2022-07-31 21:54:55

I have another question, this is also extra credit and not homework. This time I need to create a border with out using java2d. The instructions are... Write a method called drawRectangleBorder having six parameters which does not use the graphics package. It draws a rectangular border starting at the x and y coordinates given as the first two parameters, having a width and height given by the third and fourth parameters, the width of the border given by the fifth parameter in the color given by the sixth parameter. The parameter list is: x, y, width, height, borderWidth, color

我有另一个问题,这也是额外的功劳,而不是功课。这次我需要使用java2d创建一个边框。说明是......编写一个名为drawRectangleBorder的方法,该方法有六个不使用图形包的参数。它绘制一个矩形边框,从作为前两个参数给出的x和y坐标开始,具有由第三个和第四个参数给出的宽度和高度,由第六个参数给出的颜色中第五个参数给出的边框宽度。参数列表为:x,y,width,height,borderWidth,color

I used a previous method I made to create a border around the outside of a picture but the best I can make it do now is a couple scattered boxes. The most recent version will not show anything

我使用了之前的方法来创建一个围绕图片外部的边框,但我现在可以做的最好的是几个分散的盒子。最新版本不会显示任何内容

public void drawRectangleBorder(
        int x, int y, int width, int height, int border, Color newColor) {
    int startX = 0;
    int startY = 0;

    // top and bottom  
    for (startX = x; x < width; x++) {
        for (startY = y; y < border; y++) {
            // top pixel
            this.getPixel(startX, startY).setColor(newColor);
            // bottom pixel
            this.getPixel(startX + width, startY + height).setColor(newColor);
        } // for-y
    } // for-x

    // left and right  
    for (startX = x; x < border; x++) {
        for (startY = y; y < height; y++) {
            // left pixel
            this.getPixel(startX, startY).setColor(newColor);
            // right pixel
            this.getPixel(startX + width, StartY + height).setColor(newColor);
        } // for-y
    } // for-x

    return;
} // end drawRectangleBorder

Again I thank you for any input.

我再次感谢你的任何意见。

2 个解决方案

#1


0  

You can alter the pixels in a java.awt.BufferedImage as shown here.

您可以更改java.awt.BufferedImage中的像素,如下所示。

#2


0  

I might be too sleepy but I think your forgetting to set the pixel back into this (whatever this is ^^)

我可能太困了,但我认为你忘了将像素设置回来(不管这是^^)

I'm guessing this.getPixel sends your back a copy so you might want to do something like

我猜这个.getPixel会发回你的副本,所以你可能想要做类似的事情

Pixel p = this.getPixel( startX, startY );
p.setColor(newColor);
this.setPixel(startX, startY, p);

#1


0  

You can alter the pixels in a java.awt.BufferedImage as shown here.

您可以更改java.awt.BufferedImage中的像素,如下所示。

#2


0  

I might be too sleepy but I think your forgetting to set the pixel back into this (whatever this is ^^)

我可能太困了,但我认为你忘了将像素设置回来(不管这是^^)

I'm guessing this.getPixel sends your back a copy so you might want to do something like

我猜这个.getPixel会发回你的副本,所以你可能想要做类似的事情

Pixel p = this.getPixel( startX, startY );
p.setColor(newColor);
this.setPixel(startX, startY, p);