无法在原始类型char上调用equals(char)

时间:2019-12-17 16:04:58

标签: java image multidimensional-array methods char

我正在编写一个程序,以输入一个文本文件,其中包含一个50 x 50的三个不同字符的块:#,*和/,并根据每个符号为输出PNG文件的每个像素着色,以便每个字符都有一种颜色。但是,我在第40、46和52行遇到错误,该错误是程序 “无法在原始类型char上调用equals(char)” 。为什么会这样?

这三个语句的目的是说“对于2d数组中具有此字符的任何位置,请将其设置为该RGB值”。这是下面的代码。如有必要,我还可以提供读入的文本文件来创建数组。

import java.util.Scanner;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter; 

public class Lab_Week8_ImgFrmTxt_Part2 {

    public static void main(String[] args) throws Exception  {  

    int image_width = 50;
    int image_height = 50;
    String output_file_path = ("image.png");
    String input_file_path = "superimage.txt";

    }

    public static BufferedImage imageFromText (int image_width, int image_height, String input_file_path, String output_file_path, 
    int r, int g, int b, char[][] characters) throws IOException{

        if(image_width <= 0 || image_height <= 0) {
            System.err.println("Width and Height have to be strictly positive!");
            return null;
        }

        BufferedImage image = new BufferedImage (image_width, image_height, BufferedImage.TYPE_INT_ARGB);

        BufferedImage imagetowrite = imageFromText(image_width, image_height, input_file_path, output_file_path, r, g, b, characters); 
        File f = new File (output_file_path);
        ImageIO.write(imagetowrite, "png", f);

        System.out.println(characters);

        int x = 0;
        int y = 0;
        for (x = 0; x < image_width; x++)
        {
        for (y = 0; y < image_height; y++){
            if (characters[x][y].equals ('#'))
            {r = 255;
            b = 0;
            g = 0;
            }

            else if (characters[x][y].equals ('/'))
            {r = 0;
            b = 255;
            g = 0;
            }

            else if (characters[x][y].equals ('*'))
            {r = 0;
            b = 0;
            g = 255;
            }
            setPixel(image, x, y, r, g, b); }
        }
        return image;
    }

    public static void setPixel(BufferedImage image, int x, int y, int r, int g, int b) {

        if(x < 0 || y < 0) {
            System.err.println("Coordinates (x, y) cannot be negative numbers!");
            return;
        }
        if(r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {
            System.err.println("Colour values (r, g, b) have to be between 0 and 255!");
            return;
        }

        int a = 255; //alpha value
        /*
         * Write the different value of the pixel, i.e. the colours red, green, blue and alpha.
         * The different colour values are all stored into a single integer using byte operators.
         * a = x << y means write the value of x as bytes at the address y in the object a.
         * a = o1 | o2 means a will be composed by o1 and o2, where o1 and o2 are binary operators.
         * It is necessary to use this operator because the setRGB method of BufferedImage 
         * take only one integer that have to hold all the colour values for one given pixel.
         */
        int p = (a << 24) | (r << 16) | (g << 8) | b;

        image.setRGB(x, y, p);
    }

    public static char[][] readTextImage(int image_width, int image_height, String input_file_path) throws FileNotFoundException {  
        /* Create a 2-dimensional array of characters to store the data from the file */  
        char[][] characters = new char[image_width][image_height]; 

        /* Create a scanner to read the file given in parameter */  
        Scanner reader = new Scanner (new FileInputStream(input_file_path));

        /* Set x and y variable at 0 to keep track of the coordinates */  
        int x = 0; 
        int y = 0;  
        /* While there is something to read in the file */  
        while (reader.hasNextLine()) {   
            /* Grab the next line */   
            String s = reader.nextLine(); 
            /* For each character in the current line */   
            for (int i = 0; i < s.toCharArray().length; i++) {    
                /* Get the current character (use s.toCharArray and the index i to access the right element */    
                char c = reader.next().charAt(0);
                /* Fill the 2-dimentional array of character with c at x and y coordinates */    
                characters [x][y] = c;
                /* Update the x coordinate */    
                x += 1;   }   
                /* Reset the x coordinate and update the y coordinate */   
                x = 0;   
                y += 1;  } 

        /* Close the scanner and return the 2-dimensional array */ 

        reader.close();  
        return characters; }   

}

0 个答案:

没有答案