Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将图片分成4个部分并更改每个部分?_Java - Fatal编程技术网

Java 如何将图片分成4个部分并更改每个部分?

Java 如何将图片分成4个部分并更改每个部分?,java,Java,在Picture类的主体中,创建一个名为“SpecialFect”的新公共方法,该方法的返回类型为void,不接受任何参数。 该方法应包含四个循环,每个循环迭代图像像素,每个循环迭代总像素数的1/4,并执行以下不同效果: 第一个循环是应用一种效果,从每个像素中移除蓝色和绿色分量,而保持红色分量不变。 下一个循环从最后一个离开的地方继续,从每个像素中移除蓝色和红色分量,保持绿色分量不变。 下一个循环从上一个循环结束的地方继续,以移除每个像素的绿色和红色分量,而保持蓝色分量不变。 最后一个循环是将每

在Picture类的主体中,创建一个名为“SpecialFect”的新公共方法,该方法的返回类型为void,不接受任何参数。 该方法应包含四个循环,每个循环迭代图像像素,每个循环迭代总像素数的1/4,并执行以下不同效果: 第一个循环是应用一种效果,从每个像素中移除蓝色和绿色分量,而保持红色分量不变。 下一个循环从最后一个离开的地方继续,从每个像素中移除蓝色和红色分量,保持绿色分量不变。 下一个循环从上一个循环结束的地方继续,以移除每个像素的绿色和红色分量,而保持蓝色分量不变。 最后一个循环是将每个像素转换为灰度

我该怎么做呢?我只知道如何把它一分为二,再把上面的部分改一下。。不知道如何开始做部分


谢谢

我假设您的Picture类具有以下方法:

int getNumberOfPixels()
float getRValueOfNthPixel(int n)
float getGValueOfNthPixel(int n)
float getBValueOfNthPixel(int n)
void setRValueOfNthPixel(int n, float r)
void setGValueOfNthPixel(int n, float g)
void setBValueOfNthPixel(int n, float b)
如果像素数始终是4的倍数,则“SpecialFect”方法的一个可能实现是:

public void specialEffect() {
    int n = getNumberOfPixels();
    int limit1 = 1 * n / 4;
    int limit2 = 2 * n / 4;
    int limit3 = 3 * n / 4;
    int limit4 = 4 * n / 4;

    /*
     * The first loop is to apply an effect that removes the both the blue
     * and green component from each pixel leaving the red component
     * unchanged.
     */
    for (int i = 0; i < limit1; i++) {
        setBValueOfNthPixel(i, 0);
        setGValueOfNthPixel(i, 0);
    }

    /*
     * The next loop continuing from where the last left off is to remove
     * the blue and red component from each pixel leaving the green
     * component unchanged.
     */
    for (int i = limit1; i < limit2; i++) {
        setBValueOfNthPixel(i, 0);
        setRValueOfNthPixel(i, 0);
    }

    /*
     * The next loop continuing from where the last left off it to remove
     * the green and red component from each pixel leaving the blue 
     * component unchanged.
     */
    for (int i = limit2; i < limit3; i++) {
        setGValueOfNthPixel(i, 0);
        setRValueOfNthPixel(i, 0);
    }

    /*
     * The final loop continuing from where the last off is to convert each
     * pixel to greyscale.
     */
    for (int i = limit3; i < limit4; i++) {
        float grayValue = (getRValueOfNthPixel(i)
                + getGValueOfNthPixel(i)
                + getBValueOfNthPixel(i)) / 3;
        setRValueOfNthPixel(i, grayValue);
        setGValueOfNthPixel(i, grayValue);
        setBValueOfNthPixel(i, grayValue);
    }
}
public void specialefect(){
int n=getNumberOfPixels();
int limit1=1*n/4;
int limit2=2*n/4;
int limit3=3*n/4;
int limit4=4*n/4;
/*
*第一个循环是应用一个效果,该效果将同时移除蓝色和蓝色
*和来自每个像素的绿色分量离开红色分量
*不变。
*/
对于(int i=0;i
我假设您的Picture类具有以下方法:

int getNumberOfPixels()
float getRValueOfNthPixel(int n)
float getGValueOfNthPixel(int n)
float getBValueOfNthPixel(int n)
void setRValueOfNthPixel(int n, float r)
void setGValueOfNthPixel(int n, float g)
void setBValueOfNthPixel(int n, float b)
如果像素数始终是4的倍数,则“SpecialFect”方法的一个可能实现是:

public void specialEffect() {
    int n = getNumberOfPixels();
    int limit1 = 1 * n / 4;
    int limit2 = 2 * n / 4;
    int limit3 = 3 * n / 4;
    int limit4 = 4 * n / 4;

    /*
     * The first loop is to apply an effect that removes the both the blue
     * and green component from each pixel leaving the red component
     * unchanged.
     */
    for (int i = 0; i < limit1; i++) {
        setBValueOfNthPixel(i, 0);
        setGValueOfNthPixel(i, 0);
    }

    /*
     * The next loop continuing from where the last left off is to remove
     * the blue and red component from each pixel leaving the green
     * component unchanged.
     */
    for (int i = limit1; i < limit2; i++) {
        setBValueOfNthPixel(i, 0);
        setRValueOfNthPixel(i, 0);
    }

    /*
     * The next loop continuing from where the last left off it to remove
     * the green and red component from each pixel leaving the blue 
     * component unchanged.
     */
    for (int i = limit2; i < limit3; i++) {
        setGValueOfNthPixel(i, 0);
        setRValueOfNthPixel(i, 0);
    }

    /*
     * The final loop continuing from where the last off is to convert each
     * pixel to greyscale.
     */
    for (int i = limit3; i < limit4; i++) {
        float grayValue = (getRValueOfNthPixel(i)
                + getGValueOfNthPixel(i)
                + getBValueOfNthPixel(i)) / 3;
        setRValueOfNthPixel(i, grayValue);
        setGValueOfNthPixel(i, grayValue);
        setBValueOfNthPixel(i, grayValue);
    }
}
public void specialefect(){
int n=getNumberOfPixels();
int limit1=1*n/4;
int limit2=2*n/4;
int limit3=3*n/4;
int limit4=4*n/4;
/*
*第一个循环是应用一个效果,该效果将同时移除蓝色和蓝色
*和来自每个像素的绿色分量离开红色分量
*不变。
*/
对于(int i=0;i
下面的代码生成此图片:

private static BufferedImage specialEffect(BufferedImage in) {

    BufferedImage out = new BufferedImage(in.getWidth(), in.getHeight(), 
            BufferedImage.TYPE_INT_ARGB);

    for (int x = 0; x < out.getWidth() / 2; x++) {
        for (int y = 0; y < out.getHeight() / 2; y++) {
            Color c = new Color(in.getRGB(x, y));
            out.setRGB(x, y, new Color(c.getRed(), 0, 0).getRGB());
        }
    }
    for (int x = out.getWidth() / 2; x < out.getWidth(); x++) {
        for (int y = 0; y < out.getHeight() / 2; y++) {
            Color c = new Color(in.getRGB(x, y));
            out.setRGB(x, y, new Color(0, c.getGreen(), 0).getRGB());
        }
    }
    for (int x = 0; x < out.getWidth() / 2; x++) {
        for (int y = out.getHeight() / 2; y < out.getHeight(); y++) {
            Color c = new Color(in.getRGB(x, y));
            out.setRGB(x, y, new Color(0, 0, c.getBlue()).getRGB());
        }
    }
    for (int x = out.getWidth() / 2; x < out.getWidth(); x++) {
        for (int y = out.getHeight() / 2; y < out.getHeight(); y++) {
            Color c = new Color(in.getRGB(x, y));
            int m = Math.max(c.getRed(),Math.max(c.getGreen(),c.getBlue()));
            out.setRGB(x, y, new Color(m, m, m).getRGB());
        }
    }

    return out;
}

public static void main(String[] args) throws IOException {
    JFrame frame = new JFrame("Test");

    frame.add(new JComponent() {

         BufferedImage image = specialEffect(ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png")));

         @Override
         protected void paintComponent(Graphics g) {
              super.paintComponent(g);
              g.drawImage(image, 0, 0, this);
         }
    });

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);
    frame.setVisible(true);
}

private static BufferedImage specialefect(BufferedImage in){
BuffereImage out=新的BuffereImage(in.getWidth()、in.getHeight(),
BuffereImage.TYPE_INT_ARGB);
对于(int x=0;x