Results 1 to 2 of 2
  1. #1
    choppsuey is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default Help with drawing a rectangle

    Hello all,

    I am brand new to Java and am having some trouble finishing an assignment in which I am to get user input and draw a rectangle using it. Below is the code I am using:

    ***Rectangle Class***

    import java.io.*;
    import java.awt.Graphics;

    public class Rectangle extends main {

    public static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
    public static int width;
    public static int length;
    public static Graphics rect;

    public Rectangle() {
    length = 0;
    width = 0;
    }

    public static int getWidth() {

    try {
    System.out.println("Enter the width of the rectangle: ");
    width = Integer.parseInt(keyboard.readLine());
    }
    catch(Exception e) {System.out.println("Error : "+e);
    }
    return width;
    }

    public static int getLength() {

    try {
    System.out.println("Enter the length of the rectangle: ");
    length = Integer.parseInt(keyboard.readLine());
    }
    catch(Exception e) {System.out.println("Error : "+e);
    }
    return length;
    }

    public static void paint(Graphics rect)
    {
    rect.drawRect(0,0, width,length);
    }
    }

    ***Main***

    [B]import java.awt.*;
    import java.awt.font.GraphicAttribute;

    public class main {

    public static void main(String[] args) {
    Rectangle rectangle = new Rectangle();
    rectangle.getWidth();
    rectangle.getLength();
    rectangle.paint();
    }
    }

    Everything seems to be working but I cannot for the life of me figure out what I am doing wrong in regards to painting the actual rectangle. The user input portion works fine but the paint method ends up in a Null Exception. If anyone could please point me in the right direction or at least some good resources it would be very much appreciated. Thanks.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Help with drawing a rectangle

    If you are attempting to draw using Swing, I highly recommend reading the following tutorial, as you need a user interface...(and at first glance, your code should not be working - should be throwing a NullPointerException)
    Trail: 2D Graphics (The Java™ Tutorials)
    And further, I would recommend not making everything static, and there is no reason Rectangle should extend main

Similar Threads

  1. Rectangle class again
    By toppcon in forum New To Java
    Replies: 2
    Last Post: 05-05-2011, 03:25 AM
  2. Replies: 6
    Last Post: 11-30-2010, 11:29 PM
  3. Java Rectangle
    By java_beginner_ in forum New To Java
    Replies: 9
    Last Post: 10-31-2010, 07:15 PM
  4. Wrong with Rectangle res = new Rectangle(0,0,0,0);???
    By jiapei100 in forum AWT / Swing
    Replies: 3
    Last Post: 09-25-2010, 03:39 PM
  5. Replies: 2
    Last Post: 04-10-2010, 06:35 PM

Posting Permissions

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