Tech Support banner

Status
Not open for further replies.
1 - 1 of 1 Posts

·
Registered
Joined
·
14 Posts
Discussion Starter #1
Java help

The idea of the program is to allow the user to change color and shape. When the circle and square are displayed the scroll bars should be syncronized with shape size. Also displayed in text box is width and text.

The error I keep getting is a stack overflow occurred. When I google this it states that I have something too deeply embedded, but I could not figure out what it was. I believe there may be something wrong with my syncronize method, but I have looked at it so many times I am sure that it is something really stupid that I have overlooked.

An anyone make a suggestion where I might have gone wrong in my coding.

Thanks!
*********************************************************
*******************following is my code***********************
*********************************************************


//Abstract Windows Template classes

import java.awt.*;

// Abstract Windows Template classes for event handling

import java.awt.event.*;

// Applet classes
import java.applet.Applet.*;

// Java Foundation Swing classes

import javax.swing.*;

// Java Beans Component Library layout classes

import com.borland.jbcl.layout.*;

/**
* <p>Title: Shapes</p>
*
* <p>Description: Various Graphic Shapes on a JPanel</p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: Old Dominion University</p>
*
* @author Lori Hoffman
* @version 1.0
*/

// The "Shapes" class is inheritied from "JApplet"

public class Shapes extends JApplet
{
// The class data members are defined

boolean isStandalone = false;

// The "XYLayout" manager enables precise location of objects
// within a "Panel" by their x, y values. This is a JBuilder
// layout manager that works well if the applet development
// platform and the platform the applet will run on is the same.

XYLayout xYLayout1 = new XYLayout();
XYLayout xYLayout2 = new XYLayout();
XYLayout xYLayout3 = new XYLayout();
XYLayout xYLayout4 = new XYLayout();
XYLayout xYLayout5 = new XYLayout();
XYLayout xYLayout6 = new XYLayout();
XYLayout xYLayout7 = new XYLayout();
XYLayout xYLayout8 = new XYLayout();
XYLayout xYLayout9 = new XYLayout();
XYLayout xYLayout10 = new XYLayout();
XYLayout xYLayout11 = new XYLayout();

// JPanel objects are visual containers to group other objects.

JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
JPanel jPanel5 = new JPanel();
JPanel jPanel7 = new JPanel();
JPanel jPanel8 = new JPanel();

// This JPanel object will be the visual container on which the
// drawn objects will be displayed.

JPanel drawingBoard = new JPanel();

// JLabel objects are static text used for labels.

JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();

// JTextField objects are used for entering and displaying text.

JTextField widthText = new JTextField();
JTextField heightText = new JTextField();

// ButtonGroup objects are used to indicate the relationship
// between JRadioButtons.

ButtonGroup selectShape = new ButtonGroup();

// JRadioButton objects are used to indicate option selections.

JRadioButton ellipse = new JRadioButton();
JRadioButton circle = new JRadioButton();
JRadioButton rectangle = new JRadioButton();
JRadioButton square = new JRadioButton();

// JButton objects are used to trigger some action in the
// interface.

JButton exit = new JButton();
JButton start = new JButton();

// Scroll bar objects are declared.

JScrollBar heightScroll = new JScrollBar();
JScrollBar widthScroll = new JScrollBar();

// Combination control objects are declared.

JComboBox colorComboBox = new JComboBox();

// Objects that control the graphic objects are declared

Graphics drawingBoardGraphic;
String shapeType = new String();
Rectangle shapeSize = new Rectangle();
Color shapeColor;


// ****************** Shapes ****************************
// The class constructor function. Constructor functions are
// typically used to initialize class variables. This task is
// now usually done in the "init" or initialize method.

public Shapes()
{
}
// ************Shapes end *********************************

// The init() method is called by the AWT when an applet is first
// loaded or reloaded. Override this method to perform whatever
// initialization your applet needs, such as initializing data
// structures, loading images or fonts, creating frame windows,
// setting the layout manager, or adding UI components.

public void init()
{
try
{
jbInit();
}
catch (Exception e)
{
// The printStackTrace() method displays a history of the
// method calls that lead to exception.

e.printStackTrace();
}
}
// ******** end init ***********************

// The jbInit() method is used by Borland to initialize the
// objects that comprise the graphic user interface.

private void jbInit() throws Exception
{
// The setLayout method set the layout manager for the
// current content pane. setBackground method sets color for
// the current content pane.

this.setSize(new Dimension(800, 600));
this.getContentPane().setLayout(xYLayout10);

// The layout manager for other panels are declared.

jPanel1.setLayout(xYLayout2);
jPanel2.setLayout(xYLayout1);
jPanel3.setLayout(xYLayout3);
jPanel4.setLayout(xYLayout4);
jPanel5.setLayout(xYLayout5);
drawingBoard.setLayout(xYLayout6);
jPanel7.setLayout(xYLayout8);
jPanel8.setLayout(xYLayout9);

// The attributes of additional objects are initialized by
// calling the set methods.

jPanel2.setBackground(Color.orange);
jPanel2.setToolTipText("");
jPanel3.setBackground(Color.orange);
jPanel3.setBorder(BorderFactory.createLineBorder(Color.black));
jPanel4.setBackground(new Color(243, 246, 140));
jPanel4.setBorder(BorderFactory.createRaisedBevelBorder());
jPanel5.setBackground(Color.orange);
jPanel5.setBorder(BorderFactory.createLineBorder(Color.black));
jPanel7.setBackground(Color.orange);
jPanel7.setBorder(BorderFactory.createRaisedBevelBorder());
jPanel8.setBackground(Color.orange);
jPanel8.setBorder(BorderFactory.createLoweredBevelBorder());

xYLayout10.setWidth(800);

jLabel1.setBackground(Color.orange);
jLabel1.setFont(new java.awt.Font("Arial", Font.BOLD, 30));
jLabel1.setBorder(BorderFactory.createRaisedBevelBorder());
jLabel1.setOpaque(true);
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("The Shape of Things to Come");
jLabel2.setFont(new java.awt.Font("Arial", Font.PLAIN, 14));
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setText("Width");
jLabel3.setFont(new java.awt.Font("Arial", Font.PLAIN, 14));
jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
jLabel3.setText("Height");
jLabel4.setFont(new java.awt.Font("Arial", Font.PLAIN, 14));
jLabel4.setHorizontalAlignment(SwingConstants.CENTER);
jLabel4.setText("Select Color");
jLabel5.setFont(new java.awt.Font("Arial", Font.PLAIN, 14));
jLabel5.setHorizontalAlignment(SwingConstants.CENTER);
jLabel5.setText("Select Shape");

exit.setBackground(Color.orange);
exit.setFont(new java.awt.Font("Arial", Font.BOLD, 14));
exit.setBorder(BorderFactory.createRaisedBevelBorder());
exit.setOpaque(true);
exit.setContentAreaFilled(false);
exit.setText("Exit");

start.setBackground(Color.orange);
start.setFont(new java.awt.Font("Arial", Font.BOLD, 14));
start.setBorder(BorderFactory.createRaisedBevelBorder());
start.setOpaque(true);
start.setActionCommand("");
start.setContentAreaFilled(false);
start.setText("Start");

drawingBoard.setBackground(new Color(243, 246, 140));
drawingBoard.setBorder(BorderFactory.createLineBorder(Color.black));

heightScroll.setBlockIncrement(1);
heightScroll.setMaximum(200);
heightScroll.setUnitIncrement(1);
heightScroll.setVisibleAmount(0);
heightScroll.setAlignmentX((float) 0.0);
heightScroll.setAlignmentY((float) 0.0);

widthScroll.setBlockIncrement(1);
widthScroll.setMaximum(200);
widthScroll.setOrientation(JScrollBar.HORIZONTAL);
widthScroll.setUnitIncrement(1);
widthScroll.setVisibleAmount(0);
widthScroll.setAlignmentX((float) 0.0);
widthScroll.setAlignmentY((float) 0.0);

widthText.setFont(new java.awt.Font("Arial", Font.PLAIN, 14));
widthText.setBorder(BorderFactory.createLoweredBevelBorder());

heightText.setFont(new java.awt.Font("Arial", Font.PLAIN, 14));
heightText.setBorder(BorderFactory.createLoweredBevelBorder());
heightText.setText("");

ellipse.setFont(new java.awt.Font("Arial", Font.PLAIN, 14));
ellipse.setOpaque(false);
ellipse.setHorizontalAlignment(SwingConstants.LEFT);
ellipse.setSelected(false);
ellipse.setText("Ellipse");

circle.setFont(new java.awt.Font("Arial", Font.PLAIN, 14));
circle.setOpaque(false);
circle.setHorizontalAlignment(SwingConstants.LEFT);
circle.setText("Circle");

rectangle.setFont(new java.awt.Font("Arial", Font.PLAIN, 14));
rectangle.setOpaque(false);
rectangle.setHorizontalAlignment(SwingConstants.LEFT);
rectangle.setText("Rectangle");

square.setFont(new java.awt.Font("Arial", Font.PLAIN, 14));
square.setOpaque(false);
square.setHorizontalAlignment(SwingConstants.LEFT);
square.setText("Square");

colorComboBox.setFont(new java.awt.Font("Arial", Font.PLAIN, 14));
colorComboBox.setEditor(null);
colorComboBox.setMaximumRowCount(6);

// ***********************************************
// Event Listeners for the Radio Buttons. (Shape)
// ***********************************************

square.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
square_itemStateChanged(e);
}
});

