0

here is what I wanted to do. There is a textfield and user enters what he wants. For example "Rectangle" or "rectangle", "circle" or "CIRCLE" like that. And then the user presses the button. After that program draws the shape that user wrote down below. I couldn't use the "paint" function itself. It got worse somehow. So I used "paintRec" etc. But I think that's not true according to OOP. So please show me the legit way to solve this question. There are a lot of wrong coding down there. That's for sure. Show me how can I do this. Where am I doing wrong. Thanks.

public class extends Applet implements ActionListener{
TextField tf;
Button draw;

public void init(){
    tf = new TextField(10);
    draw = new Button("Draw");
    draw.addActionListener(this);
    add(tf);
    add(draw);
}

public void actionPerformed(ActionEvent e) {
    String shape = tf.getText();
    if (shape.equals("rectangle") || shape.equals("RECTANGLE"))
    {
        paintRec(null);
    }
    if (shape.equals("circle") || shape.equals("CIRCLE"))
    {
        paintCirc(null);
    }
}

public void paintRec(Graphics g){
    g.drawRect(30,30,50,60);
}
public void paintCirc(Graphics g){
    g.drawOval(30, 30, 50, 60);
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ufuk Ozdogan
  • 13
  • 1
  • 8
  • Well first off you don't even have a `class name` – 3kings Apr 28 '16 at 16:15
  • 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 Apr 29 '16 at 03:02
  • @Andrew Yes it is. Thanks so much for the information. I just love learning more and more. I'll definetely take a look at those links. And send them to my teacher too. – Ufuk Ozdogan Apr 29 '16 at 08:27

1 Answers1

1

The problem lies at here:

public void actionPerformed(ActionEvent e) {
  String shape = tf.getText();
  if (shape.equals("rectangle") || shape.equals("RECTANGLE"))
  {
    paintRec(null);//passing null value to a method which has Graphics class instance and using it for drawing
  }
  if (shape.equals("circle") || shape.equals("CIRCLE"))
  {
      paintCirc(null);//same here
  }
}

The better way is always use paint() method and call repaint() method. Use the below code:

import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;

/*
<applet code = "Demo.class" width = 400 height = 200> </applet>
*/

public class Demo extends Applet implements ActionListener{
  TextField tf;
  Button draw;
  String shape = "rectangle";//drawing rectangle by default

  public void init(){
    tf = new TextField(10);
    draw = new Button("Draw");
    draw.addActionListener(this);
    add(tf);
    add(draw);
  }

  public void actionPerformed(ActionEvent e) {
    shape = tf.getText();
    repaint();
  }

  public void paint(Graphics g){
    super.paint(g);
    if (shape.equals("rectangle") || shape.equals("RECTANGLE"))
    {
      g.drawRect(30,30,50,60);
    }
    if (shape.equals("circle") || shape.equals("CIRCLE"))
    {
      g.drawOval(30, 30, 50, 60);
    }
    else
    {
      //notify to enter the correct input
    }
  }
}
  • Note: `shape.equals("rectangle") || shape.equals("RECTANGLE")` can be shortened to `shape.toLowerCase().equals("rectangle"))` – Andrew Thompson Apr 29 '16 at 03:04
  • @Avinash Thank you sir! That worked like charm and I learnt something today. I understand my mistakes and I won't do them again. – Ufuk Ozdogan Apr 29 '16 at 08:10
  • @Andrew that kept my code clean and it looks awesome. Thanks! – Ufuk Ozdogan Apr 29 '16 at 08:10
  • @AndrewThompson, yes. We can use equalsIgnoreCase() method too. Then we don't need to worry about the case at all –  Apr 29 '16 at 14:43