Results 1 to 7 of 7
Thread: Graphics question
- 07-18-2011, 07:28 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
Graphics question
Hello! I am trying to make a simple program in which a circle can be moved around the screen using the wasd keys. I have managed to do it in a different program using a JLabel, but now trying to add 2d graphics to my program is confusing me.
Here is my code so far:
My to problems are:Java Code:import java.awt.*; import javax.swing.*; import java.awt.geom.*; import java.awt.event.*; public class Game extends JFrame{ int x = 715; // x position of circle int y = 400; // y position of circle char input; // user key input Graphics c; //what do I initialize this to? JFrame f = new JFrame(); public Game() { f.setTitle("Game"); f.setSize(1430, 800); f.setResizable(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); draw(c); f.setVisible(true); } public void draw(Graphics c) { Graphics2D c2D = (Graphics2D)c; c2D.setColor(Color.black); BasicStroke bPen = new BasicStroke(); c2D.setStroke(bPen); Ellipse2D.Float ball = new Ellipse2D.Float(600, 350, 50, 50); c2D.fill(ball); f.add(c2D); // error: The method add(Component) in the type Container is not applicable for the arguments (Graphics2D) } public static void main(String[] args) { Game w1 = new Game(); } }
1. I don't know what to initialize 'Graphics c' to.
EDIT: I tried to initialize by writing Graphics c = new Graphics(); this returned the error Cannot instantiate the type
Graphics.
The code works fine without that line of code but then if I try call the draw method:
draw(c); it can't find the variable c, however the method itself works fine.
public void draw(Graphics c) { //What is the Graphics c in the method parameters?
Graphics2D c2D = (Graphics2D)c;
//etc
}
2. When I add it to my frame 'f.add(c2D)' it says The method add(Component) in the type Container is not applicable for the arguments (Graphics2D)Last edited by Guy; 07-18-2011 at 09:16 PM.
- 07-18-2011, 07:37 PM #2
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
I think you initialize it by writing "Graphics c = new Graphics();". Did it work?
- 07-18-2011, 07:39 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
I tried that already - it comes up with the error: Cannot instantiate the type
Graphics
Is there something else I need to import?
- 07-18-2011, 09:45 PM #4
You're doing a lot wrong. You don't have to initialize graphics or add it to a frame: just see it as a gift from "super".
I can try to explain, but just following a tutorial is much better:
Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)No bug ever had to calculate its fitnessfunction.
- 07-18-2011, 10:01 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 42
- Rep Power
- 0
Thanks very much for the link!
-
- 07-19-2011, 07:23 AM #7
Member
- Join Date
- Jul 2011
- Posts
- 42
- 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 -
Question on Graphics etc...
By loopsnhoops in forum Advanced JavaReplies: 2Last Post: 02-10-2011, 11:10 PM -
Question on Java Graphics, KeyBoard Events and JFrame
By loopsnhoops in forum New To JavaReplies: 1Last Post: 02-10-2011, 03:12 AM -
Drawing a graphics onto another Graphics ?
By Ziden in forum Java AppletsReplies: 0Last Post: 01-08-2011, 07:30 PM -
java graphics question
By bobCallahan24 in forum AWT / SwingReplies: 1Last Post: 01-15-2010, 06:04 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks