(Java)组件未显示在Panel上

时间:2015-12-23 12:31:56

标签: java swing jpanel repaint

我知道这个问题被多次询问并且经常回答,我在这个主题上读了几个小时试图继续工作,但无法让它运行起来。

我想向用户显示一个面板,他或她可以点击该面板来选择点,记住坐标并稍后用它们做一些事情。

我想在点击时显示点数,但它们只是没有显示出来。

我在面板周围绘制的框架确实显示出来,但我设置的点没有显示。

对于给出的任何建议都很高兴,我对java GUI编程非常陌生。

//in main class
SwingUtilities.invokeLater(() -> {
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    drawBoard = new DrawBoard();
    drawBoard.setOpaque(true);
    container conti = getContentPane();
    conti.setLayout(new BorderLayout());
    conti.add(drawBoard, BorderLayout.CENTER);
    setLocation(400, 200);
    setSize(1000, 600);
    setVisible(true);
    setResizable(false);
});

int timerDelay = 200;
new Timer(timerDelay, e -> {
    drawBoard.repaint();
}).start();
//end of main class

public class drawBoard extends JPanel {
    int n = 0; 
    MyPoint[] myPoints = new MyPoint[5];

    public drawBoard(){
        MyMouseAdapter ma = new MyMouseAdapter(this);
        this.addMouseListener(ma);
    }

    public void addMyPoint(Point p){
        this.myPoints[this.n] = new MyPoint(p);
        add(myPoints[this.n]); 
        n++;                // overflow ignored cause example
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawRect(0,0,600,500);    
    }

}

public class MyMouseAdapter extends MouseAdapter{
    drawBoard owner;

    public MyMouseAdapter(drawBoard owner){
        this.owner = owner;
    }

    public void mouseClicked(MouseEvent b) {
        owner.addMyPoint(b.getPoint());
        owner.invalidate();
        owner.repaint();
    }   
}

public class MyPoint extends Component {
    public int x;
    public int y;

    public MyPoint(Point p){
        x = (int)p.getX();
        y = (int)p.getY();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawOval(x - 3, y - 3, 6, 6);
    }
}

使用此代码,我看到矩形,但没有点椭圆。我错了的任何线索?

1 个答案:

答案 0 :(得分:2)

椭圆不应该在扩展Component或任何其他GUI类的类中,而应该在"逻辑"类,换句话说,非GUI类。让DrawBoard完成所有绘图,包括迭代MyPoint对象的ArrayList(不是数组),并绘制它们。此外,所有绘图都应该在paintComponent覆盖范围内完成,而不是绘制覆盖。

如,

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class MainFoo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            createAndShowGui();
        });
    }

    private static void createAndShowGui() {
        DrawBoard mainPanel = new DrawBoard();
        JFrame frame = new JFrame("Main Foo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

// DrawBoard should start with a capital letter
class DrawBoard extends JPanel {
    private static final int PREF_W = 1000;
    private static final int PREF_H = 600;
    int n = 0;
    List<MyPoint> myPoints = new ArrayList<>();

    public DrawBoard() {
        MyMouseAdapter ma = new MyMouseAdapter(this);
        this.addMouseListener(ma);
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        } else {
            return new Dimension(PREF_W, PREF_H);
        }
    }

    public void addMyPoint(Point p) {
        MyPoint myPoint = new MyPoint(p);
        myPoints.add(myPoint);
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawRect(0, 0, 600, 500);

        // use Graphics2D to allow for smoothing
        Graphics2D g2 = (Graphics2D) g;

        // smooth with rendering hiints
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // iterate through each MyPoint in the list
        for (MyPoint myPoint : myPoints) {
            myPoint.draw(g2); // and draw it
        }

    }

}

class MyMouseAdapter extends MouseAdapter {
    DrawBoard owner;

    public MyMouseAdapter(DrawBoard owner) {
        this.owner = owner;
    }

    // btetter to use mousePressed, not mouseClicked
    @Override
    public void mousePressed(MouseEvent b) {
        owner.addMyPoint(b.getPoint());
    }
}

class MyPoint {
    public int x;
    public int y;

    public MyPoint(Point p) {
        x = (int) p.getX();
        y = (int) p.getY();
    }

    public void draw(Graphics2D g2) {
        g2.drawOval(x - 3, y - 3, 6, 6);
    }
}