Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: need help or explanation

Reply
Thread Tools

Re: need help or explanation

 
 
John B. Matthews
Guest
Posts: n/a
 
      12-23-2009
In article <(E-Mail Removed) >,
"Dave" <(E-Mail Removed)> 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>
 
Reply With Quote
 
 
 
 
John B. Matthews
Guest
Posts: n/a
 
      12-23-2009
In article <(E-Mail Removed) >,
"Dave" <(E-Mail Removed)> 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>
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      12-23-2009
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
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      12-23-2009
In article <hgs7fl$146$(E-Mail Removed)>, Lew <(E-Mail Removed)>
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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: need help or explanation markspace Java 1 12-23-2009 03:20 AM
Need help understanding this part of code. Explanation in post(2008-04-18) patrick@patrick-isp.com Javascript 1 04-18-2008 05:10 AM
New to .net need help/explanation Dave ASP .Net 4 07-01-2004 04:00 PM
Need Explanation Kaladhaur Palaniappa Perl 0 08-07-2003 09:47 AM
Help! Need explanation of "X & Y resolution" Ralph O'Rourke Digital Photography 7 07-15-2003 11:49 AM



Advertisments