Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #6434

A question about some long java code that has getters/setters

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!t8g2000prm.googlegroups.com!not-for-mail
From Chad <cdalten@gmail.com>
Newsgroups comp.lang.java.programmer
Subject A question about some long java code that has getters/setters
Date Fri, 22 Jul 2011 16:12:27 -0700 (PDT)
Organization http://groups.google.com
Lines 148
Message-ID <d94f5902-8b2e-484f-8095-9ce034e916d4@t8g2000prm.googlegroups.com> (permalink)
NNTP-Posting-Host 66.81.50.100
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
X-Trace posting.google.com 1311376448 27329 127.0.0.1 (22 Jul 2011 23:14:08 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Fri, 22 Jul 2011 23:14:08 +0000 (UTC)
Complaints-To groups-abuse@google.com
Injection-Info t8g2000prm.googlegroups.com; posting-host=66.81.50.100; posting-account=kTs1ygoAAACgG1TSoyECpovEyy-V6_8b
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-Header-Order ARLUEHNKC
X-HTTP-UserAgent Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.2),gzip(gfe)
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6434

Show key headers only | View raw


The following code, which is taken from one of my school books,
displays 4 different boxes inside a gui



import java.awt.*;
import javax.swing.*;

public class TestMessagePanel extends JFrame {

    public TestMessagePanel() {
        MessagePanel messagePanel1 = new MessagePanel("Top Left");
        MessagePanel messagePanel2 = new MessagePanel("Top Right");
        MessagePanel messagePanel3 = new MessagePanel("Bottom Left");
        MessagePanel messagePanel4 = new MessagePanel("Bottom Right");
        messagePanel1.setBackground(Color.RED);
        messagePanel2.setBackground(Color.CYAN);
        messagePanel3.setBackground(Color.GREEN);
        messagePanel4.setBackground(Color.WHITE);
        messagePanel1.setCentered(true);

        setLayout(new GridLayout(2, 2));
        add(messagePanel1);
        add(messagePanel2);
        add(messagePanel3);
        add(messagePanel4);
    }

    public static void main(String[] args) {
        TestMessagePanel frame = new TestMessagePanel();
        frame.setSize(300, 200);
        frame.setTitle("TestMessagePanel");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }//end main()
}

class MessagePanel extends JPanel {

    private String message = "Nope";
    private int xCoordinate = 20;
    private int yCoordinate = 20;
    private int interval = 10;
    private boolean centered;

    public MessagePanel() {
    }

    public MessagePanel(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
        repaint();
    }

    public int getXCoordinate() {
        return xCoordinate;
    }

    public void setXCoordinate(int x) {
        this.xCoordinate = x;
        repaint();
    }

    public int getYCoordinate() {
        return yCoordinate;
    }

    public void setYCoordinate(int y) {
        this.xCoordinate = y;
        repaint();
    }

    public boolean isCentered() {
        return centered;
    }

    public void setCentered(boolean centered) {
        this.centered = centered;
        repaint();
    }

    public int getInterval() {
        return interval;
    }

    public void setInterval(int interval) {
        this.interval = interval;
        repaint();
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (centered) {
            FontMetrics fm = g.getFontMetrics();
            int stringWidth = fm.stringWidth(message);
            int stringAscent = fm.getAscent();
            xCoordinate = getWidth() / 2 - stringWidth / 2;
            yCoordinate = getWidth() / 2 - stringAscent / 2;
        }
        g.drawString(message, xCoordinate, yCoordinate);
    }

    public void MoveLeft() {
        xCoordinate -= interval;
        repaint();
    }

    public void MoveRight() {
        xCoordinate += interval;
        repaint();
    }

    public void moveUp() {
        yCoordinate -= interval;
        repaint();
    }

    public void moveDown() {
        yCoordinate += interval;
        repaint();
    }

    public Dimension getPreferredSize() {
        return new Dimension(200, 30);
    }
}


What I don't get is why the book defines stuff like getXCoordinate(),
getYCoordinate(), and getInterval() when it doesn't even use them in
this very long code example. I tried reading over the section in the
book, but the author gives no explanation on why he included a bunch
of unused getters/setters. On top of that, the code seems to work fine
when I comment out these methods.

Ideas?

Chad

Back to comp.lang.java.programmer | Previous | NextNext in thread | Find similar


Thread

A question about some long java code that has getters/setters Chad <cdalten@gmail.com> - 2011-07-22 16:12 -0700
  Re: A question about some long java code that has getters/setters Arne Vajhøj <arne@vajhoej.dk> - 2011-07-22 19:28 -0400
  Re: A question about some long java code that has getters/setters Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-07-22 23:31 +0000
  Re: A question about some long java code that has getters/setters markspace <-@.> - 2011-07-22 16:51 -0700
  Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-23 09:02 -0700
    Re: A question about some long java code that has getters/setters blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-07-23 17:41 +0000
      Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-23 11:42 -0700
      Re: A question about some long java code that has getters/setters Arne Vajhøj <arne@vajhoej.dk> - 2011-07-23 17:59 -0400
        Re: A question about some long java code that has getters/setters Steve Sobol <sjsobol@JustThe.net> - 2011-07-23 23:39 -0700
          Re: A question about some long java code that has getters/setters blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-07-25 19:44 +0000
          Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-26 09:03 -0700
    Re: A question about some long java code that has getters/setters Arne Vajhøj <arne@vajhoej.dk> - 2011-07-23 19:26 -0400
      Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-26 09:00 -0700
    Re: A question about some long java code that has getters/setters Patrick May <patrick@softwarematters.org> - 2011-07-25 16:27 -0400
      Re: A question about some long java code that has getters/setters markspace <-@.> - 2011-07-25 15:14 -0700
      Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-25 15:56 -0700
        Re: A question about some long java code that has getters/setters Patricia Shanahan <pats@acm.org> - 2011-07-25 17:00 -0700
          Re: A question about some long java code that has getters/setters Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-25 21:17 -0400
      Re: A question about some long java code that has getters/setters markspace <-@.> - 2011-07-25 16:50 -0700
        Re: A question about some long java code that has getters/setters Gene Wirchenko <genew@ocis.net> - 2011-07-25 18:30 -0700
          Re: A question about some long java code that has getters/setters Patricia Shanahan <pats@acm.org> - 2011-07-25 18:41 -0700
          Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-26 09:05 -0700
          Re: A question about some long java code that has getters/setters Gene Wirchenko <genew@ocis.net> - 2011-07-26 10:48 -0700
            Re: A question about some long java code that has getters/setters Gene Wirchenko <genew@ocis.net> - 2011-07-26 11:56 -0700
              Re: A question about some long java code that has getters/setters Patricia Shanahan <pats@acm.org> - 2011-07-26 13:34 -0700
                Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-26 14:21 -0700
            Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-26 14:20 -0700
        Re: A question about some long java code that has getters/setters Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-25 21:35 -0400
        Re: A question about some long java code that has getters/setters Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-07-26 06:59 -0300
    Re: A question about some long java code that has getters/setters Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2011-07-26 12:19 +0300
      Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-26 09:08 -0700
        Re: A question about some long java code that has getters/setters Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2011-07-27 15:56 +0300
          Re: A question about some long java code that has getters/setters lewbloch <lewbloch@gmail.com> - 2011-07-27 14:14 -0700

csiph-web