簡體   English   中英

現有JPanel內的繪圖圓

[英]Drawing circle inside an existing JPanel

我遵循了peeskillet的操作,並根據他的最后一個想法更改了密碼。 這是更改的代碼

CriclePanel類

    public class CirclePanel extends JPanel {
    int centerX, centerY, radius;
    Color circle_color;

    public void setCircle(int centerX, int centerY, int radius, Color c) {
        this.centerX = centerX;
        this.centerY = centerY;
        this.radius = radius;
        circle_color = c;
        revalidate();
        repaint();
    }

    protected void paintComponent(Graphics g) {
        System.out.println("Trying to draw circle");
        super.paintComponent(g);
        g.fillOval(centerX, centerY, radius*2, radius*2);
    }
}

網格面板(從調色板添加並自定義構造)

myGridPanel = new JPanel(new GridLayout(8,8));
panels = new CirclePanel[8][8];
    for (int i = 0; i < panels.length; i++) {
        for (int j = 0; j < panels[i].length; j++) {
            CirclePanel panel = new CirclePanel();
            panels[i][j] = panel;
            myGridPanel.add(panel);
        }
    }

嘗試添加繪制圓:

if(dType==DiceType.blackDice){
            System.err.println("black @"+i+","+j);
            panels[i][j].setCircle(x, y, radius, Color.BLACK);
        }else{
            System.err.println("white @"+i+","+j);
            panels[i][j].setCircle(x, y, radius, Color.WHITE);
        }

但是,沒有在網格面板上繪制圓,並且在母面板“ myGridPanel”上也沒有網格。 我看到沒有調用CirclePanel的paintComponant()。

輸出: http : //s25.postimg.org/v8u398dun/no_Gridnd_Circle.png

正如我在評論中提到的,你可以優化您的設計通過不使每Circle的面板。 這是一個例子

另一個適合您當前設計的選項是在每個圓形面板中都有一個設置器,您可以為其傳遞圓形模型。

public class CirclePanel extends JPanel {
    private Circle circle;

    public void setCircle(Circle cirlce) {
        this.circle = circle;
        repaint();
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (circle != null) {
            // get the state from circle and paint
            g.fillOval(circle.getX(), circle.getY(), getWidth(), getHeight());
        }
    }
}

注意:在這種情況下, Circle 不是面板

然后為gridPanel提供一個GridLayout並將所有CirclePanel添加到其中。

JPanel gridPanel = new JPanel(new GridLayout(5, 5));
CirclePanel[][] panels = new CirclePanel[5][5];
for (int i = 0; i < panels.length; i++) {
    for (int j = 0; j < panels[i].length; j++) {
        CirclePanel panel = new CirclePanel();
        panels[i][j] = panel;
        gridPanels.add(panel);
    }
}

這樣做, gridPanel中將有一個空的CirclePanel 然后,當您想在其中一個面板上繪制一個圓圈時,可以執行以下操作

Circle circle = new Circle(...);
panels[1][1].setCircle(circle);

更新

或者現在我考慮一下,您甚至根本不需要Circle類,因為您可以將圓形繪制為0, 0, getWidth(), getHeight() 您可以簡單地設置一個標志來繪制。 但是,如果您想為圓添加更多狀態,則可以保留一個圓類。 但是,如果沒有,它可能看起來像

public class CirclePanel extends JPanel {
    private boolean draw;
    private Color color;

    // setter for color
    // setter for draw

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (draw) {
            // get the state from circle and paint
            g.fillOval(0, 0, getWidth(), getHeight());
        }
    }
}

用示例更新

參見方法setDraw 那是我設置是否抽簽的標志。 我在Circle類的鼠標偵聽器中調用它,但是您可以在任何地方調用它。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class CircleDemo {
    private static final int GRID_SIZE = 5;
    private Circle[][] circlePanels 
            = new Circle[GRID_SIZE][GRID_SIZE];
    private JPanel gridPanel 
            = new JPanel(new GridLayout(GRID_SIZE, GRID_SIZE));

    public CircleDemo() {
        initCircles();
        JFrame f = new JFrame();
        f.add(gridPanel);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private void initCircles() {
        for (int i = 0; i < circlePanels.length; i++) {
            for (int j = 0; j < circlePanels[i].length; j++) {
                Circle circle = new Circle();
                circlePanels[i][j] = circle;
                gridPanel.add(circle);
            }
        }
    }

    class Circle extends JPanel {
        private boolean draw = false;
        private Color color = Color.BLUE;

        public Circle() {
            setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
            addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e) {
                    if (isDraw()) {
                        setDraw(false);
                    } else {
                        setDraw(true);
                    }  
                }
            });
        }

        public boolean isDraw() {
            return draw;
        }

        public void setDraw(boolean draw) {
            this.draw = draw;
            repaint();
        }

        public void setColor(Color color) {
            this.color = color;
            repaint();
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (draw) {
                g.setColor(color);
                g.fillOval(0, 0, getWidth(), getHeight());
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new CircleDemo();
            }
        });
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM