简体   繁体   中英

Creating a custom method to draw - Graphics 2D g2 is null

I am trying to solve the following problem: Write a program that displays the Olympic rings. Color the rings in the Olympic colors. Provide a method drawRing that draws a ring of a given position and color.


I am having a major issue with my understanding of how exactly objects of type Graphics work. I initially tried using getGraphics() to pass it as a parameter to the method however i learned that this method is typically avoided as null exceptions are likely to occur. So i read that you should override the paintComponent method in the component class? so i tried this by making a class containing the override and my drawRing method, making Graphics2D g2 an instance variable however i am still getting a null error once it is called upon in the first line of drawRing . I have been trying to figure something out for hours but I'm really not sure how to approach this problem to make the method have access to the frame in main and if there's an entirely different way i should be tackling it. Any help is much appreciated.


Also, I would just like to note that I have not begun accounting for x and y coordinates as well as color in my drawRing method, I first want to make sure the program can access the method and that it can draw on the frame made in main first, afterwards I figure those details will be fairly simple.


My Code:

import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.awt.geom.*;
public class test{
    public static class ringComponent extends JComponent{
        private Graphics2D g2;
        public void paintComponent(Graphics g){
            g2 = (Graphics2D) g;
        }
        public void drawRing(){
            g2.setColor(Color.RED);//Null error here
            Ellipse2D.Double circle = new Ellipse2D.Double(50, 50, 200, 200);
            g2.draw(circle);
        }
    }
    public static void main(String[] args){
        JFrame frame = new JFrame();
        final int FRAME_WIDTH = 800;
        final int FRAME_HEIGHT = 800;
        frame.setSize(FRAME_WIDTH,FRAME_HEIGHT);
        frame.setTitle("Olympic Rings");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ringComponent rc = new ringComponent();
        rc.drawRing();
        frame.add(rc);
        frame.setVisible(true);
    }
}

You should NOT be defining a "g2" variable.

You should NOT invoke the drawRing() method.

Instead the code from the drawRing() method should be placed in the paintComponent() method. Swing will then invoke the paintComponent() method automatically when the component needs to be repainted.

Read the section from the Swing tutorial on Custom Painting for more information and working examples.

Of course g2 is null. You haven't called paintComponent , so it hasn't been initialized yet.

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