gpt4 book ai didi

java - 在现有 JPanel 内绘制圆圈

转载 作者:行者123 更新时间:2023-12-02 05:17:09 25 4
gpt4 key购买 nike

我已经关注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

最佳答案

正如我在评论中提到的,您可以 Refine your Design不让每个Circle成为一个面板。这是an example

适合您当前设计的另一个选项是在每个圆形面板中都有一个 setter ,您可以为其传递圆形模型。

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

注意:在这种情况下不是面板

然后对于 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 类,因为你可以用 a 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();
}
});
}
}

关于java - 在现有 JPanel 内绘制圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26827914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com