1

我正在尝试在 JPanel 上呈现动态变化的图像。我已经尝试了很多事情,但我根本不知道该怎么做或者我做错了什么,我快要发疯了。我是摇摆新手,这是我程序的一个小而重要的部分,它使我能够看到我在做什么。

简单的要求是,在下面提到的代码中最初在 JPanel 中呈现的图片应该在每个刻度处更新为随机颜色。我无法让图像更新。相反,我正在尝试更新的图像中间出现另一个正方形,并且那个小正方形会动态变化。我不知道我做错了什么。请让我知道如何更正下面的代码,以便我可以更新整个图像。

谢谢你。

代码如下

(MainTest.java)

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.Timer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

public class MainTest extends JFrame {
static ImageEditor img ;
JPanel panel;
Timer to;

public MainTest()
{
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(265, 329);
    getContentPane().setLayout(null);
    img =  new ImageEditor();
    panel = new ImageEditor();
    panel.setBounds(10, 11, 152, 151);
    panel.add(img);
    getContentPane().add(panel);

    JButton btnIterate = new JButton("Iterate");
    btnIterate.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {


            ThirdClass t = new ThirdClass(img);
            to = new Timer(10,t);
            to.start();             
        }

    });
    btnIterate.setBounds(10, 230, 89, 23);
    getContentPane().add(btnIterate);

}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            MainTest frame = new MainTest();
            //frame.getContentPane().add(imgx);
            frame.setVisible(true);
        }
    });

}
}

(ImageEditor.java)
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

@SuppressWarnings("serial")
public class ImageEditor extends JPanel {

private static BufferedImage img = new BufferedImage(100, 100, 1);  
public ImageEditor() {
    render();
}

public void paintComponent(Graphics g) {
    if (img == null)
        super.paintComponents(g);
    else
        g.drawImage(img, 0, 0, this);
}


public void render() {

float cellWidth = 10;
float cellHeight = 10;

    int imgW = img.getWidth();
    int imgH = img.getHeight();
    float r, g, b;
    Graphics2D g2 = img.createGraphics();
    g2.setBackground(Color.black);
    g2.clearRect(0,0,imgW,imgH);
    for (int x=0; x<100; x++) {
        for (int y=0; y<100; y++) {
            r = (float)Math.random();
            g = (float)Math.random();
            b = (float)Math.random();
            g2.setColor(new Color(r,g,b));
            g2.fillRect((int)(x*cellWidth), (int)(y*cellHeight),
                        (int)cellWidth+1, (int)cellHeight+1);
        }
    }
    g2.setColor(Color.black);
    g2.dispose();
    repaint();
}

public BufferedImage getImage() {
    if (img == null)
        img = (BufferedImage)createImage(100, 100);

    return img;
}

public void setImage(BufferedImage bimg) {
    img = bimg;
}
}


(ThirdClass.java)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ThirdClass implements ActionListener {

static ImageEditor mock;

public ThirdClass(ImageEditor img)
{
    mock = img;
    Train();
}

public void Train()
{
    mock.render();
}

@Override
public void actionPerformed(ActionEvent arg0) {
    Train();
}

    }
4

2 回答 2

2

从代码:

img =  new ImageEditor();
panel = new ImageEditor();
panel.setBounds(10, 11, 152, 151);
panel.add(img);
getContentPane().add(panel);

您将其定义imgImageEditor,但您可能打算将其定义为BufferedImage. 然后,您将它添加到panel另一个ImageEditor- 但它是通过超类方法添加的JPanel.add,当您可能打算使用您编写的方法时,ImageEditor.setImage()

编辑:总结一下:

您描述的更新像素的小块是BufferedImage( img.img),位于您的 内部ImageEditor img,而 又位于 内部ImageEditor panel,后者本身位于小程序的内容窗格中。

  • static ImageEditor imgMainTest课堂上替换为BufferedImage img

  • img = new ImageEditorinMainTest的无参数构造函数替换为img = new BufferedImage()

  • panel.add(img)inMainTest的无参数构造函数替换为panel.setImage(img)

  • static ImageEditor mockThirdClass课堂上替换为BufferedImage mock

  • 将的构造ImageEditor img函数中的参数替换为ThirdClassBufferedImage img

  • 替换任何其他涉及img正确处理它的东西BufferedImage

另一件事,虽然这个问题本身并不是任何程序故障的原因,但您应该将Train()方法重命名ThirdClass为类似的名称train(),以便它匹配正确的 java 样式。

于 2013-06-19T20:06:25.037 回答
0

在我最初发布上述问题之后,我回到了基础知识并设法回答了我自己的问题。

我在下面插入了完整的代码,这样另一个有同样问题的用户就不必再经历同样的过程了。

我的错误非常微不足道。但是对于开始摇摆的人,我认为这可能会有所帮助

(MainTest.java)
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.Timer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainTest extends JFrame { 

ImageEditor img;
JPanel panel;
Timer t;

public MainTest()
{
    setSize(600,600);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    getContentPane().setLayout(null);

    img = new ImageEditor();
    panel = img;
    panel.setBounds(0, 0, 510, 510);
    getContentPane().add(panel);

    JButton btnStart = new JButton("Start");
    btnStart.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            test();
        }
    });
    btnStart.setBounds(0, 528, 89, 23);
    getContentPane().add(btnStart);
}

private void test() {
    Train tx = new Train(img);
    t = new Timer(100, tx);
    t.start();
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            MainTest frame = new MainTest();
            frame.setVisible(true);
        }
    });

  }

}

(ImageEditor.java)
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

@SuppressWarnings("serial")
public class ImageEditor extends JPanel  {

private static BufferedImage img = new BufferedImage(500, 500, 1);  

public ImageEditor() {
    setLayout(null);
}

public void paintComponent(Graphics g) {
    if (img == null)
        super.paintComponents(g);
    else
        g.drawImage(img, 0, 0, this);
}

public void render() {

float cellWidth = 10;
float cellHeight = 10;

    int imgW = img.getWidth();
    int imgH = img.getHeight();
    float r, g, b;
    Graphics2D g2 = img.createGraphics();
    g2.setBackground(Color.black);
    g2.clearRect(0,0,imgW,imgH);
    for (int x=0; x<100; x++) {
        for (int y=0; y<100; y++) {
            r = (float)Math.random();
            g = (float)Math.random();
            b = (float)Math.random();
            g2.setColor(new Color(r,g,b));
            g2.fillRect((int)(x*cellWidth), (int)(y*cellHeight),
                        (int)cellWidth+1, (int)cellHeight+1);
        }
    }
    g2.setColor(Color.black);
    g2.dispose();
    repaint();
}

public BufferedImage getImage() {
    if (img == null)
        img = (BufferedImage)createImage(500, 500);

    return img;
}

public void setImage(BufferedImage bimg) {
    img = bimg;
}

}

(Train.java this class was named ThirdClass.java in my question)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Train implements ActionListener {

ImageEditor temp;

Train(ImageEditor img)
{
    temp = img;
}

public void train()
{
    temp.render();  
}

@Override
public void actionPerformed(ActionEvent e) {
    train();
}

}

感谢所有评论并回答我问题的人。

于 2013-06-21T05:26:49.400 回答