1
\$\begingroup\$

I've been using this class for so long, I never thought about how terribly big and how bad it looks. Looking back on it, this class looks so out-of-date, and nothing explains itself; I want to get it looking much better, not with all the extra junk that it doesn't need.
Here's the class:

import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

public class Display {

    private JFrame frame;
    private Canvas canvas;

    private String title;
    private int width, height;

    public Display(String title, int width, int height){
        this.title = title;
        this.width = width;
        this.height = height;

        createDisplay();
    }

    private void createDisplay(){
        frame = new JFrame(title);
        frame.setSize(width, height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        canvas = new Canvas();
        canvas.setPreferredSize(new Dimension(width, height));
        canvas.setMaximumSize(new Dimension(width, height));
        canvas.setMinimumSize(new Dimension(width, height));
        canvas.setFocusable(false);

        frame.add(canvas);
        frame.pack();
    }

    public Canvas getCanvas(){
        return canvas;
    }

    public JFrame getFrame(){
        return frame;
    }

}

Is there anything here I can condense, or any way to make it look cleaner?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

one minor issue - no redundant code...

canvas = new Canvas();
canvas.setPreferredSize(new Dimension(width, height));
canvas.setMaximumSize(new Dimension(width, height));
canvas.setMinimumSize(new Dimension(width, height));
canvas.setFocusable(false);

you should use

canvas.setPreferredSize(dim); //see below
canvas.setMaximumSize(dim);
canvas.setMinimumSize(dim);

that leads to another issue: create a default constructor that doesn't require a size

private static final int DEFAULT_WIDTH = 100;
private static final int DEFAULT_HEIGHT = 100;
public Display(String title){
    this(title, DEFAULT_WIDTH, DEFAULT_HEIGHT);
}

and use this parametrized constructor:

//private int width, height; TODO remove: no more required
private static final Dimension dim;
public Display(String title, int width, int height){
    this.dim = new Dimension(width, height);
    createDisplay();
}

another issue would be removing unrequired attributes - whats the sense of keeping the title? just forward it...

//private String title; TODO remove: no more required
public Display(String title, int width, int height){
    this.dim = new Dimension(width, height);
    createDisplay(title);
}

private void createDisplay(final String title){
    frame = new JFrame(title);
    ...
}
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.