jpIII
jpIII

Reputation: 77

Is there a better way - Java Conditional Statements

I've just started programming. I wrote this class for my programming class. It pulls in parameters from a driver, the teacher gave us, then puts proportional shapes into the quadrant of the jPanel the user has chosen. It works fine but I'm wondering if there is a more efficient and cleaner way to write code. The teacher has not given any feedback on earlier projects so I though I would ask here. Thanks for any help.

import java.awt.*;

 public class LearnGraphics {


public static void drawRectangle(Graphics g, int size, String cord_x, String cord_y) {
        int longside = size / 4;
        int shortside = size / 8;
        int x = 0;
        int y = 0;
        if(cord_x.equals("top")&&cord_y.equals("right")) {  
            x = size-(size/3);
            y = size/8;
        }
        if(cord_x.equals("top")&&cord_y.equals("left")) {  
            x = size/12;
            y = size/8;
        }
        if(cord_x.equals("bottom")&&cord_y.equals("right")) {  
            x = size-(size/3);
            y = size-(size/4);
        }
        if(cord_x.equals("bottom")&&cord_y.equals("left")) {  
            x =  size/12;
            y = size-(size/4);
        }
        g.drawRect(x, y, longside, shortside);

    }

public static void drawLine(Graphics g, int size, String cord_x, String cord_y) {

    int x = 0;
    int y = 0;
    int x1 = 0;
    int y1 = 0;
    if(cord_x.equals("top")&&cord_y.equals("right")) {  
        x = size/12;
        y = size/6;
        x1 = size/3;
        y1 = size/6;
    }
    if(cord_x.equals("top")&&cord_y.equals("left")) {  
        x = (size/2)+(size/6);
        y = size/6;
        x1 = size-(size/12);
        y1 = size/6;
    }
    if(cord_x.equals("bottom")&&cord_y.equals("right")) {  
        x = size/12;
        y = size-(size/6);
        x1 = size/3;
        y1 = size-(size/6);
    }
    if(cord_x.equals("bottom")&&cord_y.equals("left")) {  
        x = (size/2)+(size/6);
        y = size-(size/6);
        x1 = size-(size/12);
        y1 = size-(size/6);
    }
    g.drawLine(x, y, x1, y1 )  
}
public static void drawOval(Graphics g, int size, String cord_x, String cord_y) {
    int longside = size / 4;
    int shortside = size / 8;
    int x = 0;
    int y = 0;
    if(cord_x.equals("top")&&cord_y.equals("right")) {  
        x = size-(size/3);
        y = size/8;
    }
    if(cord_x.equals("top")&&cord_y.equals("left")) {  
        x = size/12;
        y = size/8;
    }
    if(cord_x.equals("bottom")&&cord_y.equals("right")) {  
        x = size-(size/3);
        y = size-(size/4);
    }
    if(cord_x.equals("bottom")&&cord_y.equals("left")) {  
        x =  size/12;
        y = size-(size/4);
    }
    g.drawOval(x, y, longside, shortside);    
   }
  }

Upvotes: 1

Views: 127

Answers (3)

UFL1138
UFL1138

Reputation: 642

I'm a big fan of enums. Here's a way to do it using enums. This also includes your code (in another class) as well as a driver program that compares them side-by-side. HTH.

package test;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class LearnGraphics {
    public static void drawRectangle(Graphics g, int size, String cord_x, String cord_y) {
        Quadrant q = Quadrant.valueOf(cord_x, cord_y);
        float x = q.horiz.rectX*size, y = q.vert.rectY*size;
        g.drawRect(Math.round(x), Math.round(y), size / 4, size / 8);
    }

    public static void drawLine(Graphics g, int size, String cord_x, String cord_y) {
        Quadrant q = Quadrant.valueOf(cord_x,cord_y);
        float x0 = q.horiz.lineX0*size, y  = q.vert.lineY*size, x1 = q.horiz.lineX1*size;
        g.drawLine(Math.round(x0), Math.round(y), Math.round(x1), Math.round(y));
    }

    public static void drawOval(Graphics g, int size, String cord_x, String cord_y) {
        Quadrant q = Quadrant.valueOf(cord_x,cord_y);
        float x = q.horiz.ovalX*size, y = q.vert.ovalY*size;
        g.drawOval(Math.round(x), Math.round(y), size / 4, size / 8);
    }


    public static void invertColor(Graphics g){
        Color c = g.getColor();
        g.setColor(new Color(255-c.getRed(), 255-c.getGreen(), 255-c.getBlue()));
    }

    public static void main(String[] arg){
        JFrame jf = new JFrame("Learn Enums (NEW WAY)");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final int size=400;

        @SuppressWarnings("serial")
        JPanel jp = new JPanel(){
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.BLUE);
                drawRectangle(g, size, "left", "top");
                g.setColor(Color.ORANGE);
                drawOval(g, size, "left", "bottom");
                g.setColor(Color.GREEN);
                drawLine(g, size, "right", "bottom");
            }
        };

        jf.setContentPane(jp);
        jf.setBounds(120, 120, size, size);
        jf.setVisible(true);

        JFrame owjf = new JFrame("Learn Graphics (OLD WAY)");
        owjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        @SuppressWarnings("serial")
        JPanel owjp = new JPanel(){
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.BLUE);
                OldWays.drawRectangle(g, size, "left", "top");
                g.setColor(Color.ORANGE);
                OldWays.drawOval(g, size, "left", "bottom");
                g.setColor(Color.GREEN);
                OldWays.drawLine(g, size, "right", "bottom");
            };
        };
        owjf.setContentPane(owjp);
        owjf.setBounds(120+50+size, 120, size, size);
        owjf.setVisible(true);

    }

}
enum Quadrant{
    top_left(Vert.top,Horiz.left), 
    top_right(Vert.top,Horiz.right), 
    bottom_left(Vert.bottom,Horiz.left), 
    bottom_right(Vert.bottom,Horiz.right);

    private Quadrant(Vert v, Horiz h){
        vert=v;
        horiz=h;
    }
    final Vert vert;
    final Horiz horiz;
    public static Quadrant valueOf(String horiz, String vert){
        return valueOf(vert+"_"+horiz);
    }
    enum Vert{
        top(   1/8f, 1/6f, 1/8f),
        bottom(3/4f, 5/6f, 3/4f);
        private Vert(float rectY, float lineY, float ovalY){
            this.rectY=rectY;
            this.lineY=lineY;
            this.ovalY=ovalY;
        }
        final float rectY, lineY, ovalY;
    }
    enum Horiz{
        left( 1/12f,  2/3f, 11/12f, 1/12f),
        right( 1/3f, 1/12f,   1/3f,  2/3f);
        private Horiz(float rectX, float lineX0, float lineX1, float ovalX){
            this.rectX=rectX;
            this.lineX0=lineX0;
            this.lineX1=lineX1;
            this.ovalX=ovalX;
        }
        final float rectX, lineX0, lineX1, ovalX;
    }
}
class OldWays{
    /** INCLUDED SO YOU CAN TEST THE VERSIONS AGAINST EACH OTHER **/
    public static void drawRectangle(Graphics g, int size, String cord_y,
            String cord_x) {
        int longside = size / 4;
        int shortside = size / 8;
        int x = 0;
        int y = 0;
        if (cord_x.equals("top") && cord_y.equals("right")) {
            x = size - (size / 3);
            y = size / 8;
        }
        if (cord_x.equals("top") && cord_y.equals("left")) {
            x = size / 12;
            y = size / 8;
        }
        if (cord_x.equals("bottom") && cord_y.equals("right")) {
            x = size - (size / 3);
            y = size - (size / 4);
        }
        if (cord_x.equals("bottom") && cord_y.equals("left")) {
            x = size / 12;
            y = size - (size / 4);
        }

        System.out.printf("ow.drawRectangle(%d, %d, %d, %d);\n", x, y, longside, shortside);
        g.drawRect(x, y, longside, shortside);

    }

