简体   繁体   English

(Java)通过从充满数字的文本文件中读取来生成图像

[英](Java) Producing image by reading from a text-file filled with digits

I'm a complete beginner in Java programming and I'm interested to learn more about its concepts. 我是Java编程的完整入门者,并且有兴趣了解有关其概念的更多信息。

Recently, I've been given an exercise which instructs me to display two versions of a picture. 最近,我进行了一个练习,指示我显示一张图片的两个版本。 The picture to be displayed is provided in the form of a data file of 40,000 digits that are arranged in rows (although there is no marker between rows) and it starts from the top of the picture. 要显示的图片以40,000位数据文件的形式提供,该文件以行排列(尽管行之间没有标记),并且它从图片的顶部开始。 So the first digit represents the top left corner of the picture and the last is the bottom right. 因此,第一个数字代表图片的左上角,最后一个数字代表右下角。

Basically, what the exercise wants me to construct a program that plots a dot in one of two colours for each digit. 基本上,练习需要我构造一个程序,为每个数字以两种颜色之一绘制一个点。 If the digit is in the range 0 to 3 the output should be one colour and for digits in the range 4 to 9 the dot should be in the other colour. 如果数字在0到3范围内,则输出应为一种颜色,对于4到9范围内的数字,点应为另一种颜色。

I understand I have to use arrays and also loops to perform this. 我知道我必须使用数组,还必须循环执行此操作。 I'm familiar with the fillEllipse, drawEllipse, drawRectangle and fillRectangle but this exercise is nothing I've attempted before. 我对fillEllipse,drawEllipse,drawRectangle和fillRectangle很熟悉,但是我没有尝试过此练习。

Any hints on how to make this work? 关于如何进行这项工作的任何提示? Your help would be greatly appreciated. 您的帮助将不胜感激。

As a hint, rather than a complete solution, I would suggest looking into creating a java.awt.image.BufferedImage , and then set the colors of the individual pixels using the setRGB() method. 作为提示,而不是完整的解决方案,我建议您考虑创建一个java.awt.image.BufferedImage ,然后使用setRGB()方法设置各个像素的颜色。 You would then display this image using drawImage() on your Graphics object. 然后,您将在Graphics对象上使用drawImage()显示此图像。

All what you need is how to read the digits from the file and put it into two dimension array 您所需要的就是如何从文件中读取数字并将其放入二维数组中

check this tutorial for how to read a file 查看本教程, 了解如何读取文件

Then you have to draw each pixel on a fram or a Panel 然后,您必须在框架或面板上绘制每个像素

check this Java basic graphics tutorial 查看此Java基本图形教程

I hope this could help! 希望对您有所帮助!

use Scanner to read the data like : 使用扫描仪读取数据,例如:

Scanner sc = new Scanner(new File("path_to_your_digits_file"));
int[][] digits= new int [200][200];
String line;
while(sc.hasNext()){//this means there is still a line to go
  line = sc.nextLine();
  //split the line and fill the array . or read char by char ,
  // i dont know how your file looks like, it's just some simple string manipulation here
  }
  int x;
  BufferedImage img = new BufferedImage(200,200,BufferedImage.TYPR_INT_RGB);
  for(int i=0;i<200;i++){
    for(int j=0;i<200;j++){
    if(digits[i][j]>0 && digits[i][j]<=3){
      x=//some color code;
     }else{
         x=//some other color code;
      } //not sure about this loop , again idk the structure of your file;
      img.setRGB(i,j,x);
     }
   }
      JLabel lbl = new JLabel();
      lbl.setSize(200,200);
      ImageIcon ico=new ImageIcon(img);
      lbl.setIcone(ico);
      lbl.setVisible(true);
      JFrame frame = new Jframe();
      frame.setSize(500,500);
      frame.add(lbl);
      frame.setVisible(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM