gpt4 book ai didi

java - 无法以联觉方式绘制像素、Pi 数

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:58:32 24 4
gpt4 key购买 nike

我想将 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);
}
}

最佳答案

我不熟悉 MemoryImageSource。这是 π 的前 16 300 位,在 BufferedImage 中重复并使用您的颜色表。

Pi.png

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class PiRaster extends JPanel {

private static final int W = 30;
private static final int H = 30;
private static List<Integer> pi = new ArrayList<Integer>();
BufferedImage image;
private int[] clut = {
0x000000, 0x787878, 0x008B00, 0x00008B, 0x008B8B,
0x008B00, 0xCDCD00, 0xFF4500, 0x8B0000, 0xFF0000
};

public PiRaster() {
this.setPreferredSize(new Dimension(W * 16, H * 10));
String s = ""
+ "31415926535897932384626433832795028841971693993751"
+ "05820974944592307816406286208998628034825342117067"
+ "98214808651328230664709384460955058223172535940812"
+ "84811174502841027019385211055596446229489549303819"
+ "64428810975665933446128475648233786783165271201909"
+ "14564856692346034861045432664821339360726024914127";
for (int i = 0; i < s.length(); i++) {
pi.add(s.charAt(i) - '0');
}
}

@Override
public void paintComponent(Graphics g) {
if (image == null) {
image = (BufferedImage) createImage(W, H);
int i = 0;
for (int row = 0; row < H; row++) {
for (int col = 0; col < W; col++) {
image.setRGB(col, row, clut[pi.get(i)]);
if (++i == pi.size()) {
i = 0;
}
}
}
}
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new PiRaster());
frame.pack();
frame.setVisible(true);
}
});
}
}

关于java - 无法以联觉方式绘制像素、Pi 数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2626731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com