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

  1. you forgetting call super.paintcomponent inside paintcomponent method. 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.

  2. the way doing not produce "continous" painting. one, you're not drawing line jcomponent graphics context. second, once call super.paintcomponent inside paintcomponent method, lose previous painting

here's could/should do, instead...

  1. have list of line2d objects.

    list<line2d> lines = new arraylist<>(); 
  2. loop through list in paintcomponent method, , draw them

    @override protected void paintcomponent(graphics g) {     super.paintcomponent(g);     graphics2d g2 = (graphics2d)g;     ...     (line2d line : lines) {         g2.draw(line);     } } 

    oh btw, paintcomponent should protected , not public

  3. when ever want add line drawing, add line2d object list , repaint()

    lines.add(new line2d.double(x1, y1, x2, y2)); repaint(); 

see more @ line2d api , graphics2d tutorial


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -