Results 1 to 7 of 7
  1. #1
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default 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:

    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();
    	}
    }
    My to problems are:
    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 10:16 PM.

  2. #2
    TheTF is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    I think you initialize it by writing "Graphics c = new Graphics();". Did it work?

  3. #3
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default

    I tried that already - it comes up with the error: Cannot instantiate the type
    Graphics
    Is there something else I need to import?

  4. #4
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    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.

  5. #5
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default

    Thanks very much for the link!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by TheTF View Post
    I think you initialize it by writing "Graphics c = new Graphics();". Did it work?
    Original poster, just to be sure, you know to completely ignore this "advice" by The TF, right?

  7. #7
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

Similar Threads

  1. Question on Graphics/JFrame/KeyEvents
    By loopsnhoops in forum New To Java
    Replies: 4
    Last Post: 02-11-2011, 12:22 AM
  2. Question on Graphics etc...
    By loopsnhoops in forum Advanced Java
    Replies: 2
    Last Post: 02-11-2011, 12:10 AM
  3. Question on Java Graphics, KeyBoard Events and JFrame
    By loopsnhoops in forum New To Java
    Replies: 1
    Last Post: 02-10-2011, 04:12 AM
  4. Drawing a graphics onto another Graphics ?
    By Ziden in forum Java Applets
    Replies: 0
    Last Post: 01-08-2011, 08:30 PM
  5. java graphics question
    By bobCallahan24 in forum AWT / Swing
    Replies: 1
    Last Post: 01-15-2010, 07:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •