不用Java绘制图形

时间:2015-01-11 15:08:55

标签: java swing

嘿伙计们我正在尝试用Java构建自定义形状,并且由于某种原因它不会将它们绘制到我的Canvas中。

我创建了一个类:

public class MyCircleCanvas extends JComponent{
    private void doDrawing(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(new Color(150, 150, 150));

        //g2d.fillRect(30, 20, 50, 50);
        //g2d.fillRect(120, 20, 90, 60);
        //g2d.fillRoundRect(250, 20, 70, 60, 25, 25);

        //g2d.fill(new Ellipse2D.Double(10, 100, 80, 100));
        //g2d.fillArc(120, 130, 110, 100, 5, 150);
        g2d.fillOval(270, 130, 50, 50);   
   } 

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        doDrawing(g);
    }    

}

然后在我的JFrame中调用此类:

private void initComponents() {
        createMenu();
        createToolBar();
        createCenterPanel();
        //createCanvas();
    }

private void createCanvas() {
        //c = new Canvas();
        //c.setBackground(Color.white);
        // this.add(c);
        add(new MyCircleCanvas());
    }

这很好用。但是当我尝试从JButton的ActionListener调用方法createCanvas()时,它不会创建我想要的形状。任何建议???

2 个答案:

答案 0 :(得分:1)

您应该覆盖paint方法而不是paintComponent。要将MyCircleCanvas ses添加到图形用户界面,我使用JPanel创建了BoxLayout。在此面板中,新添加的MyCircleCanvas ses将垂直添加。

确保覆盖getPreferredSize方法,因为在将MyCircleCanvas添加到用户界面时,它的大小不会是可见的。

此外,pack方法可确保JFrame的大小合适,以包含新的MyCircleCanvas

    public class MyCircleCanvas extends JComponent {
    private void doDrawing(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(new Color(150, 150, 150));
        g2d.fillOval(25, 25, 50, 50);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        doDrawing(g);
    }

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

这个框架:

public class CircleCanvasFrame extends JFrame implements ActionListener {
    private JPanel circlePanel = new JPanel();

    public CircleCanvasFrame() {
        setVisible(true);
        setSize(400, 400);
        circlePanel.setLayout(new BoxLayout(circlePanel, BoxLayout.Y_AXIS));
        JButton button = new JButton("add a circle canvas");
        button.addActionListener(this);
        add(button, BorderLayout.NORTH);
        add(circlePanel, BorderLayout.CENTER);
    }

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

    @Override
    public void actionPerformed(ActionEvent e) {
        circlePanel.add(new MyCircleCanvas());
        pack();
    }
}

答案 1 :(得分:0)

你的问题是你将椭圆定位得太远而无法看到。使位置更接近原点。

private void doDrawing(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.YELLOW);
        g2d.fillOval(0, 0, 50, 50);   
   } 

以下是关于如何在此之后完成的大致想法:

EventQueue.invokeLater(new Runnable() {
            public void run(){
                final JFrame frame = new JFrame();
                JButton button = new JButton("test");
                frame.setLayout(new FlowLayout());

                button.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {

                        MyCircleCanvas circle = new MyCircleCanvas();
                        frame.add(circle);
                        frame.pack();

                    }
                });

                frame.add(button);
                frame.setSize(100,100);
                frame.setVisible(true);
            }
        });