【问题标题】:How can I pass values to paintComponent如何将值传递给paintComponent
【发布时间】:2018-12-19 18:38:20
【问题描述】:

我有一个包含 100 个随机整数的数组。我想用它们创建一个条形图。我知道如何在框架中创建单个矩形但不传递值。

这是绘图类:

import javax.swing.*;
import java.awt.*;

public class draw extends JPanel
{
    public void drawing() {

        repaint();
    }

public void paintComponent(Graphics g) {

        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.fillRect(100, 150, 12, 3);
    }
}

我想用我的随机 int 值替换 g.fillRect(100,150,12,3) 中的值。但是由于我从 main 调用 repaint() 来调用paintComponent,所以我无法将值传递给paintComponent。我该怎么做?如果不可能,我有什么选择?

【问题讨论】:

  • 为什么不把这些值放在一个类字段中?
  • 这里的主要问题是您自己没有显式调用paintComponent,swing框架会为您完成。因此,即使你可以向它传递参数,它也是毫无意义的。相反,您应该使用函数调用让 paintComponent 请求值,或者将先前在类中定义的值作为实例变量。

标签: java swing graphics


【解决方案1】:

您可以通过引入字段和初始化然后使用构造函数、设置器或两者来做到这一点:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Draw extends JPanel {

    int x, y, width, height;
    public Draw(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    void setX(int x) {this.x = x;}

    void setY(int y) {this.y = y;}

    void setWidth(int width) {this.width = width;}

    void setHeight(int height) {this.height = height;}

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.fillRect(x, y, width, height);
    }
}

【讨论】:

    【解决方案2】:

    如何将值传递给paintComponent

    您不能将值传递给paintComponent()

    但是由于我从 main 调用 repaint() 来调用paintComponent,我无法将值传递给paintComponent。我该怎么做?

    对于您想要自定义和自己绘制的任何对象,您都可以为此创建一个类并使用draw() 之类的方法来进行绘制,例如:

    //Just a normal class with a draw() method
    class BarGraph{
        private int x;
        private int y;
        private int width;
        private int height;
        private Color color;
    
        public BarGraph(int x, int y, int width, int height){
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }
    
        public void setColor(Color color){
            this.color = color;
        }
    
        public void draw(Graphics g){
            g.setColor(color);
            g.fillRect(x, y, width, height);
        }
    }
    

    然后在您的主面板中显示您的自定义图像:

    class DrawingSpace extends JPanel{
        private BarGraph barGraph;
    
        public DrawingSpace(){
            barGraph = new BarGraph(50, 50, 400, 100);
            barGraph.setColor = (Color.RED);
        }
    
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g){
                barGraph.draw(g);        //draw your own customized object
            }
        }
    }
    

    因此,您可以在自己的类(在本例中为 BarGraph 类)中传递/设置值,而不是尝试传递值来指示paintComponent 的绘制方式。从 Graphics 内容中,实现您希望它的绘制方式。

    在您的paintComponent 中,只需调用已经有“一组说明”的draw 方法来说明它应该如何绘制。

    【讨论】:

      【解决方案3】:

      我们不能将值作为参数传递给paintComponent 方法,因为我们只是覆盖了由 Swing 调用的现有方法。但是我们可以在类中有一个实例变量(在我的示例中为values),它可以在paintComponent 方法中访问(如其他答案/cmets 中所建议的那样)。

      我在下面放了一个示例程序,因为有时示例可以更好地传达想法。

      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import java.awt.BorderLayout;
      import java.awt.Color;
      import java.awt.Graphics;
      
      public class BarGraph extends JPanel
      {
        private static final int GRAPH_HEIGHT = 300;
        private static final int BAR_WIDTH = 50;
        private static final int GAP_BETWEEN_BARS = 20;
        private static final int GRAPH_X_OFFSET = 50;
      
        private int[] values;
      
        public BarGraph(int[] values)
        {
          this.values = values;
        }
      
        @Override
        protected void paintComponent(Graphics g)
        {
          super.paintComponent(g);
          g.setColor(Color.BLUE);
          for (int i = 0; i < values.length; i++)
          {
            g.fillRect(
                GRAPH_X_OFFSET + (i * (GAP_BETWEEN_BARS + BAR_WIDTH)),
                GRAPH_HEIGHT - values[i],
                BAR_WIDTH,
                values[i]
            );
          }
        }
      
        public static void main(String[] args)
        {
          int[] graphValues = {100, 150, 50, 250, 200, 75};
      
          JFrame frame = new JFrame("Graph");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new BarGraph(graphValues), BorderLayout.CENTER);
          frame.setBounds(300, 200, 600, 400);
          frame.setVisible(true);
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-01
        • 2021-08-14
        • 2021-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-19
        相关资源
        最近更新 更多