3

下面是我正在从事的项目。

问题出在方法 Plotline() 中。该方法需要三个变量,并且必须使用这些变量来绘制一条不应超过 JLable 宽度和长度的黑线。

我试图在 for 循环中做到这一点,但我不知道如何在这个项目中的变量和对象之间建立关系。

该项目通过另一个类是 NewJFrame.java

import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class Image {
  private JLabel          label;
  private BufferedImage   image;
  private Color           color;
  private Color[][]       color_array;
  private Color[][]       temp_array;

public Image(JLabel _label, Color _color)
{
    label = _label;
    image           = new bufferedImage(label.getHeight(),label.getWidth(),BufferedImage.TYPE_INT_ARGB);
    color_array     = new Color[label.getWidth()][label.getHeight()];       
    color = _color;
    Background();
    Draw();
}

public void Background()
{
    for(int i = 0; i < color_array.length ; i++)
        for(int j = 0; j < color_array[i].length; j++)
            color_array[i][j] = color;

}
public void Plotline(int _x1, int _x2, int _y)
{
    Color tmp_color = new Color(0);
    for(int i=0; i <color_array.length-1; i++){
        Draw();
    }

}

public void Draw()
{
    for(int i = 0; i < color_array.length ; i++)
        for(int j = 0; j < color_array.length; j++)
            image.setRGB(i, j, color_array[i][j].getRGB());
    label.setIcon(new ImageIcon(image));
    label.repaint();
}
}
4

1 回答 1

2

你说的对。不同方法中的变量之间没有联系。它们应该通过一些参数或类变量连接。其实不应该这么复杂。一种方法足以划清界限。这是固定代码:

import java.awt.Color;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Test {

    public Test() {
        JFrame frame = new JFrame("Test");
        JLabel label = new JLabel("Hello, World!");
        frame.add(label);

        frame.setSize(300, 200);
        frame.setVisible(true);

        new Image(label, Color.BLACK).Plotline(10, 90, 100);
        frame.repaint();
    }

    public static void main(String a[]) {
        new Test();
    }
}

// /
class Image {
    private JLabel label;
    private BufferedImage image;
    private Color color;
    private Color[][] color_array;
    private Color[][] temp_array;

    public Image(JLabel _label, Color _color) {
        label = _label;
        image = new BufferedImage(label.getHeight(), label.getWidth(),
                BufferedImage.TYPE_INT_ARGB);
        color_array = new Color[label.getWidth()][label.getHeight()];
        color = _color;
        Background();
    }

    public void Background() {
        for (int i = 0; i < color_array.length; i++) {
            for (int j = 0; j < color_array[i].length; j++) {
                color_array[i][j] = color;
            }
        }
    }

    public void Plotline(int _x1, int _x2, int _y) {
        int black_color = new Color(0).getRGB();
        for (int i = _x1; i < color_array.length - 1 && i < _x2; i++) {
            if (_y >= 0 && _y < color_array[i].length)
                image.setRGB(i, _y, black_color);
        }
        label.setIcon(new ImageIcon(image));
        label.repaint();
    }
}
于 2015-05-21T18:27:32.553 回答