Results 1 to 6 of 6
  1. #1
    Andrew_3ds is offline Member
    Join Date
    Sep 2013
    Posts
    13
    Rep Power
    0

    Default drawRect not working

    this is the code can someone please tell me why it doesn't draw it
    ---------------------------------------------------------------


    package com.chopstickgaming.window;

    import java.awt.Dimension;
    import java.awt.Graphics;

    import javax.swing.JFrame;

    public class window extends JFrame {
    public static final int WIDTH = 650;
    public static final int HEIGHT = WIDTH * 3 / 4;
    public static final String TITLE = "Game";
    public static final Dimension SIZE = new Dimension(WIDTH, HEIGHT);

    public static void main(String[] args) {
    JFrame j = new JFrame(TITLE);

    j.setSize(WIDTH, HEIGHT);
    j.setVisible(true);
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void paint(Graphics g) {
    super.paint(g);

    g.drawRect(50, 50, 150, 25);
    }
    }

  2. #2
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: drawRect not working

    You'll have to do 3 things:

    1. Instead of creating a JFrame, create a "window", by the way, classes start with an upper-case letter.
    2. You'll also have to create a constructor for your window class.
    3. Before calling drawRect, set the color to the color that you want to draw by calling setColor()

    If I can help you further, you can contact me, no charge.

    -- Kaydell
    Last edited by kaydell2; 09-30-2013 at 06:58 AM.

  3. #3
    Andrew_3ds is offline Member
    Join Date
    Sep 2013
    Posts
    13
    Rep Power
    0

    Default Re: drawRect not working

    Quote Originally Posted by kaydell2 View Post
    You'll have to do 3 things:

    1. Instead of creating a JFrame, create a "window", by the way, classes start with an upper-case letter.
    2. You'll also have to create a constructor for your window class.
    3. Before calling drawRect, set the color to the color that you want to draw by calling setColor()

    -- Kaydell
    Java Coaching: Home
    How do you make a "window"?
    I only know JFrame, I started java only 2 days ago

  4. #4
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: drawRect not working

    How do you make a "window"? I only know JFrame, I started java only 2 days ago
    I meant to do

    GameWindow gameWindow = new GameWindow();

    instead of

    JFrame j = new JFrame();

    Java Code:
    import java.awt.Dimension;
    import java.awt.Graphics;
    
    import javax.swing.JFrame;
    
    @SuppressWarnings("serial")
    public class GameWindow extends JFrame {
    	
    	public static final int WIDTH = 650;
    	public static final int HEIGHT = WIDTH * 3 / 4;
    	public static final String TITLE = "Game";
    	public static final Dimension SIZE = new Dimension(WIDTH, HEIGHT);
    
    	public static void main(String[] args) {
    		GameWindow gameWindow = new GameWindow();
    		gameWindow.setTitle(TITLE);
    		gameWindow.setSize(WIDTH, HEIGHT);
    		gameWindow.setVisible(true);
    		gameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    
    	@Override
    	public void paint(Graphics g) {
    		super.paint(g);
    		g.drawRect(50, 50, 150, 25);
    	}
    
    }
    Try setColor(Color.RED) etc before calling drawRect() you could try fillRect() and fillOval(), drawOval()

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: drawRect not working

    If you are just starting Java, why are you trying to do graphics? I suggest you check out the tutorials (see my signature) and buy a good book. Otherwise your experience in learning the language won't be as enjoyable as it could be.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: drawRect not working

    @kaydell2 - just so you know, this forum frowns on posting personal websites. It's considered spamming. You may want to edit your post and remove it before the moderators do.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Where to start. Loop drawRect.
    By vuekong in forum New To Java
    Replies: 7
    Last Post: 07-28-2013, 01:57 AM
  2. Basic Q: passing to drawRect() in an applet
    By hayden06f4i in forum New To Java
    Replies: 20
    Last Post: 12-08-2010, 04:30 AM
  3. Random drawRect
    By Wallsurfer in forum New To Java
    Replies: 5
    Last Post: 03-10-2009, 10:20 AM
  4. Replies: 4
    Last Post: 09-05-2008, 05:47 PM
  5. Replies: 8
    Last Post: 05-28-2008, 08:00 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
  •