    public static void drawLine(Graphics g, int size, String cord_y,
            String cord_x) {

        int x = 0;
        int y = 0;
        int x1 = 0;
        int y1 = 0;
        if (cord_x.equals("top") && cord_y.equals("right")) {
            x = size / 12;
            y = size / 6;
            x1 = size / 3;
            y1 = size / 6;
        }
        if (cord_x.equals("top") && cord_y.equals("left")) {
            x = (size / 2) + (size / 6);
            y = size / 6;
            x1 = size - (size / 12);
            y1 = size / 6;
        }
        if (cord_x.equals("bottom") && cord_y.equals("right")) {
            x = size / 12;
            y = size - (size / 6);
            x1 = size / 3;
            y1 = size - (size / 6);
        }
        if (cord_x.equals("bottom") && cord_y.equals("left")) {
            x = (size / 2) + (size / 6);
            y = size - (size / 6);
            x1 = size - (size / 12);
            y1 = size - (size / 6);
        }
        System.out.printf("ow.drawLine(%d, %d, %d, %d);\n", x, y, x1, y1);
        g.drawLine(x, y, x1, y1);
    }

    public static void drawOval(Graphics g, int size, String cord_y,
            String cord_x) {
        int longside = size / 4;
        int shortside = size / 8;
        int x = 0;
        int y = 0;
        if (cord_x.equals("top") && cord_y.equals("right")) {
            x = size - (size / 3);
            y = size / 8;
        }
        if (cord_x.equals("top") && cord_y.equals("left")) {
            x = size / 12;
            y = size / 8;
        }
        if (cord_x.equals("bottom") && cord_y.equals("right")) {
            x = size - (size / 3);
            y = size - (size / 4);
        }
        if (cord_x.equals("bottom") && cord_y.equals("left")) {
            x = size / 12;
            y = size - (size / 4);
        }
        System.out.printf("ow.drawOval(%d, %d, %d, %d);\n", x, y, longside, shortside);
        g.drawOval(x, y, longside, shortside);
    }
}

Note: Sometimes when you create enums that are more than just a value -- in this case they have values attached to them -- the enum code itself can look a little messy, but look at how much cleaner the client code is!

Upvotes: 0

No Idea For Name
No Idea For Name

Reputation: 11577

if you do if-else statement then you won't go through if statements once you've entered one of them and that will be more efficient, plus you wont ask the same condition twice

public static void drawRectangle(Graphics g, int size, String cord_x, String cord_y) {
    int longside = size / 4;
    int shortside = size / 8;
    int x = 0;
    int y = 0;
    if(cord_x.equals("top")) {  
        if(cord_y.equals("right"))
        {
           x = size-(size/3);
           y = size/8;
        }
        else
        {
           if(cord_y.equals("left"))
           {
               x = size/12;
               y = size/8;
           }
        }
    }
    else
    {

        if(cord_x.equals("bottom")) {  
           if(cord_y.equals("right"))
           {
              x = size-(size/3);
              y = size-(size/4);
           }
           else
           {
              if(cord_y.equals("left"))
              {
                 x =  size/12;
                y = size-(size/4);
              }
           }
        }
    }
    g.drawRect(x, y, longside, shortside);

}

other then that there is nothing to worry at the moment because modern computers are very fast and this code is not long, so it will be fast

Upvotes: 1

Skylion
Skylion

Reputation: 2752

Just use boolean methods.

Ex:

  public static boolean isRight(){
          return cord_x.equals("right");
     }

Now do that for every method like isLeft(), isTop() etc. And now you can do stuff like this.

else if(isRight() && isBottom()) 

else statements would make your code slightly more efficient.

Upvotes: 0

Related Questions