3

I am following the examples from Java : The complete reference 8th edition (JDK 7) on AWT and I cannot succeed to display a string on the window that appears. The size and title are set correctly and the window shows up. If I output a string on the console in the paint() method I see that it actually gets called a few times but the string does not appear on the window of my application. I can't see where I diverged from the example; I actually have a bit less code (they added a mouse listener and a key listener) :\

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Main {
    public static void main(String[] args) {
        Application app = new Application();
        app.setSize(new Dimension(640, 480));
        app.setTitle("This is a test");
        app.setVisible(true);
    }
}

class MyWindowAdapter extends WindowAdapter {
    public void windowClosing(WindowEvent we) {
        System.exit(0);
    }
}

class Application extends Frame {
    public Application() {
        addWindowListener(new MyWindowAdapter());
    }

    public void paint(Graphics g) {
        System.out.println("Hey hey !");
        g.drawString("Test", 10, 10);
    }
}
ApplePie
  • 8,814
  • 5
  • 39
  • 60
  • 1
    @MadProgrammer i didn't see setVisible xD should call super.paint() – nachokk Jul 06 '13 at 03:29
  • 1
    @nachokk +1 for `super.paint()`...but the real question is why AWT? – MadProgrammer Jul 06 '13 at 03:37
  • 1
    Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Jul 06 '13 at 03:49
  • Honestly, I am learning AWT only because it's the next chapter in the book. Swing is introduced a few chapters later. The book says that it's good to learn AWT since Swing built on top of it. – ApplePie Jul 06 '13 at 13:40
  • *"The book says that it's good to learn AWT since Swing built on top of it."* The book is misleading. the 'non-component' parts of the AWT are used extensively in Swing. OTOH you should never have to use the AWT Components, and given their use is different, it will just confuse things. Tip: Add @MadProgrammer (or whoever - the `@` is important) to notify them of a new comment. – Andrew Thompson Jul 07 '13 at 02:02

2 Answers2

5

The problem you're having is the fact that you are painting directly on top of the frame. The frame also includes the frame border, so position 0, 0 (or in your case 10, 10) is actually hidden UNDER the frame border.

You can see more about that here.

Instead, you should draw onto a Canvas and add that to the frame

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class BadFrame {

    public static void main(String[] args) {
        new BadFrame();
    }

    public BadFrame() {
        Application app = new Application();
        app.setSize(new Dimension(640, 480));
        app.setTitle("This is a test");
        app.setLayout(new BorderLayout());
        app.add(new MyCanvas());
        app.setVisible(true);
    }

    class MyWindowAdapter extends WindowAdapter {

        public void windowClosing(WindowEvent we) {
            System.exit(0);
        }
    }

    public class MyCanvas extends Component {

        @Override
        public void paint(Graphics g) {
            super.paint(g);
            System.out.println("Hey hey !");
            g.drawString("Test", 10, 10);
        }

    }

    class Application extends Frame {

        public Application() {
            addWindowListener(new MyWindowAdapter());
        }

    }
}

The next question that comes to mind is, why AWT? The API has being moth balled in favor of Swing. If nothing else, it's automatically double buffered ;)

ps- You may also find 2D Graphics of some interest, especially the discussion on text

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

Your string gets drawn but is hidden under the title bar of the window. Just use e.g.

g.drawString("Test", 10, 200); 

and you'll see it appear

  • 1
    Yes, but why? Also, developers should be discouraged from paint directly to top level containers, specifically for the reason that the OP is having issues with... – MadProgrammer Jul 06 '13 at 03:36
  • @MadProgrammer: the paint in the super classes draws all the window decorations, etc. too afaik which means 0,0 can't account for the title bar. Your example is a lot nicer but for experimenting with some stuff out of a book I don't think using paint() directly is a big deal. I'd assume OP doesn't want to release it as a commercial component –  Jul 06 '13 at 03:45
  • Don't tell me, tell the OP ;) – MadProgrammer Jul 06 '13 at 03:53
  • Thanks guy ! I explained why in the comments to my question. – ApplePie Jul 06 '13 at 13:41