Prev: 32-bit characters in Java string literals
Next: SocketHandler permissions Re: servlet/applet communicationproblem or Linux/Windows trouble ?
From: Dave on 22 Dec 2009 21:45 Thanks for the reply. In my program, if I replaced mp.repaint() with frame.repaint() the program works OK. But my original intent was not to clear the previous draws. This program is a simple version, to keep it small, of another program that I'm writing. In that program I don't want to clear the previous draws on the screen, but I want to leave a trail. "markspace" <nospam(a)nowhere.com> wrote in message news:hgrtgq$hq5$1(a)news.eternal-september.org... > Dave wrote: > >> 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. > > > I see the double menus too. I have Vista Ultimate, and Java 1.6 update 17 > also. Good job on the sample code btw, you're light-years ahead of most > of the new posters we get around here. > > I see two big problems which may be affecting this. First, you absolutely > have to use proper synchronization when dealing with Swing components. > Otherwise, all bets are off. You call methods which are not thread safe > directly from your "main" method, which could be causing all sorts of > errors. > > <http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html> > > The second thing that I notice that if Thread.sleep() is called on the > EDT, well, you're going to be really unhappy about that. So I substituted > a Swing Timer instead. > > What actually fixed the funny display is calling repaint() on the whole > frame instead of just the component. Swing likes to layout whole > components, just trying to update part of one may not work I suppose. > > Finally, pay attention to coding standards. Class names should begin with > a capital letter. If this code is from work, I'd think you'd want to be a > bit more rigorous about adhering to standards. > > My version follows: > > > package test; > > import java.awt.event.ActionEvent; > import javax.swing.*; > import java.awt.*; > import java.awt.event.ActionListener; > > public class Main { > public static void main(String[] args) { > graph1.main( args ); > } > > } > > class graph1 > { > int x = 70; > int y = 70; > > public static void main(String[] args) > { > SwingUtilities.invokeLater( new Runnable() { > public void run() { > graph1 gui = new graph1(); > gui.go(); > } > } ); > } > > public void go() > { > JMenuBar mBar = new JMenuBar(); > JMenu mMenu1 = new JMenu("Menu1"); > JMenuItem mItem = new JMenuItem("first entry"); > > mBar.add(mMenu1); > mMenu1.add(mItem); > > final JFrame frame = new JFrame("Graph1"); > frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); > frame.setJMenuBar(mBar); > > final MyPaint mp = new MyPaint(); > frame.getContentPane().add(mp); > > frame.setSize(600,600); > frame.setVisible(true); > > ActionListener sprite = new ActionListener() { > > public void actionPerformed(ActionEvent e) { > ++x; > ++y; > frame.repaint(); > } > }; > > Timer spriteTimer = new Timer(200, sprite); > spriteTimer.setInitialDelay( 400 ); > spriteTimer.start(); > } > > class MyPaint extends JPanel > { > public void paintComponent(Graphics g) > { > g.setColor(Color.green); > g.fillOval(x,y,40,40); > } > } > } > >
From: John B. Matthews on 22 Dec 2009 21:49 In article <ycOdnfcNP4LYHazWnZ2dnUVZ_iydnZ2d(a)posted.oberlin>, "Dave" <none123(a)none456.net> 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>
From: markspace on 22 Dec 2009 22:20 Dave wrote: > Thanks for the reply. > > In my program, if I replaced mp.repaint() with frame.repaint() > the program works OK. But my original intent was not to clear > the previous draws. This program is a simple version, to keep it > small, of another program that I'm writing. In that program I don't > want to clear the previous draws on the screen, but I want to leave a trail. If you want to leave a trail, you need to save your previous draw commands. Make the updates to an Image, then blit the image to the screen when you paint the panel. You have to redraw the background or you'll lose your previous draws commands the first time the app is minimized (try it and see), and on some systems as soon as it's covered by another app.
From: John B. Matthews on 23 Dec 2009 12:11 In article <hgs7fl$146$1(a)news.albasani.net>, Lew <noone(a)lewscanon.com> 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>
From: Lew on 22 Dec 2009 23:48
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 |