java - JComponent 不会显示图像

标签 java swing graphics2d jcomponent paintcomponent

我是一名初学者程序员,我正在尝试让这个程序运行。尽管 JComponent 的新 Java 屏幕上没有出现任何图像,但一切都可以正确编译。该程序要做的几乎就是获取一个输入值并将其分配给条形图的大小值。在程序中,我必须使用外部油漆组件类来运行原始类以及驱动程序,这可能是让我失望的原因。提前致谢!

public class BarChartTester
{
public static void main(String[] args)
  {

      BarChartPaintComponent component = new BarChartPaintComponent();
      Scanner in = new Scanner(System.in);
      System.out.println("Enter the Values you wish to use (>0). Press -1 on an empty line to stop");
      Boolean flag = false;
      while(!flag)
      {
           double numbers = in.nextDouble();
           if(numbers == -1)
           flag = true;
           else if(numbers<-1)
           System.out.println("You have typed in invalid number");
           else
           component.add(numbers);
     }
      JFrame frame = new JFrame();
      frame.setSize(300, 300);
      frame.setTitle("A Bar Graph");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(component);
      frame.setVisible(true);
    }

}

public class BarChart extends JComponent
{
private ArrayList<Double> list;
private double value;
private int i;

public BarChart()
{

    list = new ArrayList<Double>();
}


public void add(double value)
{
    list.add(i, value);
    i++;
}

public void draw(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;

    Double greatest = list.get(0);
    Double least;
    for(int j =1;j<=list.size();j++)
    {
        if(list.get(j)> greatest)
        greatest = list.get(j);
        else
        least = list.get(j);


    }

    for(int i = 0;i<=list.size();i++)
    {
        int x = 20;
        int width = 20;
        double barNumber = list.get(i);
        double size = barNumber;
        if(list.get(i) == greatest){
        g2.setPaint(Color.BLUE);
        g2.fill(new Rectangle2D.Double(x,300,width,300));
        }
        else
        {
            g2.setPaint(Color.BLUE);
             g2.fill(new Rectangle2D.Double(x,300,width, barNumber));
         }

        x +=20;

    }



}










}


public class BarChartPaintComponent extends BarChart
{
public void paintComponent(Graphics g, double array){
    Graphics2D g2 = (Graphics2D) g;
    BarChart component= new BarChart();
    component.add(array);
    component.draw(g2);

}
}

最佳答案

您的主要问题是,打算绘制的代码永远不会被调用,因为您没有实现paintComponent(Graphics g)方法。因此,替换你的paintComponent:

public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    this.draw(g2);
}

然后进行史蒂文斯建议的更正。

此外,在第二个 for 中,您正在初始化变量 x (int x = 20)。这样,每次循环迭代时 x 都将为 20。在 for 循环之前执行此操作。

关于java - JComponent 不会显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9237200/

相关文章:

java - 如何根据字符串、空格和符号的组合进行拆分?

java - Spring MVC 仅发布选定的数据

java - 将 JFrame 保存为图像文件(.JPG、.PNG、.BMP 等)

java - component.get MouseMotionListener 返回多个

java - 使用 arrayList 作为 Graphics2d 中的 y 坐标

Java - 学习继承,使用Graphics2D,调整框架大小时重绘对象

java - 指纹匹配算法!

java - 限制 JScrollPane View 宽度

java - 为什么当循环运行时 MouseListener 不起作用?

java - 在运行作为 JAR 存档分发的项目时加载图像等资源