import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.lang.Math;
import java.applet.Applet;
import javax.swing.event.*;
import javax.swing.event.*;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Maint extends JApplet implements
Runnable,ActionListener,MouseListener,MouseMotionLi
stener
{
/* Declaring the variables.*/
JPanel panel;
JPanel textAreaPanel;
Point dot[] = new Point[1000];
Point start, end;
int dots = 0;
JTextArea drawTextArea;
JScrollPane drawScrollPane;
JLabel line, color, paint,kid,cil;
JButton circle, rect, poly, ellipse;
JComboBox cbLine, cbFill;
GridBagLayout g;
GridBagConstraints gbc;
boolean mouseUp = false;
boolean Circle = false;
boolean Rectangle = false;
boolean Polygon = false;
boolean Ellipse = false;
Thread datimeThread;
Date date;
GregorianCalendar calendar;
String strDate, strTime, strStatus;
public void init()
{
g=new GridBagLayout();
gbc=new GridBagConstraints();
panel= (JPanel)getContentPane();
panel.setLayout(g);
paint = new JLabel("My Paint Application");
gbc.anchor= GridBagConstraints.NORTH;
gbc.gridx =2;
gbc.gridy = 0;
g.setConstraints(paint,gbc);
panel.add(paint);
rect = new JButton("Rectangle");
gbc.ipadx = 2;
gbc.ipady = 2;
gbc.gridy = 1;
gbc.gridx = 1;
g.setConstraints(rect,gbc);
panel.add(rect);
circle = new JButton("Circle");
gbc.ipadx = 2;
gbc.ipady = 2;
gbc.gridy = 1;
gbc.gridx = 2;
g.setConstraints(circle,gbc);
panel.add(circle);
poly = new JButton("Polygon");
gbc.ipadx = 2;
gbc.ipady = 2;
gbc.gridy = 1;
gbc.gridx = 3;
g.setConstraints(poly,gbc);
panel.add(poly);
ellipse = new JButton("Ellipse");
gbc.ipadx = 2;
gbc.ipady = 2;
gbc.gridy = 1;
gbc.gridx = 4;
g.setConstraints(ellipse,gbc);
panel.add(ellipse);
line = new JLabel("select line color");
gbc.ipadx = 4;
gbc.ipady = 4;
gbc.gridy = 2;
gbc.gridx = 1;
g.setConstraints(line,gbc);
panel.add(line);
kid = new JLabel("");
gbc.ipadx = 4;
gbc.ipady = 4;
gbc.gridy = 2;
gbc.gridx = 2;
g.setConstraints(kid,gbc);
panel.add(kid);
color = new JLabel("select Fill color");
gbc.ipadx = 4;
gbc.ipady = 4;
gbc.gridy = 2;
gbc.gridx = 3;
g.setConstraints(color,gbc);
panel.add(color);
String Line[]={"choose
thecolor","Red","Yellow","Green","Blue","Cyan","Man
geta","Orange"};
cbLine = new JComboBox(Line);
gbc.fill=GridBagConstraints.BOTH;
gbc.insets=new Insets(10,0,0,30);
gbc.ipadx = 4;
gbc.ipady = 4;
gbc.gridy = 3;
gbc.gridx = 1;
g.setConstraints(cbLine,gbc);
panel.add(cbLine);
cil = new JLabel("");
gbc.ipadx = 4;
gbc.ipady = 4;
gbc.gridy = 3;
gbc.gridx = 2;
g.setConstraints(cil,gbc);
panel.add(cil);
String Fill[]={"choose the
color","Red","Yellow","Green","Blue","Cyan","Manget
a","Orange"};
cbFill = new JComboBox(Fill);
gbc.fill=GridBagConstraints.BOTH;
gbc.insets=new Insets(10,0,0,30);
gbc.ipadx = 4;
gbc.ipady = 4;
gbc.gridy = 3;
gbc.gridx = 3;
g.setConstraints(cbFill,gbc);
panel.add(cbFill);
Color c =new Color(250,100,100);
Container contentpane= getContentPane();
textAreaPanel = new JPanel(new
GridLayout(2,1,5,5));
drawTextArea = new JTextArea();
drawScrollPane = new JScrollPane(drawTextArea);
textAreaPanel.add(drawScrollPane);
contentpane.setLayout(new BorderLayout());
contentpane.add(textAreaPanel,BorderLayout.CENTER);
circle.addActionListener(this);
rect.addActionListener(this);
poly.addActionListener(this);
ellipse.addActionListener(this);
addMouseListener(this);
addMouseMotionListener(this);
dateTime();
}
public void dateTime()
{
datimeThread=new Thread(this);
datimeThread.start();
}
public void run()
{
while(datimeThread !=null)
{
display();
try
{
datimeThread.sleep(1000);
}
catch(InterruptedException e)
{
showStatus("Thread interrupted");
}
}
}
public void display()
{
date =new Date();
calendar =new GregorianCalendar();
calendar.setTime(date);
strTime =
calendar.get(Calendar.HOUR)+":"+calendar.get(Calend
ar.MINUTE)+":"+calendar.get(Calendar.SECOND);
strDate=
(calendar.get(Calendar.MONTH)+1)+"/"+calendar.get(C
alendar.DATE)+"/"+calendar.get(Calendar.YEAR);
strStatus=strTime+" "+strDate;
showStatus(strStatus);
setSize(700,500);
setVisible(true);
}
public void mousePressed(MouseEvent e)
{
mouseUp = false;
start = new Point(e.getX(), e.getY());
}
public void mouseReleased(MouseEvent e)
{
if(Circle){
end = new Point(e.getX(), e.getY());
} else {
end = new Point(Math.max(e.getX(), start.x),
Math.max(e.getY(), start.y));
start = new Point(Math.min(e.getX(), start.x),
Math.min(e.getY(), start.y));
}
mouseUp = true;
repaint();
}
public void mouseDragged(MouseEvent e)
{
if(Ellipse){
dot[dots] = new Point(e.getX(), e.getY());
dots++;
repaint();
}
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void paint (Graphics g)
{
if (mouseUp) {
int width = end.x - start.x;
int height = end.y - start.y;
if(Circle){
g.drawOval(start.x, start.y, width, height);
}
else if(Rectangle){
g.drawRect(start.x, start.y, width, height);
}
else if(Polygon){
g.drawRoundRect(start.x, start.y, width, height,
10, 10);
}
else if(Ellipse){
for(int loop_index = 0; loop_index < dots - 1;
loop_index++){
g.drawLine(dot[loop_index].x, dot[loop_index].y,
dot[loop_index + 1].x, dot[loop_index + 1].y);
}
}
}
}
public void actionPerformed(ActionEvent e)
{
setFlagsFalse();
if(e.getSource() == poly)Polygon = true;
if(e.getSource() == circle)Circle = true;
if(e.getSource() == rect)Rectangle = true;
if(e.getSource() == ellipse)Ellipse = true;
}
void setFlagsFalse()
{
Polygon = false;
Circle = false;
Rectangle = false;
Ellipse = false;
}
}
Firstly,before add listener to it ,the JTextarea show but please, assist me on how to let the buttons, labels and combobox be in NORTH and JTextarea in SOUTH and show clearly.
secondly, after add listener, it does not shown at all except time/date