Results 1 to 8 of 8
Thread: Graphics problem
- 02-19-2011, 06:09 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
Graphics problem
Hi trying to draw a circle on my frame.
Getting the error
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at SuperStarTrekGame.paint(SuperStarTrekGame.java:44)
The error is onJava Code:import java.awt.Graphics; import javax.swing.JFrame; public class SuperStarTrekGame extends JFrame{ private int numberOfKlingons, numberOfRomulans; private boolean setSuperCommander; private int timeRemaining; private int numberOfStarBases, numberOfBlackHoles; private String difficulty; private Quadrant[] galaxy = new Quadrant[7]; private int arrayPos; public void prepareGame(){ arrayPos = 5; setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(800,600); setVisible(true); } public void adjustTime(){ } public void paint(Graphics g){ galaxy[arrayPos].paint(g); } public void checkGame(){ } public static void main(String[] args){ SuperStarTrekGame game = new SuperStarTrekGame(); game.prepareGame(); } } import java.awt.Color; import java.awt.Graphics; public class Quadrant{ public Quadrant(){ } public void paint(Graphics g){ g.setColor(Color.green); g.drawOval(40, 40, 100, 100); g.fillOval(40, 40, 100, 100); } }
galaxy[arrayPos].paint(g);
Any ideas?
- 02-19-2011, 06:16 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
I see a declared and initialized array of Quadrants, but nowhere do I see the Quadrants being initialized, so the array galaxy contains null values. That's probably the error, but it's possible you're not showing us the whole code. Also, instance variables should be initialized in the constructor, not at declaration.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 02-19-2011, 07:01 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
Ah ofcourse, I need a for loop to initialise each Quadrant in the array.
thx !
- 02-19-2011, 07:04 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 02-19-2011, 07:19 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
Just a quick question, I am drawing my image fine, but as far as I know, I should be using paintComponent rather then paint since use Jframe. But when I use paintComponent, the circle is not drawn, it works fine with paint tho?
- 02-19-2011, 07:22 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 02-19-2011, 07:25 PM #7
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
So should I instead extend Jpanel and create a JFrame in the constructor, since paintComponent will need to override?
- 02-19-2011, 08:19 PM #8
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Similar Threads
-
Drawing a graphics onto another Graphics ?
By Ziden in forum Java AppletsReplies: 0Last Post: 01-08-2011, 07:30 PM -
problem with jframe&jmenu&graphics
By beni.vd in forum AWT / SwingReplies: 1Last Post: 12-31-2010, 07:07 PM -
i am having a problem with creating graphics
By umpire43055 in forum AWT / SwingReplies: 3Last Post: 08-07-2009, 05:26 AM -
Problem in g.drawArc() api of Graphics class
By narasimman in forum AWT / SwingReplies: 0Last Post: 07-30-2008, 08:19 AM -
Problem in printing java graphics
By Mahendra in forum Java 2DReplies: 0Last Post: 01-23-2008, 12:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks