如何传递图形g

时间:2018-11-26 21:03:19

标签: java nullpointerexception

尝试此代码时,我得到一个nullpointerexeption

package computerscience;
import java.awt.Graphics;
import java.util.Scanner;

import javax.swing.JFrame;
public class Recursion extends JFrame{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    public Recursion() {
        setTitle("recursion");
        setSize(600, 300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void repeatSquare(Graphics g, double x, double y, double size, int count, int times) {

        if (count < times)
        {
            count++;
            double doublex = x - size/2;
            double doubley = y - size/2;
            double scale = 2.2;
            int intsize = (int) Math.round(size);
            int intx = (int) Math.round(doublex);
            int inty = (int) Math.round(doubley);
            try {
                g.drawRect(intx, inty, intsize, intsize);
//              DrawSquare(null, x - size/2, y - size/2, size/scale, count, times);
//              DrawSquare(null, x - size/2, y + size/2, size/scale, count, times);
//              DrawSquare(null, x + size/2, y - size/2, size/scale, count, times);
//              DrawSquare(null, x + size/2, y + size/2, size/scale, count, times);
            }catch(Exception e){
                System.out.println(e);
            }
        }
        else
            System.out.println("Program is finished! ");        
    }


    public static void main(String[] agrs)
    {
        Recursion r = new Recursion();
        Scanner myScanner = new Scanner(System.in);
        System.out.println("How many times would you like to repeat the pattern: ");
        int times = Integer.parseInt(myScanner.next());
        r.repeatSquare( null, 600/2, 300/2, 100.0f, 0, times);
    }
}

问题出在上面说的那一行:

g.drawRect(intx, inty, insize, intsize);

我知道您不能将drawrect调用为null对象,但是我观看的每个教程都传递null,所以我不知道为什么我遇到了这个问题,我们将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:2)

  

如何传递图形g

你不知道。

Swing确定需要绘制组件时,Swing将Graphics对象传递给组件的绘制方法。

因此,您需要使用自定义绘画重写paintComponent(...)的{​​{1}}方法。然后将面板添加到框架。

阅读Custom Painting的Swing教程中的部分,以获取更多信息和工作示例。

答案 1 :(得分:1)

您将永远不会创建Graphics对象。这是AWT的问题,而不是您的问题。要进行自定义工程图,您需要覆盖paint(例如,对于JFrame)或paintComponent(例如,对于JPanel)方法,然后在图形对象上执行所需的操作。您的代码应如下所示:

package computerscience;

import java.awt.Graphics;
import java.util.Scanner;
import javax.swing.JFrame;

public class Recursion extends JFrame {

    private static final long serialVersionUID = 1L;
    private int times;

    public Recursion() {
        setTitle( "recursion" );
        setSize( 600, 300 );
        setVisible( true );
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }

    public void repeatSquare( Graphics g, double x, double y, double size, int count, int times ) {

        if ( count < times ) {
            count++;
            double doublex = x - size / 2;
            double doubley = y - size / 2;
            double scale = 2.2;
            int intsize = (int) Math.round( size );
            int intx = (int) Math.round( doublex );
            int inty = (int) Math.round( doubley );
            try {
                g.drawRect( intx, inty, intsize, intsize );
//              DrawSquare(null, x - size/2, y - size/2, size/scale, count, times);
//              DrawSquare(null, x - size/2, y + size/2, size/scale, count, times);
//              DrawSquare(null, x + size/2, y - size/2, size/scale, count, times);
//              DrawSquare(null, x + size/2, y + size/2, size/scale, count, times);
            } catch ( Exception e ) {
                System.out.println( e );
            }
        } else {
            System.out.println( "Program is finished! " );
        }
    }

    @Override
    public void paint( Graphics g ) {
        super.paint( g );
        repeatSquare( g, 600 / 2, 300 / 2, 100.0f, 0, times );
    }

    public void setTimes( int times ) {
        this.times = times;
    }

    public static void main( String[] agrs ) {
        Recursion r = new Recursion();
        Scanner myScanner = new Scanner( System.in );
        System.out.println( "How many times would you like to repeat the pattern: " );
        int times = Integer.parseInt( myScanner.next() );
        r.setTimes( times );
        r.repaint();
    }
}