Simple Java Program to Draw a Rectangle on a Pane

From: Allan Horwitz (ahorwitz_at_colburn.com)
Date: 10/05/03


Date: 5 Oct 2003 10:12:25 -0700

I am trying to draw a rectangle onto a pane. My program will compile
and run but the rectangle does not seem to get drawn. When the program
is run the program frame opens up, but I cannot see a rectangle on the
pane.

Any help would be appreciated.

Sincerely,
Allan

====start class code=====
import java.awt.*;
import javax.swing.*;

class MyDrawPanel extends JPanel {
        
        public static void main(String[] args) {
                
                MyDrawPanel gui = new MyDrawPanel();
                gui.go();
                
                
        }//close main
        
        
        public void go() {
                
                JFrame frame = new JFrame();
        
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(300,300);
                frame.setVisible(true);
                
                
        }//close go
        
        
        public void paintComponet(Graphics g) {
                                
                g.setColor(Color.orange);
                g.fillRect(20,50,100,100);
                
        }//close paintComponent
        
}//close class

======end class code======



Relevant Pages

  • Re: Printing the whole whole printable area
    ... your rectangle are being drawn just outside of the printable area. ... need to inset the right and bottom by a pixel to get the line into the ... But at the very least, you need to understand that if you try to draw a rectangle that is the same exact width and height as what you believe to be the valid area for drawing, the right and bottom edges will not be drawn. ... For the record, the following code, when I use it with the Office Document Image Writer print driver, it prints a frame exactly the way you want: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Draw a path with a Pen which has a width of 2?
    ... For a circle or rectangle, your drawn area will be larger than the geometry ... > The border can also have different shapes such as Round, Circle, ... When I use a width of 2, the path is not drawn correctly. ...
    (microsoft.public.dotnet.framework.drawing)
  • vertical text not drawn at correct location
    ... the center of the display. ... Text is suppose to be drawn at a 270 degree ... angle and centered in the middle of the rectangle. ... CRect oCR; ...
    (microsoft.public.vc.mfc)
  • Re: vertical text not drawn at correct location
    ... Your suggestions works in the sample code but not my ... Ian ... Text is suppose to be drawn at a 270 degree ... >> angle and centered in the middle of the rectangle. ...
    (microsoft.public.vc.mfc)
  • Re: How to Drawstring with a character drawn "partially"?
    ... Regarding drawing text character partially, I'm not too sure how to use the ... > You could draw the text without setting a layout rectangle, ... >> But I find that if a character cannot fit into the rectangle, ... >> character will not be drawn unless the size of the rectange increase, ...
    (microsoft.public.dotnet.framework.drawing)