Change the color of an oval at click in Java AWT -


i have draw ovals in java, , @ click change color. beginning tried change color after 20 ms, doesn't work.

my code is:

public class mycomponentnew extends frame {      public graphics2d g2d;      public mycomponentnew(string title) {         super(title);         setsize(400, 550);     }      @override     public void paint(graphics g) {         this.g2d = (graphics2d) g;          this.g2d.setcolor(color.red);         this.g2d.filloval(10, 55, 50, 100);     }      public void changecolor () {                 this.g2d.setcolor(color.blue);         this.g2d.filloval(10, 55, 50, 100);     } } 

and in class main method have:

mycomponentnew m;  m = new mycomponentnew("fereastra cu baloane"); m.setvisible(true);  m.addwindowlistener(new windowadapter() {     @override     public void windowclosing(windowevent we) {         system.exit(0);     } });  try {     thread.sleep(20); } catch(interruptedexception e) {}   m.changecolor(); 

the color of oval remains red.

you need @ performing custom painting. kinda have concept, changecolor method isn't going you.

some things note.

  • first need add mouselistener. see more @ how write mouselisteners

  • second want have color color variable. use variable set color. method changecolor should change color , repaint(). like

    public class mycomponentnew extends jpanel {     private color color = color.blue;      protected void paintcomponent(graphics g) {         super.paintcomponent(g);         g.setcolor(color);     }      public void changecolor() {         if (color == color.blue) {             color = color.red:         } else {             color = color.blue;         }         repaint();     } } 
  • third notice how use jpanel instead of jframe. preferred approach.

  • fourth should using swing , not awt.


here's full example, points above

import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.graphics2d; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.geom.ellipse2d;  import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities;  public class circlechangecolor extends jpanel {     private ellipse2d circle = new ellipse2d.double(0, 0, 200, 200);     private color color = color.blue;      public circlechangecolor() {         addmouselistener(new mouseadapter(){             public void mousepressed(mouseevent e) {                 if (circle.contains(e.getpoint())) {                     changecolor();                 }             }         });     }      protected void paintcomponent(graphics g) {         super.paintcomponent(g);         graphics2d g2 = (graphics2d)g;         g2.setcolor(color);         g2.fill(circle);     }      public dimension getpreferredsize() {         return new dimension(200, 200);     }      public void changecolor() {         if (color == color.blue) {             color = color.red;         } else {             color = color.blue;         }         repaint();     }      public static void main(string[] args) {         swingutilities.invokelater(new runnable(){             public void run() {                 jframe frame = new jframe();                 frame.add(new circlechangecolor());                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 frame.pack();                 frame.setvisible(true);             }         });      } } 

Comments

Popular posts from this blog

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -