简体   繁体   中英

How do I pass an array as parameter to a Java graphics function?

I am currently trying to pass a boolean array from another java class to my paint function, currently it get's passed along perfectly fine to the Draw function below. However I cannot access it from the Paint function.

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

public class Draw extends JPanel{

public static void Draw(boolean[] array){
    JFrame f = new JFrame ("Program");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(1200, 1000);
    f.setLocation(400, 30);
    f.setVisible(true);
    f.getContentPane().add(new Draw());
}

public void paint (Graphics g){

super.paint(g);
g.setColor(Color.black);

 for(int i = 0; i < array.length; i++){
           if (array[i]){
          g.drawLine(i, i, i, i);
 //I know the line above draws a diagonal line, will fix it later! 

               }
           }
}

What I would like to do is just use the array as a parameter like this:

public void paint (Graphics g, boolean[] array){

super.paint(g);
g.setColor(Color.black);

 for(int i = 0; i < array.length; i++){
           if (array[i]){
          g.drawLine(i, i, i, i);

               }
           }
}

But that doesn't work and if I try to pass the array directly to paint from my other class it doesn't work either because one of them is static and the other one isn't. I've tried searching all over Google, however none of the solutions seem to work. So my question is how would I go about making the paint function accept the values of my array?

Start by taking a look at Performing Custom Painting and Painting in AWT and Swing for a better understanding of how the painting system works in Swing.

The paint methods are hooks through which the painting system asks your component to repaint itself, you can't change the signature and hope that it will be called.

Instead, you need to implement one of these hooks (preferable paintComponent ) and when called, call any additional methods you need in order to paint the current state of the component.

Start by defining the array you want to use as an instance field, then in your paint method, simply reference it and perform the operations you need...

public class Draw extends JPanel{
    // Instance field, demonstration, so it won't generate a NPE
    private boolean[] array = new boolean[0];
    @Override
    protected void paintComponent(Graphics g){    
        paintComponent(g);
        g.setColor(Color.black);

        for(int i = 0; i < array.length; i++){
            if (array[i]){
                g.drawLine(i, i, i, i);
            }
        }
    }

Obviously, this is just a "fundamental" concept, you will need to define the functionality around updating the array with the values you need, which could be passed through the constructor or a setter depending on your needs.

When you want the paint system to update your component, simply call repaint on the instance you want updated

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM