Draw a black line in java -
below project i'm working on.
the problem in method plotline(). method takes 3 variables , has use these variables draw black line should not go beyond width , length of jlable.
im trying in loop don't know how make de relation between variables , objects in project.
the project runs through class 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 = 0; < 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; <color_array.length-1; i++){ draw(); } } public void draw() { for(int = 0; < 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 right. there no connection between variables in different methods. should connect via parameter or class variable. shouldn't such complicated. 1 method quite enough draw line. here 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() { (int = 0; < color_array.length; i++) { (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(); (int = _x1; < color_array.length - 1 && < _x2; i++) { if (_y >= 0 && _y < color_array[i].length) image.setrgb(i, _y, black_color); } label.seticon(new imageicon(image)); label.repaint(); } }
Comments
Post a Comment