2016-07-12 50 views
-1

enter image description here發電機設計與Java

我試圖生成以下給出緞紋設計,我的Java代碼的幫助,但這個代碼不創建一個名爲緞png文件

import java.awt.*; 
import java.awt.image.BufferedImage; 

import java.io.*; 

import javax.imageio.ImageIO; 
import javax.swing.JFrame; 


public class Sateen { 

BufferedImage image; 
int width; 
int height; 
int red,green,blue; 

public Sateen() { 

    try { 
    File input = new File("n.png"); 
    image = ImageIO.read(input); 
    width = image.getWidth(); 
    height = image.getHeight(); 
    //n is png file with only white pixels 
    for(int i=0; i<height; i++){ 

     for(int j=0; j<width; j++){ 

      Color p = new Color(image.getRGB(j, i)); 
     Color g = new Color(image.getRGB(j+5, i)); 

        //getting (j,i) coordinate pixel value and then comparing  it to next 5th pixel 

    if(p.getRed()==255&&p.getBlue()==255&&p.getGreen()==255&&g.getRed()==255&& 
    g.getBlue()==255&&g.getGreen()==255) 
        {         red=0; 
             blue=0; 
          green=0; } 
      //if both pixel value is white then setting 5th pixel value to black 
      Color newColor = new Color(red,green,blue); 


      image.setRGB(j+5,i,newColor.getRGB()); 
     j=j+5; 

     } 
    } 

    File ouptut = new File("Sateen.png"); 
    ImageIO.write(image, "png", ouptut); 
    //creating sateen png file 
    } catch (Exception e) {} 
    } 
static public void main(String args[]) throws Exception 
    { 
    Sateen obj = new Sateen(); 
} 
} 
+0

這還不編譯。您的評論以反斜槓開始,而不是常規斜槓。 – f1sh

+0

@ f1sh對不起,我的編輯速度不夠快 - 現在的代碼應該沒問題 –

+2

首先也是最重要的:沒有空的catch條款。如果有例外,它會吞下它,而你不知道它發生過。至少放置一個'e.printStackTrace()'。 – RealSkeptic

回答

0

似乎實際創建。 PNG文件(確保文件路徑是正確的?)。然而這段代碼不會產生緞紋模式,而是借鑑黑色像素第6欄,12,18等

+0

是的,我想輸入的文件應該是隻白色圖像文件 – sttaq

+0

的確,至少這是我從評論認爲 - > // n爲png文件,只有白色像素 此外,必須讓幸運沒有具有索引超出範圍的上image.setRGB – Vladimir

0

下面的代碼修復代碼中的某個問題。雖然它會生成一個輸出文件,但輸出文件看起來並不像您要生成的內容。也許你需要調整你的算法?

反正在這裏學最主要的是,如果你是遍歷數組和內環路遞增索引,那麼你需要考慮的情況下照顧您的索引可能走出數組的邊界即你j+5能成爲圖像之外的任何東西。

此外,請確保您的輸入文件中,這將取決於您運行從程序的正確位置。如果你從調試器/ IDE運行它,那麼它應該位於src文件夾旁邊的項目文件夾中。如果指定文件的完整路徑(如C:\SomeFolder\n.png)以避免輸入文件丟失錯誤,那將會更好。

最後,在您的catch塊中使用類似e.printStackTrace();的內容來找出代碼失敗的位置和原因。

import java.awt.*; 
import java.awt.image.BufferedImage; 

import java.io.*; 

import javax.imageio.ImageIO; 
import javax.swing.JFrame; 

public class Sateen { 

    BufferedImage image; 
    int width; 
    int height; 
    int red, green, blue; 

    public Sateen() { 
     try { 

      File input = new File("n.png"); 
      image = ImageIO.read(input); 
      width = image.getWidth(); 
      height = image.getHeight(); 
      // n is png file with only white pixels 
      for (int i = 0; i < height; i++) { 
       for (int j = 0; j < width; j++) { // may not need j++ as you are doing j+5? 
        Color p = new Color(image.getRGB(j, i)); 
        int jPlus5 = j + 5; 
        if (jPlus5 < width) { 
         Color g = new Color(image.getRGB(jPlus5, i)); 
         // getting (j,i) coordinate pixel value and then 
         // comparing 
         // it to next 5th pixel 

         if (p.getRed() == 255 && p.getBlue() == 255 && p.getGreen() == 255 && g.getRed() == 255 && 
           g.getBlue() == 255 && g.getGreen() == 255) 
         { 
          red = 0; 
          blue = 0; 
          green = 0; 
         } 
         // if both pixel value is white then setting 5th pixel 
         // value 
         // to black 
         Color newColor = new Color(red, green, blue); 
         image.setRGB(jPlus5, i, newColor.getRGB());        
        } 
        j = jPlus5; 
       } 
      } 

      File ouptut = new File("Sateen.png"); 
      ImageIO.write(image, "png", ouptut); 
      // creating sateen png file 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    static public void main(String args[]) throws Exception 
    { 
     Sateen obj = new Sateen(); 
    } 
} 
+0

是其工作,但爲什麼ü使用而不是傳遞J + 5直接在顏色G =新的色彩(image.getRGB(jPlus5,i))的可變jplus5;?當我用J + 5 in for(int j = 0; j musarrat

+0

使用'jPlus5'變量使它更具可讀性,可讓您存儲結果並避免重複。我假設你得到的錯誤是因爲你的代碼中沒有'if(jPlus5 sttaq