Java drawLine command doesn't draw on screen -
so making paint program draws imputing commands irc server chat room. drawing works , checked (you can see checking system out :p) drawline() command doesn't thing. please help!
ps:this class file in project, else works sending here trust me!
code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; class paddraw extends jcomponent { //this gonna image draw on image image; //this we'll using draw on graphics2d graphics2d; //these gonna hold our coordinates int currentx = 0, currenty = 0, oldx = 0, oldy = 0; public paddraw() { } public void oncommand(string msg) { if(msg.equalsignorecase("up")) { if(oldy > 0) { currenty = currenty - 30; if(graphics2d != null) graphics2d.drawline(oldx, oldy, currentx, currenty); repaint(); oldx = currentx; oldy = currenty; } } if(msg.equalsignorecase("down")) { if(oldy < 861) { system.out.println(msg); system.out.println("starting y: " + currenty); currenty = currenty + 30; system.out.println("ending y: " + currenty); if(graphics2d != null) graphics2d.drawline(oldx, oldy, currentx, currenty); repaint(); oldx = currentx; oldy = currenty; system.out.println("old y: " + oldy); } } if(msg.equalsignorecase("left")) { if(oldx > 0) { currentx = currentx - 30; if(graphics2d != null) graphics2d.drawline(oldx, oldy, currentx, currenty); repaint(); oldx = currentx; oldy = currenty; } } if(msg.equalsignorecase("right")) { if(oldx < 847) { currentx = currentx + 30; if(graphics2d != null){ graphics2d.drawline(oldx, oldy, currentx, currenty); repaint(); } oldx = currentx; oldy = currenty; } } } public void paintcomponent(graphics g) { if(image == null) { image = createimage(getsize().width, getsize().height); graphics2d = (graphics2d)image.getgraphics(); graphics2d.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); clear(); } g.drawimage(image, 0, 0, null); }
you forgetting call
super.paintcomponentinsidepaintcomponentmethod. people wrongly leave out on purpose thinking right way achieve "continous" painting, doing breaking paint chain, , actually seeing paint artifacts, lead believe there's continuous paint functionality.the way doing not produce "continous" painting. one, you're not drawing line
jcomponentgraphics context. second, once callsuper.paintcomponentinsidepaintcomponentmethod, lose previous painting
here's could/should do, instead...
have list of
line2dobjects.list<line2d> lines = new arraylist<>();loop through
listinpaintcomponentmethod, , draw them@override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2 = (graphics2d)g; ... (line2d line : lines) { g2.draw(line); } }oh btw,
paintcomponentshouldprotected, notpublicwhen ever want add line drawing, add
line2dobject list ,repaint()lines.add(new line2d.double(x1, y1, x2, y2)); repaint();
see more @ line2d api , graphics2d tutorial
Comments
Post a Comment