有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何传递图形g

当我尝试这段代码时,我得到了一个NullPointerExection

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调用为空对象,但是我看的每一个教程都是空的,所以我不知道为什么我会遇到这个问题,任何帮助都将不胜感激。谢谢


共 (2) 个答案

  1. # 1 楼答案

    您永远不会创建图形对象。这是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();
        }
    }
    
  2. # 2 楼答案

    How to pass in graphics g

    你没有

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

    所以你需要用你的定制绘画覆盖paintComponent(...)JPanel方法。然后将面板添加到框架中

    阅读Swing教程中关于Custom Painting的部分,了解更多信息和工作示例