简体   繁体   中英

How do you design a new graphic to add to a JFrame Form?

I am trying to design a very basic aircraft attitude indicator to put into a JFrame Form using Netbeans 7.3, essentially consisting of a black box with a white line in the middle that rotates proportionally with the degree of roll of the aircraft. This then needs to be put into a JFrame form to create a virtual cockpit user interface. I am a novice programmer with very limited knowledge of the Java library, so any help would be greatly appreciated!

So far, I have created the following class:

public class AttitudeIndicator extends JPanel {
private Graphics attIndBackground;
private Graphics attIndLine;
private int aILStartX;
private int aILStartY;
private int aILEndX;
private int aILEndY;

public AttitudeIndicator(int a,int b,int c,int d){
    aILStartX = a;
    aILStartY = b;
    aILEndX = c;
    aILEndY = d;
}

public void createAttitudeIndicator(){
    attIndBackground.setColor(Color.BLACK);
    attIndBackground.fillRect(0, 0, 200, 150);
    attIndBackground.setColor(Color.RED);
    attIndBackground.drawLine(0,75,200,75);

    attIndLine.setColor(Color.WHITE);
    attIndLine.drawLine(aILStartX,aILStartY,aILEndX,aILEndY);
}

}

The idea is that arguments a,b,c and d will change and therefore change the line with the degree of pitch and roll, but I am yet to get to that point. I have then put this into the main class to try and create it:

public static void main(String[] args) {

    JFrame f = new JFrame("Title");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    AttitudeIndicator a = new AttitudeIndicator(50,50,150,75);
    a.createAttitudeIndicator();
    f.add(a);
    f.setSize(500,400);
    f.setVisible(true);
}

}

When I try to run this, I am getting a null pointer exception. Any ideas?

i think this might help you. NetBeans has a great GUI to help you make GUI's for your programs https://netbeans.org/kb/70/java/quickstart-gui.html

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