
Hi<BR> I am newbie to java and I have written a code in java that creates a window in which a line and rectangle appears. Here is the code.
import java.awt.*;<BR> public class FirstCanvase extends Canvas<BR> {<BR> public FirstCanvase()<BR> {<BR> setSize(500,500);<BR> setBackground(Color.white);<BR> }<BR> public static void main(String[] args) <BR> {<BR>
FirstCanvase FC=new FirstCanvase();<BR>
Frame f1=new Frame();<BR> f1.add(FC);<BR> f1.setSize(250,250);<BR> f1.setVisible(true);<BR> }<BR> public void paint(Graphics g)<BR> {<BR> g.setColor(Color.blue);<BR> g.drawLine(30,30,80,80);<BR> g.drawRect(50,50,100,100);<BR> }<BR> }<BR>
EVERY THING IS WORKING FINE. I GOT A WINDOW DISPLAYED WITH A LINE & RECT. BUT THE PROBLEM IS I AM UNABLE TO CLOSE THE WINDOW.
I AM USING ECLIPSE TO RUN THE PROGRAM.
WHAT CAN I DO TO SOLVE IT?

The given code draws a line and a rectangle on a swing frame using Graphics class.
import javax.swing.*;
import java.awt.*;
public class DrawLineAndRectangle{
public static void main(String[] args) {
DrawLineAndRectangle d = new DrawLineAndRectangle();
}
public DrawLineAndRectangle(){
JFrame frame = new JFrame("Drawing with Alpha");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyComponent());
frame.setSize(400,400);
frame.setVisible(true);
}
public class MyComponent extends JComponent{
public void paint(Graphics g){
int height = 200;
int width = 120;
g.setColor(Color.red);
g.drawRect(10,10,180,100);
g.setColor(Color.red);
g.drawLine(250,50, 200,100);
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.