Results 1 to 7 of 7
Thread: Problem with fillRect() function
- 03-21-2011, 11:37 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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);
}
}
- 03-22-2011, 12:11 AM #2
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.
- 03-22-2011, 12:17 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
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
- 03-22-2011, 12:33 AM #4
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.
- 03-22-2011, 12:35 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Thank you for your lesson, Junky. I will check the code with the book again.
- 03-22-2011, 12:56 AM #6
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.Graphics g = window.getGraphics();
Learn how to correctly perform custom painting here:
Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
db
- 03-22-2011, 02:59 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Problem regarding function
By sudarson in forum New To JavaReplies: 8Last Post: 02-26-2011, 10:28 PM -
help changing the RGB of a fillRect
By AnimeKitty in forum New To JavaReplies: 3Last Post: 07-27-2010, 06:02 PM -
split() function problem.
By robWhittle in forum New To JavaReplies: 2Last Post: 04-09-2010, 01:34 PM -
Problem with split function
By a.tajj in forum New To JavaReplies: 4Last Post: 04-14-2009, 03:30 AM -
Function declaration problem.
By snooze-g in forum Advanced JavaReplies: 3Last Post: 07-18-2007, 09:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks