Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.glorb.com!postnews.google.com!p29g2000pre.googlegroups.com!not-for-mail From: lewbloch Newsgroups: comp.lang.java.programmer Subject: Re: A question about some long java code that has getters/setters Date: Sat, 23 Jul 2011 09:02:35 -0700 (PDT) Organization: http://groups.google.com Lines: 173 Message-ID: <1672e2f1-a963-4fcf-b651-41b69432c9d7@p29g2000pre.googlegroups.com> References: NNTP-Posting-Host: 108.89.33.208 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1311436955 22436 127.0.0.1 (23 Jul 2011 16:02:35 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 23 Jul 2011 16:02:35 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: p29g2000pre.googlegroups.com; posting-host=108.89.33.208; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: ASELCHRU X-HTTP-UserAgent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.99 Safari/535.1,gzip(gfe) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6458 Chad wrote: > The following code, which is taken from one of my school books, > displays 4 different boxes inside a gui [sic] > > import java.awt.*; > import javax.swing.*; > > public class TestMessagePanel extends JFrame { > > =A0 =A0 public TestMessagePanel() { > =A0 =A0 =A0 =A0 MessagePanel messagePanel1 =3D new MessagePanel("Top Left= "); > =A0 =A0 =A0 =A0 MessagePanel messagePanel2 =3D new MessagePanel("Top Righ= t"); > =A0 =A0 =A0 =A0 MessagePanel messagePanel3 =3D new MessagePanel("Bottom L= eft"); > =A0 =A0 =A0 =A0 MessagePanel messagePanel4 =3D new MessagePanel("Bottom R= ight"); > =A0 =A0 =A0 =A0 messagePanel1.setBackground(Color.RED); > =A0 =A0 =A0 =A0 messagePanel2.setBackground(Color.CYAN); > =A0 =A0 =A0 =A0 messagePanel3.setBackground(Color.GREEN); > =A0 =A0 =A0 =A0 messagePanel4.setBackground(Color.WHITE); > =A0 =A0 =A0 =A0 messagePanel1.setCentered(true); > > =A0 =A0 =A0 =A0 setLayout(new GridLayout(2, 2)); > =A0 =A0 =A0 =A0 add(messagePanel1); > =A0 =A0 =A0 =A0 add(messagePanel2); > =A0 =A0 =A0 =A0 add(messagePanel3); > =A0 =A0 =A0 =A0 add(messagePanel4); > =A0 =A0 } > > =A0 =A0 public static void main(String[] args) { > =A0 =A0 =A0 =A0 TestMessagePanel frame =3D new TestMessagePanel(); > =A0 =A0 =A0 =A0 frame.setSize(300, 200); > =A0 =A0 =A0 =A0 frame.setTitle("TestMessagePanel"); > =A0 =A0 =A0 =A0 frame.setLocationRelativeTo(null); > =A0 =A0 =A0 =A0 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); > =A0 =A0 =A0 =A0 frame.setVisible(true); > > =A0 =A0 }//end main() > > } > > class MessagePanel extends JPanel { > > =A0 =A0 private String message =3D "Nope"; > =A0 =A0 private int xCoordinate =3D 20; > =A0 =A0 private int yCoordinate =3D 20; > =A0 =A0 private int interval =3D 10; > =A0 =A0 private boolean centered; > > =A0 =A0 public MessagePanel() { > =A0 =A0 } > > =A0 =A0 public MessagePanel(String message) { > =A0 =A0 =A0 =A0 this.message =3D message; > =A0 =A0 } > > =A0 =A0 public String getMessage() { > =A0 =A0 =A0 =A0 return message; > =A0 =A0 } > > =A0 =A0 public void setMessage(String message) { > =A0 =A0 =A0 =A0 this.message =3D message; > =A0 =A0 =A0 =A0 repaint(); > =A0 =A0 } > > =A0 =A0 public int getXCoordinate() { > =A0 =A0 =A0 =A0 return xCoordinate; > =A0 =A0 } > > =A0 =A0 public void setXCoordinate(int x) { > =A0 =A0 =A0 =A0 this.xCoordinate =3D x; > =A0 =A0 =A0 =A0 repaint(); > =A0 =A0 } > > =A0 =A0 public int getYCoordinate() { > =A0 =A0 =A0 =A0 return yCoordinate; > =A0 =A0 } > > =A0 =A0 public void setYCoordinate(int y) { > =A0 =A0 =A0 =A0 this.xCoordinate =3D y; > =A0 =A0 =A0 =A0 repaint(); > =A0 =A0 } > > =A0 =A0 public boolean isCentered() { > =A0 =A0 =A0 =A0 return centered; > =A0 =A0 } > > =A0 =A0 public void setCentered(boolean centered) { > =A0 =A0 =A0 =A0 this.centered =3D centered; > =A0 =A0 =A0 =A0 repaint(); > =A0 =A0 } > > =A0 =A0 public int getInterval() { > =A0 =A0 =A0 =A0 return interval; > =A0 =A0 } > > =A0 =A0 public void setInterval(int interval) { > =A0 =A0 =A0 =A0 this.interval =3D interval; > =A0 =A0 =A0 =A0 repaint(); > =A0 =A0 } > > =A0 =A0 protected void paintComponent(Graphics g) { > =A0 =A0 =A0 =A0 super.paintComponent(g); > > =A0 =A0 =A0 =A0 if (centered) { > =A0 =A0 =A0 =A0 =A0 =A0 FontMetrics fm =3D g.getFontMetrics(); > =A0 =A0 =A0 =A0 =A0 =A0 int stringWidth =3D fm.stringWidth(message); > =A0 =A0 =A0 =A0 =A0 =A0 int stringAscent =3D fm.getAscent(); > =A0 =A0 =A0 =A0 =A0 =A0 xCoordinate =3D getWidth() / 2 - stringWidth / 2; > =A0 =A0 =A0 =A0 =A0 =A0 yCoordinate =3D getWidth() / 2 - stringAscent / 2= ; > =A0 =A0 =A0 =A0 } > =A0 =A0 =A0 =A0 g.drawString(message, xCoordinate, yCoordinate); > =A0 =A0 } > > =A0 =A0 public void MoveLeft() { > =A0 =A0 =A0 =A0 xCoordinate -=3D interval; > =A0 =A0 =A0 =A0 repaint(); > =A0 =A0 } > > =A0 =A0 public void MoveRight() { > =A0 =A0 =A0 =A0 xCoordinate +=3D interval; > =A0 =A0 =A0 =A0 repaint(); > =A0 =A0 } > > =A0 =A0 public void moveUp() { > =A0 =A0 =A0 =A0 yCoordinate -=3D interval; > =A0 =A0 =A0 =A0 repaint(); > =A0 =A0 } > > =A0 =A0 public void moveDown() { > =A0 =A0 =A0 =A0 yCoordinate +=3D interval; > =A0 =A0 =A0 =A0 repaint(); > =A0 =A0 } > > =A0 =A0 public Dimension getPreferredSize() { > =A0 =A0 =A0 =A0 return new Dimension(200, 30); > =A0 =A0 } > > } > > 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? The problem with this code is that it teaches the bad and bug-prone practice of creating GUI elements on the main thread instead of the EDT. Don't use this book. The author apparently didn't know what he was doing. It is standard practice to create accessors and mutators for class attributes. There's nothing wrong with that. The class is written as any good API writer (a.k.a. "programmer") should in that one respect. While you should not build features into a class on a remote chance of their use, if you have properties then you should provide the get/set methods for them quite nearly always. But you should never, never, never do GUI magic off the EDT! It's also bad practice to use import-on-demand (import '*') rather than single-type imports. Really, don't use this book. Get a good book. -- Lew