JComponent 不会显示图像

发布于 2025-01-04 16:55:54 字数 2287 浏览 1 评论 0原文

我是一名初学者程序员,我正在尝试让这个程序运行。尽管 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);

}
}

Im a beginner programmer and Im trying to get this program to run. Everything compiles correctly although no images appear on the new Java screen for the JComponent. Pretty much what this program is suppose to do is take an input value and assign it to to the values of a bar chart for size. In the program I have to use an exterior paintcomponent class to run the original class as well as have a driver which is probably what is throwing me off. Thanks in Advance!

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

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望喜 2025-01-11 16:55:54

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

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

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

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

Your main problem is that the code intended do draw will never be called, since you are not implementing the paintComponent(Graphics g) method. So, replace your paintComponent for this:

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

Then make the corrections suggested by Stevens.

Also, in the second for, you are initializing the variable x (int x = 20). This way, x will be 20 at every iteration of the loop. Do this before the for loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文