gpt4 book ai didi

java - 在 Swing GUI Java 面板中绘制多边形

转载 作者:行者123 更新时间:2023-12-01 18:35:44 27 4
gpt4 key购买 nike

我试图弄清楚如何在 Swing GUI 的面板中使用 DrawPolygon,同时学习 Java GUI 的基础知识。

这是生成 Swing GUI 面板的代码:

polygonArea = new javax.swing.JPanel(){
protected void poly(Graphics g) {
int xpoints[] = {35, 10, 25, 60, 20};
int ypoints[] = {105, 25, 5, 105, 25};
int xpoints2[] = {60, 70, 92, 80, 82};
int ypoints2[] = {105, 25, 5, 105, 25};
int xpoints3[] = {102, 98, 145, 107};
int ypoints3[] = {5, 105, 105, 100};
int npoints = 5;
int npointsl = 4;

g.fillPolygon(xpoints, ypoints, npoints);
g.fillPolygon(xpoints2, ypoints2, npoints);
g.fillPolygon(xpoints3, ypoints3, npointsl);
}
};
polygonArea.setBackground(new java.awt.Color(240, 240, 240));

基于 Netbeans 生成的 GUI。我对 Java 很陌生,但是当我启动该文件时,它看起来像这样:

enter image description here

/image/4KsIo.jpg

而不是 Poly 函数本身显示:

enter image description here

/image/XrAsK.png

抱歉,如果这是一个非常明显的错误,我们将不胜感激!

(由于声誉问题,无法发布图片)

最佳答案

方法 poly 不会在 Swing 的绘制堆栈中自动调用。例如,您需要明确执行此操作

class PolyPanel extends JPanel {
protected void poly(Graphics g) {
...
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
poly(g);
}
}

关于java - 在 Swing GUI Java 面板中绘制多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22120843/

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