0

Situation: So I am calling the method draw from my paint method. However, I only want it to draw if the convert button is clicked. How do I tell java to not draw3Drectangle unless (ae.getSource==convert)?? I am new to GUI as you can probably tell, so simple answers please. Appreciate any help.

CODE:

public class simpgui extends Applet implements ActionListener 
{ 

  Button convert;

  Label celsius;

  Label farenheit;

  TextField cels;

  TextField fare; 

  String message = "";

  public void init()
  {

    convert = new Button("Convert");

    celsius = new Label("Celsius");

    farenheit = new Label("Farenheit");

    cels = new TextField(15);

    fare = new TextField(15);


    add(convert);

    add(celsius);

    add(cels);

    add(farenheit);

    add(fare);

    convert.addActionListener(this);

  }

  public void paint(Graphics g)
  {

    fare.setLocation(160,50);

    farenheit.setLocation(90,50);

    convert.setLocation(310,5);

    draw(g);

  }

  public void actionPerformed (ActionEvent ae)
  {

    if(ae.getSource() == convert)

    {

        int farenheit = (int) ((Double.parseDouble(cels.getText())) * (1.8)) + 32;


        fare.setText(farenheit+"");
    }

  } 

  public static void draw(Graphics g)
  {
    g.setColor(Color.BLUE);
    g.fill3DRect(0,0,400,100,true);
  }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
imgolden62
  • 396
  • 2
  • 5
  • 14
  • Don't forget to call `super.paint` before you do any actual painting... – MadProgrammer Dec 02 '14 at 04:30
  • Interesting.. what exactly is the use of super.paint and where should I put it? Thank you. @MadProgrammer – imgolden62 Dec 02 '14 at 13:10
  • Paint in responsible for, amongst other things, preparing the Graphics state for painting, painting the border of the component, background, child components and you should call it first, before you do any custom painting – MadProgrammer Dec 02 '14 at 18:53
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Dec 02 '14 at 23:08
  • @AndrewThompson Thank you Andrew. I took into consideration what you said. This is indeed for my Comp. Sci Teacher, however, I have started to research basic Swing gui programs. Seems simple enough. – imgolden62 Dec 03 '14 at 01:59

1 Answers1

1

Methods can't be "disabled" but you can use a boolean variable and an if statement to achieve the same functionality:

 boolean isClicked = false;

 public void paint(Graphics g)
 {
    super.paint();
    fare.setLocation(160,50);
    farenheit.setLocation(90,50);
    convert.setLocation(310,5);
    draw(g);
 }

 public void actionPerformed(ActionEvent ae)
 {
      if (ae.getSource() == convert)
      {
           isClicked = true;
      }
 }

 public void draw(Graphics g)
 {
     if (isClicked)
     {
        g.setColor(Color.BLUE);
        g.fill3DRect(0,0,400,100,true);
     }
 }
mdnghtblue
  • 1,117
  • 8
  • 27
  • non-static variable drawswitch cannot be referenced from a static context – imgolden62 Dec 02 '14 at 13:10
  • for a more intuitional use, throwing an `IllegalStateException` would probably make more sense when a method is "disabled". of course this depends on how a disabled element should behave. – Olavi Mustanoja Dec 02 '14 at 14:04
  • @OlaviMustanoja It would if you wanted to signal an implementation error, but the OP wants this method to be "silently" disabled, or disabled in an acceptable state. – mdnghtblue Dec 02 '14 at 14:06
  • @imgolden62 I updated my post. Either both the method and variable need to be static, or neither should be static, so I removed the static from your `draw()` method. I also included an example of `super.paint()` per @MadProgrammer's comment. – mdnghtblue Dec 02 '14 at 14:08
  • @mdnghtblue that's very much true. with buttons it's probably standard too to simply "do nothing" if it's disabled. throwing an exception provides a handy way to do something additional when a disabled button is pressed, though – Olavi Mustanoja Dec 02 '14 at 14:09
  • Thanks guys. You both helped me learn more about GUI that I did not fully understand. However, with the code that you have suggested, I get "Process Completed", however, when I click convert, the rectangle isn't drawn (draw() is not 'activated'?). Also, when I add in super.paint(), java says it cannot find the paint method. Thanks in advance. @mdnghtblue – imgolden62 Dec 02 '14 at 21:52