BufferedImage setRGB不接受给定的值

时间:2014-05-27 02:57:31

标签: java swing rgb bufferedimage

我尝试使用setRGB绘制图像,然后从文本文件中获取RGB值,但图像始终显示为黑色。但是,如果我提供自己的价值观,那就有效。

这里的代码总是用黑色绘制:

public JMenuBar menubar;
public JMenuItem importFile;
public Scanner filePath;
StringTokenizer tokens = null;
BufferedImage image;
int[][][] images;
JPanel panelDown = new JPanel();
MyPanel myPanel;
Color myColor;

int[] coords = new int[2];
int i = 0, j = 0;

public class MyPanel extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        //g.setColor(myColor);
        g.drawImage(image, 0, 0, null);
        //g.drawRect(10, 10, 40, 50);
    }
}

public Window2() {
    super("Exercise 2");
    menubar = new JMenuBar();
    setLayout(new BorderLayout());
    JPanel panelBar = new JPanel();
    add(panelBar, BorderLayout.NORTH);
    add(panelDown, BorderLayout.CENTER);
    importFile = new JMenuItem("Import", 'I');
    panelBar.add(menubar);
    menubar.add(importFile);
    importFile.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            FileDialog fd = new FileDialog(Window2.this, "Import Maze", FileDialog.LOAD);
            fd.setFile(".txt");
            fd.setLocation(Window2.this.getX() + 100, Window2.this.getY() + 100);
            fd.show();
            if (!fd.getFile().endsWith(".txt")) {
                JOptionPane.showMessageDialog(Window2.this, "Wrong file extension", "Error", JOptionPane.WARNING_MESSAGE);
            } else {
                File theFile = new File(fd.getDirectory() + "\\" + fd.getFile());
                try {
                    filePath = new Scanner(theFile);
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(Window2.class.getName()).log(Level.SEVERE, null, ex);
                }
                images = readFile(filePath);
                panelDown.setLayout(new GridLayout(coords[0], coords[1]));
                for (int k = 0; k < coords[0]; k++) {
                    for (int l = 0; l < coords[1]; l++) {
                        int red = images[k][l][0];
                        int green = images[k][l][1];
                        int blue = images[k][l][2];
                        myPanel = new MyPanel();
                        panelDown.add(myPanel);
                        image = new BufferedImage(30, 30, BufferedImage.TYPE_INT_RGB);
                        myColor = new Color(red,green,blue,255);
                        for (int i = 0; i < 30; i++) {
                            for (int j = 0; j < 30; j++) {
                                image.setRGB(i, j, myColor.getRGB());
                            }
                        }
                        myPanel.repaint();
                    }
                }
            }
        }
    });

    setSize(500, 500);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

images数组将这些值存储在文本文件中的3个集合中:

255   0   0     0 255   0     0   0 255
255 255   0   255 255 255     0   0   0

如果我改变了这个:

int red = 180;
int green = 50;
int blue = 170;
它用另一种颜色涂料。

代码有什么不对,它不接受文本文件中的值?

1 个答案:

答案 0 :(得分:2)

您在myPanel.repaint()k的循环中反复呼叫limage每次设置为BufferedImage个{{1}} 。这样做的结果是面板最终会显示您创建的最后一个图像。

在这种特殊情况下,您创建的最后一张图片是全黑的。