Swing Graphics on JFrame

前端 未结 2 1450
傲寒
傲寒 2020-12-11 13:52

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 com

相关标签:
2条回答
  • 2020-12-11 14:16

    * Using jPanel *

    using jpanel, the graphics elements can be drawn;

    import java.awt.Graphics;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.WindowConstants;
    
    public class GraphicsEx1 extends javax.swing.JFrame {
    private JScrollPane jsp1;
    private JTextArea jta1;
    private JPanel jpnl1;
    
    {
    //Set Look & Feel
    try {
    javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
    } catch(Exception e) {e.printStackTrace();}
    }
    
    public static void main(String[] args) {
    GraphicsEx1 inst = new GraphicsEx1();
    inst.setLocationRelativeTo(null);
    inst.setVisible(true);
    }
    
    public GraphicsEx1() {
    super();
    initGUI();
    postInitGUI();
    }
    
    private void initGUI() {
    try {
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    getContentPane().setLayout(null);
    {
    jsp1 = new JScrollPane();
    getContentPane().add(jsp1);
    jsp1.setBounds(10, 32, 372, 223);
    {
    jpnl1 = new JPanel();    //<----------------
    jsp1.setViewportView(jpnl1);    //<----------------
    jpnl1.setBackground(new java.awt.Color(255,255,255));
    jpnl1.setLayout(null);
    //jpnl1.setPreferredSize(new java.awt.Dimension(359, 327));
    }
    }
    pack();
    setSize(400, 300);
    } catch (Exception e) {e.printStackTrace();}
    }
    
    public void postInitGUI(){
    frmGrpView gp=new frmGrpView();
    jta1=new JTextArea();
    jta1=gp;
    jta1.setBounds(0,0, 336, 197);
    jta1.setVisible(true);
    //jpnl1.setBounds(0, 0, 336, 197);
    jpnl1.add(jta1);    //<----------------
    jpnl1.revalidate();
    jsp1.revalidate();
    }
    }
    //----------------------- Second Class --------------------------
    class frmGrpView extends JTextArea{
    public frmGrpView() {
    super();
    setEditable(false);
    }
    public void paint(Graphics g){
    super.paint(g);
    g.drawRect(10, 10, 100, 100);
    }
    }
    
    0 讨论(0)
  • 2020-12-11 14:18

    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...

    • Performing Custom Painting
    • Painting in AWT and Swing
    • How to Decorate Components with the JLayer Class

    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?

    0 讨论(0)
提交回复
热议问题