Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Using Java Graphics, I tried to draw a simple rectangle.

As Applet it runs well but when I use it to show on a JFrame, the rectangle is coming but with some unusual background

Here is coding:

package graphicsex;

import java.awt.Graphics;

public class Graphics2 extends javax.swing.JFrame {

    public static void main(String[] args) {
        Graphics2 inst = new Graphics2();
        inst.setVisible(true);
    }

    public Graphics2() {
        super();
        initGUI();
    }

    private void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            pack();
            setSize(400, 300);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void paint(Graphics g) {
        g.drawRect(10, 20, 30, 40);
    }
}

Then I tried to use JTextArea using these two classes but in this case the rectangle is not displaying at all.

GraphicsEx1.java:

package graphicsex;

import javax.swing.JTextArea;
import java.awt.Graphics;

public class GraphicsEx1 extends javax.swing.JFrame {

    private JTextArea jTextArea1;

    {
        //Set Look & Feel
        try {
            javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        GraphicsEx1 inst = new GraphicsEx1();
        inst.setVisible(true);
    }

    public GraphicsEx1() {
        super();
        initGUI();
    }

    private void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            getContentPane().setLayout(null);
            {
                jTextArea1 = new JTextArea();
                getContentPane().add(jTextArea1);
                jTextArea1.setBounds(7, 7, 371, 245);
                jTextArea1.setEnabled(false);
            }
            pack();
            setSize(400, 300);
        } catch (Exception e) {
            e.printStackTrace();
        }
        postInitGUI();
    }

    public void postInitGUI() {
        DisplayItems dp = new DisplayItems();
        jTextArea1 = dp;
        dp.setVisible(true);
        this.add(jTextArea1);
    }
}

And DisplayItems.java:

package graphicsex;

import java.awt.Dimension;
import java.awt.Graphics;

public class DisplayItems extends javax.swing.JTextArea {

    public DisplayItems() {
        super();
        initGUI();
    }

    private void initGUI() {
        try {
            setPreferredSize(new Dimension(400, 300));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void paint(Graphics g) {
        g.drawRect(10, 20, 50, 100);
        g.drawString("Kalai", 90, 150);
    }
}

Can any one help me display graphics components on any swing containers like JFrame,JPanelorJTextarea` etc.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
208 views
Welcome To Ask or Share your Answers For Others

1 Answer

It is inadvisable to override the paint method of a top level container...

JFrame contains a number of important layers onto which many other components are placed, buy doing this...

public void paint(Graphics g){
    g.drawRect(10,20,30,40);
}

You've successfully stopped any of the child components from begin painted, or in fact, anything other then your rectangle.

enter image description here

(The secret life of a top level swing container - Picture taken from How to use Root Panes)

While some might suggest simple adding a call to super.paint(g), I would not recommend it.

Better to use something like the JFrame's glass pane...This will allow you to paint over the components that reside within the frame. If you need to paint under the components, replace the JFrame's content pane instead.

You may find...

Of use...

Update

I think I'm beginning to see you problem with trying to perform custom painting on a text area.

The problem is, paintComponent paints the background AND the text in one foul swoop, meaning that any painting you do before calling super.paintComponent will be rendered over, but any painting done over it will paint over the text.

You could set the text area to be non-opaque and paint the background yourself...

enter image description here

public class CustomTextArea extends JTextArea {

    public CustomTextArea() {
        setOpaque(false);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.setColor(getBackground());
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.RED);
        g.fillRect(0, 0, 100, 100);
        super.paintComponent(g);
    }

}

The problem is though, it's easy for people to rest the opaque level and destroy your work. Sure you could override the setOpaque or getOpaque, but how do you known when the user actually wants to set the component transparent, so you can stop fill the back ground?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...