473,473 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Simple Java Program to Draw a Rectangle on a Pane

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_CLOS E);
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======
Jul 17 '05 #1
1 36631
Allan,

You need to add the panel to the content pane of your Frame:

frame.getContentPane().add(this,java.awt.BorderLay out.CENTER);

Also, you have a typo with paintComponent (not paintComponet).

Good Luck,

Rob
Allan Horwitz wrote:
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_CLOS E);
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======


Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: enclume42 | last post by:
Hello, I am a pure Linux guy. I wrote a Java program that is going to be used by some Windows people, and I wish the startup of the program to be as simple as possible, namely to double-click on...
3
by: gohaku | last post by:
Hi everyone, I would like to know if it's possible to use python (libraries) in a Java Program. I do not want to use: Process proc = Runtime.getRuntime.exec(command) I am basically looking for...
5
by: zaidalin79 | last post by:
Please-if anyone is online that can help me with a couple beginner Java programs... I have spent all day looking at them and I just can't figure out what I need to do... Please help!!
2
Nert
by: Nert | last post by:
Hi.., i have build a simple program which is done few days ago.., it's just a simple chatroom. Now my problem is how can i make my java program executable? i want to run it on the other computer...
2
by: niharkd | last post by:
Hi, I am trying to run the program below. IT compiles fine and also runs. However, once the jframe loads and the container loads, I dont see any rectangle being drawn in there. Can anyone please...
8
by: Nirav Amin | last post by:
Create a 2-D array containing the data : Pakistan Sweden France Iran China Japan Russia China Italy China China ...
16
by: feda | last post by:
Hi everybody, I am new to java I installed netbeans 6.0.1 I tried running the following simple program but it didnor work??? import java.util.*; public class Hello { public...
1
by: kummu4help | last post by:
hi, i want to draw rectangle based on mousedrag event. if user dragging the mouse, then the rectangle on the applet should increase or decrease basing on current mouse coordinates. i have the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.