Results 1 to 6 of 6
Thread: Displaying graphics on JFrame
- 07-28-2012, 08:00 PM #1
Hello
- Join Date
- Jul 2012
- Posts
- 6
- Rep Power
- 0
Displaying graphics on JFrame
I just finished my first java book (Java for Dummies by Barry Burd), and I decided to try my own project.
What this program is supposed to do is create an ball when you press the button.
Anyway, I really screwed up and I don't know how to fix it.
Problems:
1. The ball shows up initially without my ever calling the, uh, method I think is what it's called.
2. When I press the button, the compiler gives some funky error message. "NullPointerException"
So here's the program:
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DBall extends JFrame implements ActionListener{
private static final long serialVersionUID=1L;
Graphics g;
public void paint(Graphics g) {
g.drawOval(80,140, 80, 80);
}
JButton createButton = new JButton("Create a ball.");
public DBall (){
setTitle("Disappearing Ball");
pack();
setSize(400,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
add(createButton);
createButton.addActionListener(this);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
paint(g);
}
}
And the JFrame creator:
public class DisplayDTest {
public static void main(String[] args) {
new DBall();
}
}
If anyone knows the solution help is greatly appreciated!
Thanks.
-
Re: Displaying graphics on JFrame
The NullPointerException (NPE) is occurring because you trying to use a variable that is null. You should inspect the line that is throwing the NPE to see what variables are used on that line to see which could be null. My guess it's this bit:
because the Graphics object, g, is likely null.Java Code:public void actionPerformed(ActionEvent e) { paint(g); }
Most importantly, you'll want to read up on how to do Swing Graphics because to do it correctly, you have to throw out old assumptions and study what the tutorials suggest should be done. Here are two good tutorials that should get you started:
The first tutorial is for the very basics and the second is a bit more advanced. Best of luck!
- 07-28-2012, 08:24 PM #3
Member
- Join Date
- Jun 2012
- Posts
- 49
- Rep Power
- 0
Re: Displaying graphics on JFrame
instead of using paint(g) you must use repaint().
-
Re: Displaying graphics on JFrame
And also he should draw in a JPanel's paintComponent method, he should call the super's method first. He may wish to use an if block that checks a boolean variable inside of his paintComponent method to help it decide if it will draw the shape or not, and then have the boolean variable changed by his actionPerformed method before calling repaint,...
- 07-28-2012, 09:54 PM #5
Re: Displaying graphics on JFrame
Why do they call it rush hour when nothing moves? - Robin Williams
- 07-31-2012, 06:12 AM #6
Hello
- Join Date
- Jul 2012
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Question on Graphics/JFrame/KeyEvents
By loopsnhoops in forum New To JavaReplies: 4Last Post: 02-10-2011, 11:22 PM -
Need Help with graphics / Jframe
By loopsnhoops in forum AWT / SwingReplies: 1Last Post: 02-10-2011, 11:15 PM -
problem with jframe&jmenu&graphics
By beni.vd in forum AWT / SwingReplies: 1Last Post: 12-31-2010, 07:07 PM -
Jpanel and displaying graphics
By jdsflash in forum New To JavaReplies: 6Last Post: 11-21-2009, 01:14 AM -
Graphics with JFrame
By kandt in forum New To JavaReplies: 3Last Post: 12-02-2008, 12:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks