Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

java - Drawing rectangle on a JPanel

I want to draw a recangle on a JPanel. Am able to draw with the following code.

public class DrawingColor extends JFrame
{
    public static void main(String[] args) 
    {
        DrawingColor d = new DrawingColor();
    }

    public DrawingColor()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().add(new MyComponent());
        setSize(400,400);
        setVisible(true);
    }

    public class MyComponent extends JComponent
    {
        @Override
        public void paint(Graphics g)
        {
            int height = 200;
            int width = 120;
            g.setColor(Color.red);
            g.drawRect(10, 10, height, width);
            g.setColor(Color.gray);
            g.fillRect(11, 11, height, width);
            g.setColor(Color.red);
            g.drawOval(250, 20, height, width);
            g.setColor(Color.magenta);
            g.fillOval(249, 19, height, width);
        }
    }
}

But getContentPane().add(new MyComponent()); Instead of this statement,I need to add one base panel to the frame. On the base panel I want to add the MyComponent panel.

  JPanel basePanel = new JPanel();
  basePanel = new MyComponent();
  getContentPane().add(basePanel);

If I do like this, the rectangle is not getting visible. any idea? And also I need to change the size of the rectangle at runtime. is it possible?

Question&Answers:os

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

1 Reply

0 votes
by (71.8m points)

1) for Swing JComponent you have to use paintComponent() instead of paint() method

2) your JComponent doesn't returns PreferredSize

for example

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ComponentEvent;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class CustomComponent extends JFrame {

    private static final long serialVersionUID = 1L;

    public CustomComponent() {
        setTitle("Custom Component Graphics2D");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void display() {
        CustomComponents cc = new CustomComponents();
        /*cc.addComponentListener(new java.awt.event.ComponentAdapter() {

            @Override
            public void componentResized(ComponentEvent event) {
                setSize(Math.min(getPreferredSize().width, getWidth()),
                        Math.min(getPreferredSize().height, getHeight()));
            }
        });*/
        add(cc, BorderLayout.CENTER);

        CustomComponents cc1 = new CustomComponents();
        add(cc1, BorderLayout.EAST);

        pack();
        // enforces the minimum size of both frame and component
        setMinimumSize(getSize());
        //setMaximumSize(getMaximumSize());
        setVisible(true);

    }

    public static void main(String[] args) {
        CustomComponent main = new CustomComponent();
        main.display();
    }
}

class CustomComponents extends JComponent {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(100, 100);
    }

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

    @Override
    public Dimension getMaximumSize() {
        return new Dimension(800, 600);
    }

    @Override
    public void paintComponent(Graphics g) {
        int margin = 10;
        Dimension dim = getSize();
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...