Re: need help or explanation

Discussion in 'Java' started by John B. Matthews, Dec 23, 2009.

  1. In article <>,
    "Dave" <> wrote:

    > I'm new to Java and I'm running into a problem I can't figure out.
    >
    > I have a menu on a screen and I'm also drawing a moving filled oval
    > on it.
    >
    > When I run the program, I end up with a double menu line. It's as if
    > the first repaint is shifting the whole display down the size of the
    > menu line. The first oval is also shifted down the size of the menu
    > line. The rest of the oval repaints seem to be where they should be.
    >
    > Even though I have 2 menu lines, only the top menu works when I click
    > on it. When I click on "Menu1", I get 2 "first entry" items, one
    > below the other. See Menu example below.
    >
    > ------------------------------------------------
    > Menu1 (this menu works when I click on it)
    > ------------------------------------------------
    > Menu1 (this menu doesn't do anything)
    > ------------------------------------------------
    >
    > -----------------------------------------------------------
    > Menu1 (when I click the top menu, I get 2 first entries)
    > -----------------------------------------------------------
    > first entry (the lower Menu1 is written over)
    > -----------------------------------------------------------
    > first entry
    > -----------------------------------------------------------
    >
    > The bad part is, when I run this program at work, it works like it's
    > supposed to. When I run it at home, I get the double menu. I haven't
    > tried the program on any other PC's yet.


    First, you need to construct your GUI on the Even Dispatch Thread (EDT),
    otherwise your program may exhibit various such anomalies on different
    platforms:

    <http://java.sun.com/docs/books/tutorial/uiswing/concurrency/initial.html>

    Having done that, you must not then block the EDT with time-consuming
    operations. Here's a modified version of your program that uses a Swing
    Timer to periodically repaint the panel:

    package news;

    import java.awt.event.ActionEvent;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;

    public class Graph1 extends JFrame implements ActionListener {

    private final Timer timer = new Timer(200, this);
    private final MyPaint mp = new MyPaint();
    private int x = 70;
    private int y = 70;

    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
    new Graph1();
    }
    });
    }

    public Graph1() {
    super("Graph1");
    JMenuBar mBar = new JMenuBar();
    JMenu mMenu1 = new JMenu("Menu1");
    JMenuItem mItem = new JMenuItem("first entry");

    mBar.add(mMenu1);
    mMenu1.add(mItem);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setJMenuBar(mBar);
    this.add(mp);
    this.pack();
    this.setVisible(true);
    timer.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    x++; y ++;
    mp.repaint();
    }

    private class MyPaint extends JPanel {

    public MyPaint() {
    this.setPreferredSize(new Dimension (600, 600));
    }

    @Override
    public void paintComponent(Graphics g) {
    g.setColor(Color.green);
    g.fillOval(x, y, 40, 40);
    }
    }
    }

    See also, <http://zetcode.com/tutorials/javagamestutorial/>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Dec 23, 2009
    #1
    1. Advertising

  2. In article <>,
    "Dave" <> wrote:

    > Thanks for the response.
    >
    > I tried compiling the program, but I get errors on the first
    > @Override, at super("Graph1"), and the @Override after that.
    >
    > The two @Override give "method does not override a method from its
    > superclass". The super("Graph1") gives "call to super must be first
    > statement in constructor".
    >
    > I don't know what the @Override is supposed to do, but I'll look at
    > the links you provided and see if I can get anything out of them.


    Your java compiler may not recognize the annotation; just comment them
    out for now. Introduced in Java 1.5 to catch incorrect signatures in
    overridden methods, the usage was expanded to include implemented
    interfaces in 1.6:

    <http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html>
    <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Override.html>
    <http://mindprod.com/jgloss/annotations.html>

    Also, compare my variation to those of markspace and Knute Johnson. Like
    mine, the former uses Timer; the latter uses a separate thread. The
    example link discusses both:

    <http://zetcode.com/tutorials/javagamestutorial/>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Dec 23, 2009
    #2
    1. Advertising

  3. John B. Matthews

    Lew Guest

    Dave wrote:
    >> Thanks for the response.
    >>
    >> I tried compiling the program, but I get errors on the first
    >> @Override, at super("Graph1"), and the @Override after that.
    >>
    >> The two @Override give "method does not override a method from its
    >> superclass". The super("Graph1") gives "call to super must be first
    >> statement in constructor".
    >>
    >> I don't know what the @Override is supposed to do, but I'll look at
    >> the links you provided and see if I can get anything out of them.


    John B. Matthews wrote:
    > Your java compiler may not recognize the annotation; just comment them


    Of course his compiler recognized the annotation. How else would it have
    known that the annotation was placed not upon a method from the superclass?

    > out for now. Introduced in Java 1.5 to catch incorrect signatures in
    > overridden methods, the usage was expanded to include implemented
    > interfaces in 1.6:
    >
    > <http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html>
    > <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Override.html>
    > <http://mindprod.com/jgloss/annotations.html>
    >
    > Also, compare my variation to those of markspace and Knute Johnson. Like
    > mine, the former uses Timer; the latter uses a separate thread. The
    > example link discusses both:
    >
    > <http://zetcode.com/tutorials/javagamestutorial/>


    He's using @Override in constructors, which makes no sense and is wrong.

    He's using @Override in the body of the constructor, which also makes no sense
    and is wrong.

    If you actually read the documentation on annotations.

    <http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.7>

    you see that there are only certain places you can use them, and they are all
    declarations, not invocations.

    In future, Dave, when asking for help with code you wrote, it pays to show the
    code about which you're asking. Else how is one to know that '@Override' was
    inside the body of the constructor instead of atop the declaration of a method?

    --
    Lew
     
    Lew, Dec 23, 2009
    #3
  4. In article <hgs7fl$146$>, Lew <>
    wrote:

    > Dave wrote:
    > >> Thanks for the response.
    > >>
    > >> I tried compiling the program, but I get errors on the first
    > >> @Override, at super("Graph1"), and the @Override after that.
    > >>
    > >> The two @Override give "method does not override a method from its
    > >> superclass". The super("Graph1") gives "call to super must be
    > >> first statement in constructor".
    > >>
    > >> I don't know what the @Override is supposed to do, but I'll look
    > >> at the links you provided and see if I can get anything out of
    > >> them.

    >
    > John B. Matthews wrote:
    > > Your java compiler may not recognize the annotation; just comment
    > > them

    >
    > Of course his compiler recognized the annotation. How else would it
    > have known that the annotation was placed not upon a method from the
    > superclass?


    Good question: His compiler did not recognize the annotations as
    applicable to either of the two implemented interface methods in my
    example, run() and actionPerformed(). It did recognize the single
    overridden method, paintComponent(). Together, these are pathognomonic
    of the implementation in Java 1.5.

    I am unable to explain the super failure; @Override does not appear in
    the constructor, which extends JFrame. As an instructive contrast,
    markspace's example extends JPanel:

    <http://groups.google.com/group/comp.lang.java.programmer/msg/cdc97be27c89ad0a>

    [...]

    > He's using @Override in constructors, which makes no sense and is
    > wrong. He's using @Override in the body of the constructor, which
    > also makes no sense and is wrong.


    He never used @Override; I did. My usage is correct for Java 1.6
    but fails under Java 1.5:

    <http://groups.google.com/group/comp.lang.java.programmer/msg/3a6918190945a2a1>

    > If you actually read the documentation on annotations.
    >
    > <http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.7>
    >
    > you see that there are only certain places you can use them, and they
    > are all declarations, not invocations.


    Indeed. Here is a previous discussion:

    <http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thread/f3b21ed1083a3f75>

    [...]
    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Dec 23, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kaladhaur Palaniappa

    Need Explanation

    Kaladhaur Palaniappa, Aug 7, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,028
    Kaladhaur Palaniappa
    Aug 7, 2003
  2. Dave
    Replies:
    4
    Views:
    409
  3. darrel
    Replies:
    0
    Views:
    418
    darrel
    Oct 20, 2004
  4. markspace

    Re: need help or explanation

    markspace, Dec 23, 2009, in forum: Java
    Replies:
    1
    Views:
    355
    markspace
    Dec 23, 2009
  5. Replies:
    1
    Views:
    77
    Richard Cornford
    Apr 18, 2008
Loading...

Share This Page