Java Swing,我在绘图中使用了 for 循环,但只有最后一个元素呈现在面板上
Java Swing, I am using a for loop in draw but only the last element is rendered on panel
编辑为 MRE。我不太确定如何在不扩展 JFrame 或 JPanel 的情况下编写代码。这将重现我所看到的相同错误。我正在尝试在 JPanel 上渲染条形图,但似乎只绘制了 PlotPanel class 中 for 循环的最后一次迭代。
package com.company;
import java.awt.*;
import javax.swing.*;
public class VisualizeAlgorithms {
public static int initPosX = 0;
public static int initPosY = 0;
public static int numBars = 200;
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
PlotFrame frame = new PlotFrame();
});
}
}
class PlotFrame extends JFrame {
PlotPanel plotPanel;
PlotFrame() {
plotPanel = new PlotPanel();
this.add(plotPanel);
this.setTitle("Plot");
this.setBackground(Color.DARK_GRAY);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
this.setResizable(false);
this.setLocationRelativeTo(null);
}
public int[] createArray(int numBars) {
int[] numsArray = new int[numBars];
for (int i = 0; i < numBars; i++) {
numsArray[i] = i + 1;
}
return numsArray;
}
}
class PlotPanel extends JPanel{
static final int PLOT_WIDTH = 1200;
static final int PLOT_HEIGHT = 800;
static final int MAX_BAR_HEIGHT = PLOT_HEIGHT;
static final int BAR_WIDTH = PLOT_WIDTH / VisualizeAlgorithms.numBars;
Dimension plotSize = new Dimension(PLOT_WIDTH, PLOT_HEIGHT);
PlotPanel() {
this.setPreferredSize(plotSize);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.CYAN);
draw(g);
}
public void draw(Graphics g){
int numBars = VisualizeAlgorithms.numBars;
for (int i = 0; i < numBars; i++) {
g.fillRect(BAR_WIDTH * i, VisualizeAlgorithms.initPosY, BAR_WIDTH, ((i+1)/numBars)*(MAX_BAR_HEIGHT));
}
}
}
不确定我是否可以接受评论作为答案,所以我在这里发布了答案。它确实将矩形高度更改为 int h = ((i+1)*MAX_BAR_HEIGHT/numBars)
。感谢@AndrewThompson 和@c0der 不仅提供了答案,还提供了其他信息。
旁白:@c0der 给出了很好的建议“如有疑问,请打印出来”。我建议使用此更改:
public void draw(Graphics g){
int numBars = VisualizeAlgorithms.numBars;
for (int i = 0; i < numBars; i++) {
int x = BAR_WIDTH * I;
int y = VisualizeAlgorithms.initPosY;
int w = BAR_WIDTH;
int h = ((i+1)/numBars)*(MAX_BAR_HEIGHT);
System.out.println(String.format("x,y WxH: %1s,%1s %1sx%1s", x,y,w,h));
g.fillRect(x,y,w,h);
}
}
(打印用于 fillRect 的值)
编辑为 MRE。我不太确定如何在不扩展 JFrame 或 JPanel 的情况下编写代码。这将重现我所看到的相同错误。我正在尝试在 JPanel 上渲染条形图,但似乎只绘制了 PlotPanel class 中 for 循环的最后一次迭代。
package com.company;
import java.awt.*;
import javax.swing.*;
public class VisualizeAlgorithms {
public static int initPosX = 0;
public static int initPosY = 0;
public static int numBars = 200;
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
PlotFrame frame = new PlotFrame();
});
}
}
class PlotFrame extends JFrame {
PlotPanel plotPanel;
PlotFrame() {
plotPanel = new PlotPanel();
this.add(plotPanel);
this.setTitle("Plot");
this.setBackground(Color.DARK_GRAY);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
this.setResizable(false);
this.setLocationRelativeTo(null);
}
public int[] createArray(int numBars) {
int[] numsArray = new int[numBars];
for (int i = 0; i < numBars; i++) {
numsArray[i] = i + 1;
}
return numsArray;
}
}
class PlotPanel extends JPanel{
static final int PLOT_WIDTH = 1200;
static final int PLOT_HEIGHT = 800;
static final int MAX_BAR_HEIGHT = PLOT_HEIGHT;
static final int BAR_WIDTH = PLOT_WIDTH / VisualizeAlgorithms.numBars;
Dimension plotSize = new Dimension(PLOT_WIDTH, PLOT_HEIGHT);
PlotPanel() {
this.setPreferredSize(plotSize);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.CYAN);
draw(g);
}
public void draw(Graphics g){
int numBars = VisualizeAlgorithms.numBars;
for (int i = 0; i < numBars; i++) {
g.fillRect(BAR_WIDTH * i, VisualizeAlgorithms.initPosY, BAR_WIDTH, ((i+1)/numBars)*(MAX_BAR_HEIGHT));
}
}
}
不确定我是否可以接受评论作为答案,所以我在这里发布了答案。它确实将矩形高度更改为 int h = ((i+1)*MAX_BAR_HEIGHT/numBars)
。感谢@AndrewThompson 和@c0der 不仅提供了答案,还提供了其他信息。
旁白:@c0der 给出了很好的建议“如有疑问,请打印出来”。我建议使用此更改:
public void draw(Graphics g){
int numBars = VisualizeAlgorithms.numBars;
for (int i = 0; i < numBars; i++) {
int x = BAR_WIDTH * I;
int y = VisualizeAlgorithms.initPosY;
int w = BAR_WIDTH;
int h = ((i+1)/numBars)*(MAX_BAR_HEIGHT);
System.out.println(String.format("x,y WxH: %1s,%1s %1sx%1s", x,y,w,h));
g.fillRect(x,y,w,h);
}
}
(打印用于 fillRect 的值)