Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I using the Java swing and graphics correctly? [closed]

Tags:

java

swing

I am just learning java and have to do something using the java swing library, and the Graphics2D class. Basically I have to draw a construction crane that has multiple parts: a body (the body of the crane), and a number of attached arms (basically it looks like this: http://i.imgur.com/4YIkYqW.jpg).

My question revolves around if I am using the Java swing class correctly? In my code below, I left out unnecessary code, as I just want to make sure my structure is correct (Using JPanel, paintComponent(), etc. correctly). Any help would be appreciated, as I am just learning Java! Thanks guys.

public class CraneSimulator {

    ...
    public JFrame frame;
    public MyPanel panel;

    public CraneSimulator() {
        frame = new JFrame("CraneSimulator");

        ...

        panel = new MyPanel();
        frame.add(panel);

    }

    public static void main(String[] args) {
        CraneSimulator simulator = new CraneSimulator();    
    }
}   

class MyPanel extends JPanel {
    CraneBody body;
    CraneArm arm1;
        ...
    Graphics2D graphics;

    public MyPanel() {
        body = new CraneBody();
        arm1 = new CraneArm(body);
        ...
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                ...         }
            }
            public void mouseReleased(MouseEvent e) {
                    ...
            }
        });

        addMouseMotionListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e) {
                ...             
            }
        });
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        graphics = (Graphics2D) g;
        ...
        body.paint(g);      
        arm1.paint(g);  
    }
}

class CraneBody {
    ...

    public CraneBody() {
         ....
    }
    ...
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

            // Use g2 to actual paint crane Body on screen here (ie. g2.drawRect, etc)      
    }
}

class CraneArm {
    ...

    public CraneArm() {
         ....
    }
    ...
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

            // Use g2 to actual paint the crane armon screen here (ie. g2.drawRect, etc)        
    }
}
like image 874
Tesla Avatar asked Feb 08 '13 06:02

Tesla


2 Answers

Your code is well-structured and follows excellent practice using Java Graphics as well as OOP.

As suggested in the comment, it is better to define your Graphics object local if you do not have a reason to make it an instance variable.

like image 102
iTech Avatar answered Oct 19 '22 23:10

iTech


Your code is almost perfect. But several suggestions:

  1. Initialization on declaration has some advantages;
  2. You just need one MouseAdapter;
  3. Use Graphics2D as argument so you don't need to cast it from Graphics again.
  4. Remove the field "graphics", instead, make it a local variable. (Thanks to @GuillaumePolet).

Some people may disagree, but according to your code, I would do these changes to make it neater.

public class CraneSimulator {
    ...
    private JFrame frame = new JFrame("CraneSimulator");
    private MyPanel panel = new JPanel();

    public CraneSimulator() {
        ...
        frame.add(panel);
    }
    public static void main(String[] args) {
        CraneSimulator simulator = new CraneSimulator();    
    }
}   

class MyPanel extends JPanel {
    CraneBody body = new CraneBody();
    CraneArm arm1 = new CraneArm(body);
    ...
    MouseAdapter mAdapter = new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            ...
        }
        public void mouseReleased(MouseEvent e) {
            ...
        }
        public void mouseDragged(MouseEvent e) {
            ...             
        }
    }

    public MyPanel() {
        ...
        addMouseListener(mAdapter);
        addMouseMotionListener(mAdapter);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D graphics = (Graphics2D) g;
        ...
        body.paint(graphics);
        arm1.paint(graphics);  
    }
}

class CraneBody {
    ...
    public CraneBody() {
         ....
    }
    ...
    public void paint(Graphics2D g) {
        // You don't need to cast a Graphics again.      
    }
}

class CraneArm {
    ...
    public CraneArm() {
         ....
    }
    ...
    public void paint(Graphics2D g) {
        // You don't need to cast a Graphics again.
    }
}
like image 45
shuangwhywhy Avatar answered Oct 20 '22 00:10

shuangwhywhy