Results 1 to 7 of 7
  1. #1
    dreamstring is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default Problem with fillRect() function

    Hi everyone,

    I am new in java, and I am practicing with an example from a java book. I want to create a window with a black rectangle on the center. The following is my code. When I run it, I got only the window, but the rectangle is not shown. I typed the code exactly from the book; I don't know why I cannot get the correct result. Anyone can help me? Thank you!

    import java.awt.*;
    import javax.swing.*;

    public class ColoredRectanglePractice {

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

    }

    class ColoredRectangle{
    private int width;
    private int height;
    private int x;
    private int y;
    private JFrame window;
    private Color color;

    // Default constructor
    public ColoredRectangle()
    {
    width = 40;
    height = 20;
    x = 100;
    y = 100;
    color = Color.BLACK;

    window = new JFrame("New Window");
    window.setSize(500, 500);
    window.setVisible(true);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);

    }
    public void paint()
    {
    Graphics g = window.getGraphics();
    g.setColor(color);
    g.fillRect(x, y, width, height);
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I'd double check your code with the code in the book. I'm 99% sure that they would not include an example program if it didn't work.

  3. #3
    dreamstring is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Thank you for your reply, Junky. I am sure that my code is exactly the same as the code in the book. I am not sure if it could be the issue of the version of JDK or the OS... I am using Win7 and JDK 1.6.0_24

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    If you have the attitude that your code is perfect and could not possibly be wrong then you are not going to do very well in programming.

  5. #5
    dreamstring is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Thank you for your lesson, Junky. I will check the code with the book again.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Graphics g = window.getGraphics();
    Scrap the book. Any book that advises to use getGraphics of a component deserves to be composted. Any method of attempting custom painting is a top level window is also deplorable.

    Learn how to correctly perform custom painting here:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

  7. #7
    dreamstring is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Thank you, Darryl! This tutorial is really helpful! I guess I need to buy a better book :). This book is really misleading.

Similar Threads

  1. Problem regarding function
    By sudarson in forum New To Java
    Replies: 8
    Last Post: 02-26-2011, 11:28 PM
  2. help changing the RGB of a fillRect
    By AnimeKitty in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 07:02 PM
  3. split() function problem.
    By robWhittle in forum New To Java
    Replies: 2
    Last Post: 04-09-2010, 02:34 PM
  4. Problem with split function
    By a.tajj in forum New To Java
    Replies: 4
    Last Post: 04-14-2009, 04:30 AM
  5. Function declaration problem.
    By snooze-g in forum Advanced Java
    Replies: 3
    Last Post: 07-18-2007, 10:15 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
  •