rectangle.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
rectangle_itemStateChanged(e);
}
});

ellipse.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
ellipse_itemStateChanged(e);
}
});

circle.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
circle_itemStateChanged(e);
}
});

// *********************************************************
// Event Listeners for the Button components. (Exit, Exit)
// *********************************************************

exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
exit_actionPerformed(e);
}
});

start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
start_actionPerformed(e);
}
});

// **************************************************************
// Event Listeners for the Scroll bar components. (Height, Width)
// **************************************************************

heightScroll.addAdjustmentListener(new AdjustmentListener()
{
public void adjustmentValueChanged(AdjustmentEvent e)
{
heightScroll_adjustmentValueChanged(e);
}
});

widthScroll.addAdjustmentListener(new AdjustmentListener()
{
public void adjustmentValueChanged(AdjustmentEvent e)
{
widthScroll_adjustmentValueChanged(e);
}
});

// ****************************************************************
// Event Listeners for the Combination control components. (Color)
// ****************************************************************

colorComboBox.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
colorComboBox_actionPerformed(e);
}
});


// Components are added to the applet

jPanel2.add(jPanel3, new XYConstraints(15, 16, 771, 569));
jPanel3.add(jPanel4, new XYConstraints(17, 15, 734, 536));
jPanel4.add(jLabel1, new XYConstraints(63, 7, 604, 86));
jPanel4.add(jLabel2, new XYConstraints(484, 442, 73, 22));
jPanel4.add(jLabel3, new XYConstraints(588, 441, 73, 22));
jPanel4.add(heightText, new XYConstraints(594, 476, 59, 31));
jPanel4.add(widthText, new XYConstraints(495, 476, 59, 31));
jPanel7.add(jLabel5, new XYConstraints(36, 74, 117, 25));
jPanel8.add(square, new XYConstraints(36, 132, 102, 26));
jPanel8.add(ellipse, new XYConstraints(36, 16, 102, 26));
jPanel8.add(circle, new XYConstraints(36, 55, 102, 26));
jPanel8.add(rectangle, new XYConstraints(36, 93, 102, 26));
selectShape.add(ellipse);
selectShape.add(circle);
selectShape.add(rectangle);
selectShape.add(square);
jPanel4.add(jPanel5, new XYConstraints(123, 119, 313, 293));
jPanel5.add(drawingBoard, new XYConstraints(7, 7, 296, 277));
jPanel4.add(jPanel7, new XYConstraints(479, 116, 188, 300));
jPanel4.add(widthScroll, new XYConstraints(123, 426, 315, 24));
jPanel4.add(start, new XYConstraints(321, 469, 86, 38));
jPanel4.add(exit, new XYConstraints(158, 469, 86, 38));
jPanel7.add(colorComboBox, new XYConstraints(39, 40, 106, 24));
jPanel7.add(jPanel8, new XYConstraints(14, 98, 157, 180));
jPanel7.add(jLabel4, new XYConstraints(54, 15, -1, -1));

this.getContentPane().add(jPanel1, new XYConstraints(10, 10, 800, -1));
this.getContentPane().add(jPanel2, new XYConstraints( -2, 0, 803, 601));
jPanel4.add(heightScroll, new XYConstraints(88, 114, 22, 299));

// Add items to the Color combo box for user selection

colorComboBox.addItem(" ");
colorComboBox.addItem("Blue");
colorComboBox.addItem("Cyan");
colorComboBox.addItem("Green");
colorComboBox.addItem("Magenta");
colorComboBox.addItem("Red");

}
// ******* jbInit end ***********************

// ******************************************
// "start" JButton
// ******************************************

public void start_actionPerformed(ActionEvent e)
{
drawShape();
}

// ******************************************
// "exit" JButton
// ******************************************

public void exit_actionPerformed(ActionEvent e)
{
System.exit(0);
}

// *******************************************
// "colorComboBox" method for choice of color.
// *******************************************

public void colorComboBox_actionPerformed(ActionEvent e)
{
Object selection = colorComboBox.getSelectedItem();

if (selection == "Cyan")
{ shapeColor = Color.cyan;}
if (selection == "Green")
{ shapeColor = Color.green;}
if (selection == "Magenta")
{shapeColor = Color.magenta;}
if (selection == "Red")
{ shapeColor = Color.red;}
else if (selection == "Blue")
{ shapeColor = Color.blue;}
}

// *******************************************
// "heightScroll" method for vertical sizing.
// *******************************************

public void heightScroll_adjustmentValueChanged(AdjustmentEvent e)
{

shapeSize.height = heightScroll.getMaximum() -
heightScroll.getValue();

if (shapeType == "Square" || shapeType == "Circle")
{
synchronize(shapeSize.height);
}
else
{
heightText.setText(" " + shapeSize.height);

}

drawShape();
}

// *******************************************
// "widthScroll" method for horizontal sizing.
// *******************************************

public void widthScroll_adjustmentValueChanged(AdjustmentEvent e)
{
shapeSize.width = widthScroll.getValue();

if (shapeType == "Square" || shapeType == "Circle")
{
synchronize(shapeSize.width);
}
else
{
widthText.setText(" " + shapeSize.width);

}

drawShape();

}

// **************************************************
// Radio Button event methods for selection of shape.
// **************************************************

public void ellipse_itemStateChanged(ItemEvent e)
{
shapeSize.width = 100;
shapeSize.height = 120;
shapeType = "Ellipse";
}

public void circle_itemStateChanged(ItemEvent e)
{
shapeSize.width = 100;
shapeSize.height = 100;
shapeType = "Circle";
synchronize(shapeSize.height);
}

public void rectangle_itemStateChanged(ItemEvent e)
{
shapeSize.width = 120;
shapeSize.height = 100;
shapeType = "Rectangle";
}

public void square_itemStateChanged(ItemEvent e)
{
shapeSize.width = 100;
shapeSize.height = 100;
shapeType = "Square";
synchronize(shapeSize.height);
}

// *******************************************************************
// The drawShape function. This user defined function is being used
// instead of the applet paint method. The advantage of this
// technique is that it is very unlikely that any of the applet's
// controls will be drawn over.
// *******************************************************************

public void drawShape()
{
Graphics drawingBoardGraphic = drawingBoard.getGraphics();

Dimension panelDimension = new Dimension(drawingBoard.getSize());

drawingBoardGraphic.setColor(new Color(243, 246, 140));
drawingBoardGraphic.fillRect(0, 0, panelDimension.width,
panelDimension.height);

shapeSize.x = (panelDimension.width - shapeSize.width) / 2;
shapeSize.y = (panelDimension.height - shapeSize.height) / 2;
drawingBoardGraphic.setColor(shapeColor);

if (shapeType == "Ellipse" || shapeType == "Circle")
{
drawingBoardGraphic.fillOval
(shapeSize.x, shapeSize.y, shapeSize.width, shapeSize.height);
}

else if (shapeType == "Rectangle" || shapeType == "Square")
{
drawingBoardGraphic.fillRect
(shapeSize.x, shapeSize.y, shapeSize.width, shapeSize.height);
}


heightText.setText(" " + shapeSize.height);
widthText.setText(" " + shapeSize.width);
}
// ************* drawShape end****************************

public void synchronize(int newValue)
{
shapeSize.width = newValue;
widthScroll.setValue(newValue);
widthText.setText(" " + newValue);
shapeSize.height = (newValue);
heightText.setText(" " + newValue);
heightScroll.setValue(shapeSize.height = 200 - heightScroll.getValue());

}
}
// *********** "Shapes" Class Definition End *******************
 
1 - 1 of 1 Posts
Status
Not open for further replies.
Top