【发布时间】:2020-04-10 11:47:28
【问题描述】:
我正在编写一个程序来输入一个包含三个不同字符的 50 x 50 块的文本文件: # , * 和 / ,并根据每个符号为输出 PNG 文件的每个像素着色,以便每个字符都有一种颜色。但是,我遇到了几个与将变量作为参数传递相关的语法错误。下面列出了这些语法错误:
Syntax error on token ",", < expected [21 , 59]
Syntax error, insert "Dimensions" to complete ArrayType [22,5]
Syntax error, insert ">" to complete ReferenceType1 [22,5]
Syntax error on token ",", delete this token [88, 53]
Syntax error, insert "... VariableDeclaratorId" to complete FormalParameter [88,69]
image_width cannot be resolved to a variable [90, 40]
image_height cannot be resolved to a variable [90, 53]
input_file_path cannot be resolved to a variable [92 , 59]
有人可以告诉我如何正确传递这些参数吗?将不胜感激。
这是程序代码:
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 (image_width, image_height, input_file_path, 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);
int x = 0;
int y = 0;
for (x = 0; x < image_width; x++)
{
for (y = 0; y < image_height; y++){
if (characters[x][y] == ('#'))
{r = 255;
b = 0;
g = 0;
}
else if (characters[x][y] == ('/'))
{r = 0;
b = 255;
g = 0;
}
else if (characters[x][y] == ('*'))
{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(image_width, image_height, 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];
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()) {
String s = reader.nextLine(); /* Grab the next line */
for (int i = 0; i < s.toCharArray().length; i++) { /* For each character in the current line */
/* 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; }
reader.close();
return characters; } /* Close the scanner and return the 2-dimensional array */
}
【问题讨论】:
标签: java methods parameters parameter-passing