1

我正在尝试编写一些自定义绘画代码。具体来说,我想要一堆扩展的 JPanel 来绘制我的 GUI 的不同方面,但是这些扩展面板中的每一个都包含如何绘制它的说明。

我已经创建了代码,但是由于某种原因,无论我做什么,扩展的 JPanel 都没有被绘制在我的 JFrame 中的主 JPanel 上。这是我的主要课程的要点和我的扩展 JPanels之一。我错过了什么?

爆发

//Java imports
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JPanel;
//Personal imports
import Ball;

public class Breakout {

    public static void main (String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {//start the GUI in a new thread
            public void run(){
                showGUI();
            }
        });
    }

    private static void showGUI() {
        JFrame frame = new JFrame("Breakout");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Dimension d = new Dimension(640,480);
        frame.setMinimumSize(d);
        frame.setResizable(false);
        JPanel p = new JPanel();
        p.add(new Ball(200,200,50,255,0,0));
        frame.add(p);
        frame.setVisible(true);
    }

}

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;

public class Ball extends JPanel {

    public int x;
    public int y;
    public int radius;
    public Color colour;

    public Ball(int x, int y, int radius, int r, int g, int b) {
        super();
        this.x = x;
        this.y = y;
        this.radius = radius;
        colour = new Color(r,g,b);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //define constants
        int topLeftX = x+radius;
        int topLeftY = y+radius;
        int diameter = radius *2;
        //draw outline
        g.setColor(Color.BLACK);
        g.drawOval(topLeftX, topLeftY, diameter, diameter);
        //fill it in
        g.setColor(colour);
        g.fillOval(topLeftX, topLeftY, diameter, diameter);
    }
}
4

2 回答 2

3

Using JPanels in this way is going to cause you no end of problems.

The two main problems you have are...

  1. JPanels already have an idea of size and position, adding another x/y coordinate is just confusing and could lead you to painting off the components viewable space
  2. The default preferred size of a JPanel is 0x0. This means when you add it another JPanel, using FlowLayout, the panel is given a size of 0x0, so nothing gets painted.

Instead, create an interface which has a method called paint and takes a Graphics2D object.

For each shape you want to paint, create a new class which implements this interface and use it's paint method to paint the object as you see fit.

Create a custom component, extending from JPanel and maintain a List of these shapes. In it's paintComponent, use a for-loop to paint each shape in the List.

This custom component should then be added to your frame...

于 2014-03-02T03:27:44.720 回答
0

showGUI主类的方法中,您有以下代码:

JPanel p = new JPanel();
p.add(new Ball(200,200,50,255,0,0));
frame.add(p);

此代码创建一个新的 JPanel,然后向其中添加另一个JPanel。这是不正确的,因为将另一个添加到您刚刚创建JPanel的完美产品中根本没有意义。JPanel而是这样做:

frame.getContentPane().add(new Ball(200, 200, 50, 255,0,0));

或者,如果您愿意:

Ball ball = new Ball(200, 200, 50, 255,0,0);
frame.getContentPane().add(ball);
于 2014-03-02T02:59:12.510 回答