空白的JPanel,即使我画它

时间:2013-12-17 13:14:05

标签: java swing drawing jpanel paintcomponent

继续我前几天开始编写的一个简单程序,我有一个新的查询。我希望程序在面板中间绘制一个黑色圆圈。稍后我将使用按钮移动圆圈,但我还没在那个舞台上。程序运行没有错误,我得到一个白色面板,下面有我的按钮,但白色面板中间没有黑色圆圈。我搜索了之前推荐使用paintComponent的一些帖子,我已经完成但是我错过了一些东西,因为它没有按照我的预期工作,而且repaint()也不起作用。感谢任何提示。

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MovingArrows extends JFrame implements ActionListener {

    private JButton buttonUp, buttonDown, buttonLeft, buttonRight;
    private JPanel panel;
    private int xCircleCentre, yCircleCentre;

    final int xCircleCentreStarting = 250, yCircleCentreStarting = 250;
    final int RADIUS = 20;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        MovingArrows frame = new MovingArrows();
        frame.setSize(550, 600);

        frame.setVisible(true);
        frame.createGUI();
        // frame.repaint();

    }

    private void createGUI() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(new FlowLayout());

        panel = new JPanel();
        panel.setPreferredSize(new Dimension(500, 500));
        panel.setBackground(Color.white);

        window.add(panel);

        buttonUp = new JButton("Up");
        buttonDown = new JButton("Down");
        buttonLeft = new JButton("Left");
        buttonRight = new JButton("Right");
        window.add(buttonUp);
        window.add(buttonDown);
        window.add(buttonLeft);
        window.add(buttonRight);
        buttonUp.addActionListener(this);
        buttonDown.addActionListener(this);
        buttonLeft.addActionListener(this);
        buttonRight.addActionListener(this);
        // panel.repaint();

        Graphics paper = panel.getGraphics();

        paintComponent(paper);

    }

    public void paintComponent(Graphics g) {
        g.setColor(Color.black);
        g.fillOval(xCircleCentreStarting - RADIUS, yCircleCentreStarting
                - RADIUS, RADIUS * 2, RADIUS * 2);

    }

    @Override
    public void actionPerformed(ActionEvent event) {

        Graphics paper = panel.getGraphics();
        paper.setColor(Color.black);
        paper.fillOval(xCircleCentreStarting - RADIUS, yCircleCentreStarting
                - RADIUS, RADIUS * 2, RADIUS * 2);

    }
}

1 个答案:

答案 0 :(得分:0)

1)你试图在没有paintComponent()的JFrame上绘画,这是错误的。例如,您需要在paintComponent()的覆盖JPanel方法中进行自定义绘画。详细了解custom paintings

2)在你的actionPerformed()方法中,为了重绘椭圆形,你只需要调用repaint()方法,这样做就可以了,你不需要自己获取Graphics个实例并自己绘画。< / p>

我已经更改了您的代码,请检查:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MovingArrows extends JFrame implements ActionListener {

    private JButton buttonUp, buttonDown, buttonLeft, buttonRight;
    private int xCircleCentre, yCircleCentre;

    final int xCircleCentreStarting = 250, yCircleCentreStarting = 250;
    final int RADIUS = 20;

    public static void main(String[] args) {

        MovingArrows  frame = new MovingArrows();
        frame.createGUI();
        frame.pack();
        frame.setVisible(true);

    }

    private void createGUI() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel btnPanel = new JPanel();
        Container window = getContentPane();
        DrawHere drawHere = new DrawHere();
        drawHere.setPreferredSize(new Dimension(400,400));
        window.add(drawHere);
        window.add(btnPanel, BorderLayout.SOUTH);
        buttonUp = new JButton("Up");
        buttonDown = new JButton("Down");
        buttonLeft = new JButton("Left");
        buttonRight = new JButton("Right");
        btnPanel.add(buttonUp);
        btnPanel.add(buttonDown);
        btnPanel.add(buttonLeft);
        btnPanel.add(buttonRight);
        buttonUp.addActionListener(this);
        buttonDown.addActionListener(this);
        buttonLeft.addActionListener(this);
        buttonRight.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        repaint();
    }

    class DrawHere extends JPanel {

        public DrawHere(){
            setBackground(Color.WHITE);
        }


        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.black);
            g.fillOval(xCircleCentreStarting - RADIUS, yCircleCentreStarting
                    - RADIUS, RADIUS * 2, RADIUS * 2);

        }
    }
}

它看起来像:

enter image description here