Results 1 to 10 of 10
  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,807
    Rep Power
    9

    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,807
    Rep Power
    9

    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 offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,001
    Rep Power
    22

    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.

  8. #8
    Rob_B is offline Member
    Join Date
    Mar 2016
    Location
    Melbourne, Australia
    Posts
    1
    Rep Power
    0

    Default Re: Problem with fillRect() function

    I have a similar problem with some software that I have written that plots graphs from values on a database. I am running Windows and the problem occurs on some computers but not others with exactly the same code (whether run from source or compiled and with Sun java either 1.7.0_79 or 1.8.0_60).
    The issue seems peculiar to drawRect and fillRect - there is no problem drawing individual lines or even pie charts (with filled sectors).

    It could be a Windows version issue (fine with Windows 7 Pro, but fails on Windows 10).

    Whatever the cause, I've decided to throw in the towel and will fill in the areas using a loop of drawLine statements.

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

    Default Re: Problem with fillRect() function

    Quote Originally Posted by Rob_B View Post
    It could be a Windows version issue (fine with Windows 7 Pro, but fails on Windows 10).

    Whatever the cause, I've decided to throw in the towel and will fill in the areas using a loop of drawLine statements.
    First, please don't resurrect old threads. This is about 5 years old. Just start your own.

    Second, I doubt it is a windows problem. I recommend you post a Short, Self Contained, Correct Example to demonstrate the problem you are having.

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

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,001
    Rep Power
    22

    Default Re: Problem with fillRect() function

    Quote Originally Posted by jim829 View Post
    First, please don't resurrect old threads. This is about 5 years old. Just start your own.
    Agree. Thread closed.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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, 06:02 PM
  3. split() function problem.
    By robWhittle in forum New To Java
    Replies: 2
    Last Post: 04-09-2010, 01:34 PM
  4. Problem with split function
    By a.tajj in forum New To Java
    Replies: 4
    Last Post: 04-14-2009, 03:30 AM
  5. Function declaration problem.
    By snooze-g in forum Advanced Java
    Replies: 3
    Last Post: 07-18-2007, 09: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
  •