(Java) Component not showing on Panel

杀马特。学长 韩版系。学妹 提交于 2020-01-06 15:04:56

问题


I know this question was asked many times and frequently answered, i read some hours on this topic trying to get following to work, but couldn't manage to get it running.

I want to show the user a panel where he or she can click on to select points, remember the coordinates and do some stuff with them later.

I want to show the points when clicked, but they just don't show up.

The frame I paint around the panel does show up, but the points I set don't.

Would be glad for any advice given, im really new to java GUI programming.

//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);
    }
}

With this code i see the rectangle, but none of the point-ovals. Any clue where i'm wrong?


回答1:


The ovals should not be in a class that extends Component or any other GUI class, but instead should be in a "logical" class, in other words, a non-GUI class. Have DrawBoard do all the drawing, including iterating through its ArrayList (not array) of MyPoint objects, and drawing them. In addition all drawing should be done within the paintComponent override, not a paint override.

e.g.,

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);
    }
}


来源:https://stackoverflow.com/questions/34435794/java-component-not-showing-on-panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!