Results 1 to 9 of 9
- 01-23-2011, 04:35 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
NullPointerException in basic GUI
I have no idea what to do here someone please help
This is the error messageThis is my Gui window main classJava Code:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at ColorPanel1.paintComponent(ColorPanel1.java:22) at javax.swing.JComponent.paint(JComponent.java:1029) at javax.swing.JComponent.paintChildren(JComponent.java:862) at javax.swing.JComponent.paint(JComponent.java:1038) at javax.swing.JComponent.paintChildren(JComponent.java:862) at javax.swing.JComponent.paint(JComponent.java:1038) at javax.swing.JLayeredPane.paint(JLayeredPane.java:567) at javax.swing.JComponent.paintChildren(JComponent.java:862) at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131) at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManag er.java:278) at javax.swing.RepaintManager.paint(RepaintManager.java:1224) at javax.swing.JComponent.paint(JComponent.java:1015) at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21) at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java: 60) at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97 ) at java.awt.Container.paint(Container.java:1780) at java.awt.Window.paint(Window.java:3375) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713) at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:6 93) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(System EventQueueUtilities.java:125) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEvent(EventQueue.java:597) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre ad.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread. java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre ad.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) Press any key to continue...
This is the Color panel 1 classJava Code:import javax.swing.*; import java.awt.*; public class readgame { public static void main(String[] args) { JFrame GUI = new JFrame(); GUI.setTitle("Austin's Game"); GUI.setSize(300, 200); GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ColorPanel1 level1 = new ColorPanel1(); Container pane = GUI.getContentPane(); pane.add(level1); GUI.setVisible(true); } }
This is the Circle classJava Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ColorPanel1 extends JPanel { private Circle c1, c2; private Circle selectedCircle; private int x, y; public void ColorPanel1() { setBackground(Color.white); c1 = new Circle (200, 100, 25, Color.red); c2 = new Circle (100, 100, 50, Color.blue); selectedCircle = null; addMouseListener(new PanelListener()); addMouseMotionListener(new PanelMotionListener()); } public void paintComponent(Graphics g){ super.paintComponent(g); c1.draw(g); c2.draw(g); } private class PanelListener extends MouseAdapter{ public void mousePressed(MouseEvent e){ x = e.getX(); y = e.getY(); if (c1.containsPoint(x, y)) selectedCircle = c1; else if (c2.containsPoint(x, y)) selectedCircle = c2; } public void mouseReleased (MouseEvent e){ x = e.getX(); y = e.getY(); selectedCircle = null; } } private class PanelMotionListener extends MouseMotionAdapter{ public void mouseDragged(MouseEvent e){ int newX = e.getX(); int newY = e.getY(); int dx = newX - x; int dy = newY - y; if(selectedCircle != null) selectedCircle.move(dx, dy); x = newX; y = newY; repaint(); } } }
Please Help:confused:Java Code:import java.awt.*; public class Circle { private int centerX, centerY, radius; private Color color; public Circle(int x, int y, int r, Color c){ centerX = x; centerY = y; radius = r; color = c; } public void draw (Graphics g){ Color oldColor = g.getColor(); g.setColor(color); g.drawOval(centerX - radius, centerY - radius, radius * 2, radius * 2); g.setColor(oldColor); } public boolean containsPoint(int x, int y){ int xSquared = (x - centerX) * (x - centerX); int ySquared = (y - centerY) * (y - centerY); int radiusSquared = radius * radius; return xSquared + ySquared - radiusSquared <= 0; } public void move(int xAmount, int yAmount){ centerX = centerX + xAmount; centerY = centerY + yAmount; } }
-
This method is never called:
Java Code:public void ColorPanel1() { setBackground(Color.white); c1 = new Circle(200, 100, 25, Color.red); c2 = new Circle(100, 100, 50, Color.blue); selectedCircle = null; addMouseListener(new PanelListener()); addMouseMotionListener(new PanelMotionListener()); }
and so c1 and c2 are never initialized. Why is it never called? Because it has a void return type and constructors should have no return type:
Luck and HTH!Java Code:public ColorPanel1() { //..... }
-
Oh, and welcome to the forum, and special kudos for using code tags in your first post in this forum. Most unusual and refreshing to see.
- 01-23-2011, 04:45 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
ah thanks thats it.
im only halfway into a year of comm science so ya thanks
also the c1.draw works but c1.fill does not
im pretty sure its because my text book is old but i have no idea what the correct code is to make the circle appear solid ...if anyones know, or if fubarable knows that would be great
-
You're welcome.
You call fill very similar to how you call draw. Please show us how you attempt to call it, and we may be able to better help you.also the c1.draw works but c1.fill does not
im pretty sure its because my text book is old but i have no idea what the correct code is to make the circle appear solid ...if anyones know, or if fubarable knows that would be great
- 01-23-2011, 04:50 AM #6
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
i triedJava Code:public void paintComponent(Graphics g){ super.paintComponent(g); c1.draw(g); c2.draw(g); }
c1.fill(g);
but it didnt work
-
fill is called like draw, but it is called on a Graphic object, i.e, Graphic#fillOval(....).
Wouldn't you would first need to give the Circle class a fill method for it to work, using a call similar to above, if you are going to try to call it off of a Circle object?
- 01-23-2011, 05:02 AM #8
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
Ah ya thats pretty much it but i already had the method it was
but on the last line it should be g.fillOval(...)Java Code:public void draw (Graphics g){ Color oldColor = g.getColor(); g.setColor(color); g.drawOval(centerX - radius, centerY - radius, radius * 2, radius * 2); g.setColor(oldColor); }
Thanks for answering my stupid questions
I appreciate all the help:D
-
You're welcome. Best of luck with your course work.
Similar Threads
-
Basic GUI help
By vahshir in forum New To JavaReplies: 5Last Post: 10-11-2010, 01:33 AM -
Need really basic help!
By anthonyalaan in forum New To JavaReplies: 9Last Post: 09-12-2010, 01:43 PM -
Basic Help
By robjames in forum New To JavaReplies: 3Last Post: 02-09-2009, 02:58 AM -
Need some really basic help
By Mayur in forum New To JavaReplies: 6Last Post: 01-24-2009, 06:00 AM -
help with basic example
By fred in forum New To JavaReplies: 1Last Post: 07-20-2007, 05:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks