Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

loop

 
tan man
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i created a programe to change the color of the box while the is clicked in the box and outside the box, for this to change colour i have to run the programe several time.

I would like to add a loop code so it can change when i run the programe once.

this is the code i used:

import element.*;
import structure.*;
import java.awt.Color;

public class IfMouse
{
public static void main(String args[])
{
DrawingWindow d = new DrawingWindow();
Rect r = new Rect(40,40,40,40);

Pt p;

r.drawOn(d);
d.awaitMousePress();
d.moveTo(d.getMouse());

p=d.getMouse();

if(r.contains(p))
{
d.setForeground(Color.red);
d.fill(r); // color the circle
}
else if

(d.mousePressed())

d.setForeground(Color.blue);
d.fill(r);


}
}
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

I'm not sure what your DrawingWindow is, but I'm going to guess it is an awt Component or Swing JComponent of some kind. In either event, look at the method addMouseListener(). That will lead you to look at MouseListener. When these are all linked up, a mouse click on the component will fire the mouseClicked() method and you can change color there.

Give this a try using the javadoc and maybe some Swing tutorials on the web. If you get some working code or "almost working" code, bring it back and let us see how you're doing!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic