简体   繁体   中英

Draw a black line in java

Below is the project where I'm working on.

The problem is in the method Plotline(). This method takes three variables and has to use these variables to draw a black line which should not go beyond the width and the length of the JLable.

Im trying to do it in a for loop but i don't know how to make de relation between variables and the objects in this project.

The project runs through another class that is 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();
}
}

You are right. There is no connection between your variables in different methods. They should connect via some parameter or class variable. Actually it shouldn't be such complicated. One method is quite enough to draw your line. Here is fixed code:

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM