Move a Graphic in Java,using keyboard arrows and mouseEvent -


i'm beginner in learning this.doing test drawing triangle , want move it.it's not working want ask what's mistake.sorry it's long. import javax.swing.jframe; import javax.swing.jpanel;

import java.awt.color; import java.awt.graphics; import java.awt.event.*;  public class test extends jframe { private triangle triangle; private final int step = 10; private triangle keyboardpanel = new triangle();  public static void main(string[] args) {     test t = new test();  }  public test() {      settitle("try try try");     setlocationrelativeto(null); // center frame     setdefaultcloseoperation(jframe.exit_on_close);     setbounds(100, 100, 500, 500);      jpanel tripanel = new jpanel();      add(tripanel);      triangle = new triangle();       tripanel.addkeylistener(null);     tripanel.addmouselistener(null);     tripanel.addmousemotionlistener(null);      setvisible(true); }     static class move extends jpanel implements keylistener,mouselistener,        mousemotionlistener{      private int x = 210;     private int y = 210;     private color color = color.black;        move()     {      addkeylistener(this);     addmouselistener(this);     addmousemotionlistener(this);     }      @override     public void keypressed(keyevent ke) {         int keycode = ke.getkeycode();         system.out.println("key code is" +keycode);           /*switch (keycode)         {         case keyevent.vk_up:             triangle.movetriangle(-10, 0);             break;         case keyevent.vk_down:             triangle.movetriangle(10, 0);             break;         case keyevent.vk_left:             triangle.movetriangle(0, -10);             break;         case keyevent.vk_right:             triangle.movetriangle(0, 10);             break;          }         repaint();*/      }      @override     public void keyreleased(keyevent arg0) {         // todo auto-generated method stub      }      @override     public void keytyped(keyevent arg0) {         // todo auto-generated method stub      }      @override     public void mouseclicked(mouseevent arg0) {         // todo auto-generated method stub      }      @override     public void mouseentered(mouseevent arg0) {         // todo auto-generated method stub      }      @override     public void mouseexited(mouseevent arg0) {         // todo auto-generated method stub      }      @override     public void mousepressed(mouseevent e) {         system.out.println("hello");         x = e.getx();         y = e.gety();         repaint();     }      @override     public void mousereleased(mouseevent arg0) {         // todo auto-generated method stub     }      @override     public void mousedragged(mouseevent e) {          x = e.getx();         y = e.gety();         system.out.println("hello123");         if(e.iscontroldown())             color = color.red;          else             color = color.black;         repaint();      }      @override     public void mousemoved(mouseevent e) {         // todo auto-generated method stub         system.out.println(e.getpoint());     }  }  public void paint(graphics g) {     super.paint(g);     triangle.drawtriangle(g); }  public void paintcomponent(graphics g) {     super.paintcomponents(g);     triangle.drawtriangle(g); }   }  import java.awt.graphics;  import javax.swing.jpanel;   public class triangle { private point p1; private point p2; private point p3;  int numx; int numy;  public triangle() {      p1 = new point(200,200);     p2 = new point(170,230);     p3 = new point(230,230);  }  public void movetriangle(int dx, int dy) {     p1.move(dx, dy);     p2.move(dx, dy);     p3.move(dx, dy);  }  public void drawtriangle(graphics g) {     g.drawline(p1.getx(), p1.gety(),p2.getx(),p2.gety());     g.drawline(p2.getx(),p2.gety(),p3.getx(),p3.gety());     g.drawline(p3.getx(), p3.gety(),p1.getx(),p1.gety()); } } public class point { private int x; private int y;  public point(int x, int y) {     x = x;     y = y; }  public void setx(int x) {     x = x; }  public void sety(int y) {     x = y; }  public int getx() {     return x; }  public int gety() {     return y; }  public void move(int dx, int dy) {     x +=dx;     y +=dy; }  public string tostring() {     return("x = "+x+" y= "+y); } } 

there lot of problems in code .when coding test current code before go further.

you create panel , add jframe .

jpanel tripanel = new jpanel(); add(tripanel); 

but graphical mechanism has written in move panel need make move panel instead of jpanel.

keylistners has added panel keylisners not triggered because keylistners work when component focused . works component jtextfiled . not panels .you have use keybinding .i have added keybind rightarrow key ..you have add other keys ,down etc..

also move paint method jframe move jpanel .

example code (run code , press right arrow ->)

import java.awt.color; import java.awt.graphics; import java.awt.event.*; import javax.swing.abstractaction; import javax.swing.jcomponent; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.keystroke;  public class test extends jframe {      private triangle triangle;     private final int step = 10;     private triangle keyboardpanel = new triangle();      public static void main(string[] args) {         test t = new test();      }      public test() {          settitle("try try try");         setlocationrelativeto(null); // center frame         setdefaultcloseoperation(jframe.exit_on_close);         setbounds(100, 100, 500, 500);          jpanel tripanel = new move();          add(tripanel);          triangle = new triangle();          setvisible(true);     }      class move extends jpanel implements mouselistener, mousemotionlistener {          private int x = 210;         private int y = 210;         private color color = color.black;          move() {              addmouselistener(move.this);             addmousemotionlistener(move.this);              this.getinputmap(jcomponent.when_in_focused_window).put(keystroke.getkeystroke(keyevent.vk_right, 0), "moveright");             this.getactionmap().put("moveright", new abstractaction() {                 @override                 public void actionperformed(actionevent e) {                     triangle.movetriangle(10, 0);                     repaint();                 }             });          }          @override         public void mouseclicked(mouseevent arg0) {             // todo auto-generated method stub          }          @override         public void mouseentered(mouseevent arg0) {             // todo auto-generated method stub          }          @override         public void mouseexited(mouseevent arg0) {             // todo auto-generated method stub          }          @override         public void mousepressed(mouseevent e) {             system.out.println("hello");             x = e.getx();             y = e.gety();             repaint();         }          @override         public void mousereleased(mouseevent arg0) {             // todo auto-generated method stub         }          @override         public void mousedragged(mouseevent e) {              x = e.getx();             y = e.gety();             system.out.println("hello123");             if (e.iscontroldown()) {                 color = color.red;             } else {                 color = color.black;             }             repaint();          }          @override         public void mousemoved(mouseevent e) {             // todo auto-generated method stub             //system.out.println(e.getpoint());         }          @override         public void paint(graphics g) {             super.paint(g);             triangle.drawtriangle(g);         }      }      class triangle {          private point p1;         private point p2;         private point p3;          int numx;         int numy;          public triangle() {              p1 = new point(200, 200);             p2 = new point(170, 230);             p3 = new point(230, 230);         }          public void movetriangle(int dx, int dy) {             p1.move(dx, dy);             p2.move(dx, dy);             p3.move(dx, dy);          }          public void drawtriangle(graphics g) {             g.drawline(p1.getx(), p1.gety(), p2.getx(), p2.gety());             g.drawline(p2.getx(), p2.gety(), p3.getx(), p3.gety());             g.drawline(p3.getx(), p3.gety(), p1.getx(), p1.gety());         }     }      class point {          private int x;         private int y;          public point(int x, int y) {             x = x;             y = y;         }          public void setx(int x) {             x = x;         }          public void sety(int y) {             x = y;         }          public int getx() {             return x;         }          public int gety() {             return y;         }          public void move(int dx, int dy) {             x += dx;             y += dy;         }          public string tostring() {             return ("x = " + x + " y= " + y);         }     } } 

output >>

enter image description here


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -