无法以联觉方式绘制像素、Pi 数
我想将 pi 数字的每个数字打印为彩色像素,因此,我得到一个带有 pi 数字的输入,然后将其解析为一个列表,每个节点包含一个数字(我知道,稍后我将使用一个数组) ,但我从来没有把它画到屏幕上......有人能帮我看看我错在哪里吗?
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.MemoryImageSource;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PiPainter extends JPanel
{
private static final long serialVersionUID = 6416932054834995251L;
private static int pixels[];
private static List<Integer> pi = new ArrayList<Integer>();
private final static int[] color = {
0xFF000000, 0xFF787878, 0xFF008B00, 0xFF00008B, 0xFF008B8B,
0xFF008B00, 0xFFCDCD00, 0xFFFF4500, 0xFF8B0000, 0xFFFF0000
};
public static void readFile(String name)
{
File file = new File(name);
BufferedReader reader = null;
char[] digits;
try
{
reader = new BufferedReader(new FileReader(file));
String text = null;
while((text = reader.readLine()) != null)
{
digits = text.toCharArray();
for(char el : digits)
if(el != ' ')
pi.add(Character.getNumericValue(el));
}
} catch (Exception e)
{
e.printStackTrace();
}
}
public void paint(Graphics gg)
{
// page containing pi number, http://gc3.net84.net/pi.htm
// other source, http://newton.ex.ac.uk/research/qsystems/collabs/pi/pi6.txt
readFile("c:\\pi.txt");
int h = 300;
int w = 300;
int digit;
int i = 0;
pixels = new int[w * h];
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
pixels[i] = color[pi.get(i)];
i++;
}
}
Image art = createImage(new MemoryImageSource(w, h, pixels, 0, w));
gg.drawImage(art, 0, 0, this);
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.getContentPane().add(new PiPainter(), BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
}
}
I want to print each digit of pi number as a colored pixel, so, I get an input, with the pi number, then parse it into a list, each node containing a digit (I know, I'll use an array later), but I never get this painted to screen... Can someone help me to see where I'm wrong?
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.MemoryImageSource;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PiPainter extends JPanel
{
private static final long serialVersionUID = 6416932054834995251L;
private static int pixels[];
private static List<Integer> pi = new ArrayList<Integer>();
private final static int[] color = {
0xFF000000, 0xFF787878, 0xFF008B00, 0xFF00008B, 0xFF008B8B,
0xFF008B00, 0xFFCDCD00, 0xFFFF4500, 0xFF8B0000, 0xFFFF0000
};
public static void readFile(String name)
{
File file = new File(name);
BufferedReader reader = null;
char[] digits;
try
{
reader = new BufferedReader(new FileReader(file));
String text = null;
while((text = reader.readLine()) != null)
{
digits = text.toCharArray();
for(char el : digits)
if(el != ' ')
pi.add(Character.getNumericValue(el));
}
} catch (Exception e)
{
e.printStackTrace();
}
}
public void paint(Graphics gg)
{
// page containing pi number, http://gc3.net84.net/pi.htm
// other source, http://newton.ex.ac.uk/research/qsystems/collabs/pi/pi6.txt
readFile("c:\\pi.txt");
int h = 300;
int w = 300;
int digit;
int i = 0;
pixels = new int[w * h];
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
pixels[i] = color[pi.get(i)];
i++;
}
}
Image art = createImage(new MemoryImageSource(w, h, pixels, 0, w));
gg.drawImage(art, 0, 0, this);
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.getContentPane().add(new PiPainter(), BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不熟悉
MemoryImageSource
。这是 π 的前16300 位数字,在BufferedImage
中重复并使用您的颜色表。I'm not familiar with
MemoryImageSource
. Here's the first16300 digits of π, repeated in aBufferedImage
and using your color table.@trashgod
感谢您的回答,我对其进行了一些更改以实现我想要的目标; )
现在您可以轻松更改宽度,以更好地查看图像并适合内容,并且数字不会重复,从而可以轻松感知图案(如果可能有)。哦,我在最后添加了大约 2~3 行,以澄清它。
@trashgod
Thanks for your answer, I changed it a little bit to achieve what I was looking for ; )
Now you can change the width easily to achieve a better view of the image and fit the contents, and the number don't repeats, making it easy to perceive patterns (if there might be). Oh, and I added about 2~3 lines at the end, to clarify it.