Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Status Bar

Status
Not open for further replies.

M626

Programmer
Mar 13, 2002
299
I am having problems trying to create a status bar at the bottom that will be used to display all my event messages instead of using the JOptionPane.showMessageDialog. It get annoying after a while and I would rather have the diplay in a status bar. Can anyone help...i'll post the code I have now...

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

class Draw extends Frame implements
ActionListener, // menu item
ItemListener, // radio buttons
MouseListener, // mouse clicks
MouseMotionListener, // mouse movement
WindowListener { // Window

/* -------------------------------------------------------- */
/* */
/* FIELDS */
/* */
/* -------------------------------------------------------- */

static final int WIDTH = 800; // Sketch pad width
static final int HEIGHT = 600; // Sketch pad height
int upperLeftX, upperLeftY; // Rectangle upper left corner coords.
int width, height; // Rectangle size
int x1, y1, x2, y2; // Point coords.
boolean fillFlag = false;
boolean eraseFlag = false;
String drawColor = new String("black");
String drawShape = new String("line");

// Help

String helpText = "THIS APPLET ALLOWS YOU TO DRAW LIKE A PAINT PAD" +
"\n" + "PLUS INFORMS YOU WHEN VARIOUS EVENTS OCCUR";

// Components

TextField color = new TextField();
TextField shape = new TextField();
TextField position = new TextField();
CheckboxGroup fillOutline = new CheckboxGroup();
TextArea info = new TextArea(helpText,0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
Frame about = new Frame("About Terry's EventViewer");

// Menues

String[] colorNames = {"black","blue","cyan","gray","green","magenta","red","white"};
String[] shapeNames = {"line","square","rectangle","circle","ellipse"};
String[] toolNames = {"erase","clearpad"};
String[] helpNames = {"information","about"};


/* -------------------------------------------------------- */
/* */
/* CONSTRUCTORS */
/* */
/* -------------------------------------------------------- */

public Draw(String heading) {
super(heading);
setBackground(Color.yellow);
setLayout(null);

/* Initialise components */

initialiseTextFields();
initializeMenuComponents();
initializeRadioButtons();

addWindowListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}

/* -------------------------------------------------------- */
/* */
/* METHODS */
/* */
/* -------------------------------------------------------- */

/* Initialise Text Fields */

private void initialiseTextFields() {
// Add text field to show colour of figure
color.setLocation(5,490);
color.setSize(80,30);
color.setBackground(Color.white);
color.setText(drawColor);
add(color);

// Add text field to show shape of figure
shape.setLocation(5,525);
shape.setSize(80,30);
shape.setBackground(Color.white);
shape.setText(drawShape);
add(shape);

// Add text field to show position of mouse
position.setLocation(5,560);
position.setSize(80,30);
position.setBackground(Color.white);
add(position);

// Set up text field for help
info.setLocation(150,250);
info.setSize(500,100);
info.setBackground(Color.white);
info.setEditable(false);
}

/* Initialize Menu Components */

private void initializeMenuComponents() {
// Create menu bar

MenuBar bar = new MenuBar();

// Add colurs menu

Menu colors = new Menu("COLORS");
for(int index=0;index < colorNames.length;index++)
colors.add(colorNames[index]);
bar.add(colors);
colors.addActionListener(this);

// Add shapes menu

Menu shapes = new Menu(&quot;SHAPES&quot;);
for(int index=0;index < shapeNames.length;index++)
shapes.add(shapeNames[index]);
bar.add(shapes);
shapes.addActionListener(this);

// Add tools menu

Menu tools = new Menu(&quot;TOOLS&quot;);
for(int index=0;index < toolNames.length;index++)
tools.add(toolNames[index]);
bar.add(tools);
tools.addActionListener(this);

// Add help menu

Menu help = new Menu(&quot;HELP&quot;);
for(int index=0;index < helpNames.length;index++)
help.add(helpNames[index]);
bar.add(help);
help.addActionListener(this);

// Set up menu bar

setMenuBar(bar);
}

/* Initilalise Radio Buttons */

private void initializeRadioButtons() {

// Define checkbox

Checkbox fill = new Checkbox(&quot;fill&quot;,fillOutline,false);
Checkbox outline = new Checkbox(&quot;outline&quot;,fillOutline,true);

// Fill buttom

fill.setLocation(5,455);
fill.setSize(80,30);
add(fill);
fill.addItemListener(this);

// Outline button

outline.setLocation(5,420);
outline.setSize(80,30);
add(outline);
outline.addItemListener(this);
}

/* -------------------------------------------------------- */
/* */
/* LISTENERS */
/* */
/* -------------------------------------------------------- */

// ----------------------- ACTION LISTENERS -------------------

/* Action performed. Detects which item has been selected from a menu */

public void actionPerformed(ActionEvent event) {
{
JOptionPane.showMessageDialog(null, &quot;Action Listener In Process&quot;);
}
Graphics g = getGraphics();

String source = event.getActionCommand();

// Identify chosen colour if any

for (int index=0;index < colorNames.length;index++) {
if (source.equals(colorNames[index])) {
drawColor = colorNames[index];
color.setText(drawColor);
return;
}
}

// Identify chosen shape if any

for (int index=0;index < shapeNames.length;index++) {
if (source.equals(shapeNames[index])) {
drawShape = shapeNames[index];
shape.setText(drawShape);
return;
}
}

// Identify chosen tools if any

if (source.equals(&quot;erase&quot;)) {
eraseFlag= true;
return;
}
else {
if (source.equals(&quot;clearpad&quot;)) {
remove(info);
g.clearRect(0,0,800,600);
return;
}
}

// Identify chosen help

if (source.equals(&quot;information&quot;)) {
add(info);
return;
}
else displayAboutWindow(about);
}

/* Dispaly About Window: Shows iformation aboutb Draw programme in
separate window */

private void displayAboutWindow(Frame about) {
about.setLocation(300,300);
about.setSize(350,160);
about.setBackground(Color.cyan);
about.setFont(new Font(&quot;Serif&quot;,Font.ITALIC,14));
about.setLayout(new FlowLayout(FlowLayout.LEFT));

about.add(new Label(&quot;Author: Terry Dhanraj&quot;));
about.add(new Label(&quot;Title: Event Viewer&quot;));
about.add(new Label(&quot;Date: Tuesday, 14 May 2002&quot;));
about.add(new Label(&quot;FINAL PROJECT NEEDED TO GRADUATE&quot;));
about.setVisible(true);
about.addWindowListener(this);
}

// ----------------------- ITEM LISTENERS -------------------

/* Item state changed: detect radio button presses. */

public void itemStateChanged(ItemEvent event) {
JOptionPane.showMessageDialog(null, &quot;Item Listener In Process&quot;);
if (event.getItem() == &quot;fill&quot;) fillFlag=true;
else if (event.getItem() == &quot;outline&quot;) fillFlag=false;
}

// ---------------------- MOUSE LISTENERS -------------------

/* Blank mouse listener methods */

public void mouseClicked(MouseEvent event) {{
JOptionPane.showMessageDialog(null, &quot;Mouse Listener In Process&quot;);
}}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}

/* Mouse pressed: Get start coordinates */

public void mousePressed(MouseEvent event) {

// Cannot draw if erase flag switched on.

if (eraseFlag) return;

// Else set parameters to 0 and proceed
upperLeftX=0;
upperLeftY=0;
width=0;
height=0;

x1=event.getX();
y1=event.getY();

Graphics g = getGraphics();

// Display start point

g.drawString(&quot;.&quot;,x1,y1);
displayMouseCoordinates(x1,y1);
}

public void mouseReleased(MouseEvent event) {
Graphics g = getGraphics();

// Get and display mouse coordinates

x2=event.getX();
y2=event.getY();
displayMouseCoordinates(x2,y2);

// If erase flag set to true reset to false

if (eraseFlag) {
eraseFlag = false;
return;
}

// Else draw shape

selectColor(g);
if (drawShape.equals(&quot;line&quot;)) g.drawLine(x1,y1,x2,y2);
else drawClosedShape(drawShape,g);

// Remove reference point

g.setColor(Color.yellow);
g.drawString(&quot;.&quot;,x1,y1);
g.setColor(Color.black);
}

/* Display Mouse Coordinates */

private void displayMouseCoordinates(int x, int y) {
position.setText(&quot;[&quot; + String.valueOf(x) + &quot;,&quot; + String.valueOf(y) + &quot;]&quot;);
}

/* Select colour */

private void selectColor(Graphics g) {
for (int index=0;index < colorNames.length;index++) {
if (drawColor.equals(colorNames[index])) {
switch(index) {
case 0: g.setColor(Color.black);break;
case 1: g.setColor(Color.blue);break;
case 2: g.setColor(Color.cyan);break;
case 3: g.setColor(Color.gray);break;
case 4: g.setColor(Color.green);break;
case 5: g.setColor(Color.magenta);break;
case 6: g.setColor(Color.red);break;
case 7: g.setColor(Color.white);break;
default: g.setColor(Color.yellow);
}
}
}
}

/* Draw closed shape */

private void drawClosedShape(String shape,Graphics g) {
// Calculate correct parameters for shape

upperLeftX = Math.min(x1,x2);
upperLeftY = Math.min(y1,y2);
width = Math.abs(x1-x2);
height = Math.abs(y1-y2);

// Draw appropriate shape

if (shape.equals(&quot;square&quot;)) {
if (fillFlag) g.fillRect(upperLeftX,upperLeftY,width,width);
else g.drawRect(upperLeftX,upperLeftY,width,width);
}
else {
if (shape.equals(&quot;rectangle&quot;)) {
if (fillFlag) g.fillRect(upperLeftX,upperLeftY,width,height);
else g.drawRect(upperLeftX,upperLeftY,width,height);
}
else {
if (shape.equals(&quot;circle&quot;)) {
if (fillFlag) g.fillOval(upperLeftX,upperLeftY,width,width);
else g.drawOval(upperLeftX,upperLeftY,width,width);
}
else {
if (fillFlag) g.fillOval(upperLeftX,upperLeftY,width,height);
else g.drawOval(upperLeftX,upperLeftY,width,height);
}
}
}
}

// ----------------------- MOUSE MOVEMENT --------------------

/* Mouse moved */

public void mouseMoved(MouseEvent event){
//{
// JOptionPane.showMessageDialog( null , &quot;Mouse Motion Listener In Process&quot;);
//}
displayMouseCoordinates(event.getX(),event.getY());
}

/* Mouse dragged */

public void mouseDragged(MouseEvent event) {
Graphics g = getGraphics();

x2=event.getX();
y2=event.getY();

if (eraseFlag) g.clearRect(x2,y2,5,5);
displayMouseCoordinates(x1,y1);
}

// ---------------------- WINDOW LISTENERS -------------------

/* Blank methods for window listener */

public void windowClosed(WindowEvent event) {}
public void windowDeiconified(WindowEvent event) {}
public void windowIconified(WindowEvent event) {}
public void windowActivated(WindowEvent event) {}
public void windowDeactivated(WindowEvent event) {}
public void windowOpened(WindowEvent event) {}

/* Window Closing */

public void windowClosing(WindowEvent event) {
if (event.getWindow() == about) {
about.dispose();
return;
}
else System.exit(0);
}
}
 
Before we dive into this ... what problems are you having?
 
Thanks, I figured it out... but thanks for responding.
 
To M626,

What was the problem, and what did you do to correct it?

Cheers,
Dave (JAVA newbe tobe)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top