简体   繁体   中英

Java AWT Window implementation not running paint() function

(I am fully aware this is an old way to go about this - I have to do it this way)

I am trying to use the Window class of AWT to draw basic visuals to the screen - the window shows up, but, despite the fact that the paint() function is being run(Tested with sysout ) - nothing shows up.

The problematic code:

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;

public class a {

    static a a = new a();
    public static void main(String[] args) {
        b b = a.new b();
        b.setVisible(true);
        b.pack();
    }
    class b extends Frame {
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, 16, 16);
            repaint();
        }
    }
}

If anyone knows what is causing this or how to rectify it I would greatly appreciate your assistance.

Thanks in advance!

Turns out I needed an extra Canvas object to draw onto - here's the functioning code for anyone having the problem I had

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;

public class a {

    static a a = new a();
    public static void main(String[] args) {
        Frame f = new Frame();
        b b = a.new b();
        f.add(b);
        f.setVisible(true);
        f.pack();
    }
    class b extends Canvas {
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, 16, 16);
            repaint();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM