Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Java in General
Why does it not show the new coordinates?!!??!!
Rob Shan Lone
Greenhorn
Posts: 8
posted 21 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Shape extends Applet implements ActionListener, ItemListener { // text fields and associated labels private TextField area, shapeWidth, shapeHeight, shapex ,shapey; private Label areaLabel, widthLabel, heightLabel, xLabel, yLabel; // choice buttons and associated labels private Choice shapeColors, shapes; private Label colorLabel, shapeLabel; // reference to our object - note that currentShape may not be instantiated as it is an abstract class! private TwoDimensionalShape currentShape; // this will be used as a superclass reference to a subclass object // color information for the color choice button private Color colorList[] = { Color.black, Color.red, Color.blue, Color.green, Color.pink, Color.cyan, Color.orange, Color.yellow, Color.gray, Color.magenta }; private String colorNames[] = { "Black", "Red", "Blue", "Green", "Pink", "Cyan", "Orange", "Yellow", "Grey", "Magenta" }; // shape information for the shape choice button private String shapeNames[] = { "Circle", "Square", "Triange"};//CG // constants to provide symbolic references to shape name index positions private final int CIRCLE = 0; private final int SQUARE = 1; private final int TRIANGLE = 2; // graphics parameters: (X,Y) coordinate, default height and width for object public int xb = 100; public int yb = 200; private final int DEFAULTSIZE = 30; private final Color BLACKNESS = Color.black; // init: first method called when applet starts up - used to initialize the GUI public void init() { // turn off layout manager setLayout( null ); // create width and height labels and text fields; add them to the applet widthLabel = new Label( "Width:" ); shapeWidth = new TextField( 3 ); heightLabel = new Label( "Height:" ); shapeHeight = new TextField( 3 ); add( widthLabel ); add( shapeWidth ); add( heightLabel ); add( shapeHeight ); // create x and y labels and text fields; add them to the applet xLabel = new Label( "X Coord:" ); shapex = new TextField( 10 ); yLabel = new Label( "Y Coord:" ); shapey = new TextField( 10 ); add( xLabel ); add( shapex ); add( yLabel ); add( shapey ); // create the area label text field - this text field is non-editable areaLabel = new Label( "Area:" ); area = new TextField( 8 ); area.setEditable( false ); add( areaLabel ); add( area ); // create the color choice button and label colorLabel = new Label( "Colors:" ); add( colorLabel ); shapeColors = new Choice(); for ( int i = 0; i < colorNames.length; i++ ) // note creation of variable i: scope is the for loop shapeColors.addItem( colorNames[i] ); // note array use... add( shapeColors ); // create the shape choice button and label shapeLabel = new Label( "Shapes:" ); add( shapeLabel ); shapes = new Choice(); for ( int i = 0; i < shapeNames.length; i++ ) shapes.addItem( shapeNames[i] ); // again note array use... add( shapes ); // place GUI objects where we want them on the applet: upper left (x,y) coordinate, width, height widthLabel.reshape( 10, 10, 40, 20 ); shapeWidth.reshape( 50, 10, 45, 20 ); heightLabel.reshape( 100, 10, 40, 20 ); shapeHeight.reshape( 145, 10, 45, 20 ); areaLabel.reshape( 200, 10, 35, 20 ); area.reshape( 235, 10, 100, 20 ); colorLabel.reshape( 10, 35, 50, 20 ); shapeColors.reshape( 60, 35, 100, 20 ); shapeLabel.reshape( 185, 35, 50, 20 ); shapes.reshape( 235, 35, 100, 20 ); xLabel.reshape( 360, 10, 100, 20 ); shapex.reshape( 470, 10, 100, 20 ); yLabel.reshape( 580, 10, 100, 20 ); shapey.reshape( 690, 10, 100, 20 ); // initialize our shape and initial display information currentShape = new Square( xb, yb, DEFAULTSIZE, DEFAULTSIZE, BLACKNESS ); shapeWidth.setText( Integer.toString( DEFAULTSIZE ) ); shapeHeight.setText( Integer.toString( DEFAULTSIZE ) ); area.setText( Double.toString( currentShape.getArea() ) ); shapex.setText( Double.toString( xb ) ); shapey.setText( Double.toString( yb ) ); //enable event handlers - Java requires registering components if we want to process their events shapeWidth.addActionListener( this ); shapeHeight.addActionListener( this ); shapex.addActionListener( this ); shapey.addActionListener( this ); shapes.addItemListener( this ); shapeColors.addItemListener( this ); } // actionPerformed: called whenever an action event (value entered in a text field) occurs. public void actionPerformed( ActionEvent e ) { // check the shape width text field if ( e.getSource() == shapeWidth ) { currentShape.setWidth( Integer.parseInt( shapeWidth.getText() ) ); area.setText( Double.toString( currentShape.getArea() ) ); } // check the shape height text field if ( e.getSource() == shapeHeight ) { currentShape.setHeight( Integer.parseInt( shapeHeight.getText() ) ); area.setText( Double.toString( currentShape.getArea() ) ); } // check the shape x coord text field if ( e.getSource() == shapex ) { currentShape.setx( Integer.parseInt( shapex.getText() ) ); } // check the shape y coord text field if ( e.getSource() == shapey ) { currentShape.sety( Integer.parseInt( shapey.getText() ) ); } repaint(); } // itemStateChanged: called whenever an item event (selection of a choice item) occurs. public void itemStateChanged( ItemEvent e ) { // check the shapes choice button if ( e.getSource() == shapes ) { // get the current height, width, and color, x, y int high = currentShape.getHeight(); int wide = currentShape.getWidth(); Color color = currentShape.getColor(); int xb = currentShape.getxcoord(); int yb = currentShape.getycoord(); // create the new shape using same parameters as last shape. Note that we are assigning // a just-instantiated subclass object to a superclass reference - this allows polymorphism. switch ( shapes.getSelectedIndex() ) { case CIRCLE: currentShape = new Circle(xb, yb, high, wide, color ); // showing use of one constructor method.... break; case SQUARE: currentShape = new Square(xb, yb, high, wide, color ); // now showing the other constructor method. break; case TRIANGLE: currentShape = new Triangle(xb, yb, high, wide, color ); break; } area.setText( Double.toString( currentShape.getArea() ) ); } // check the color selection choice button if ( e.getSource() == shapeColors ) { currentShape.setColor( colorList[ shapeColors.getSelectedIndex() ] ); } repaint(); // repaint automatically calls update(), then paint(). } // paint: updates the graphics display public void paint( Graphics g ) { currentShape.draw( g, xb , yb ); // only one line needed due to polymorphic behavior! } } import java.awt.*; public abstract class TwoDimensionalShape extends Shape { // instance variables private Color objectColor; // stores a Color object for the color of the shape private int height; // height of shape private int width; // width of shape private int x; private int y; // setColor: sets the shape's color public void setColor( Color color ) { objectColor = color; } // getColor: returns the shape's color public Color getColor() { return objectColor; } // setHeight: sets the height of the shape. Note that the height is forced to be at least 1 unit high. public void setHeight( int high ) { if ( high > 0 ) height = high; else height = 1; } // setWidth: sets the width of the shape. Note that the width is forced to be at least 1 unit wide. public void setWidth( int wide ) { if ( wide > 0 ) width = wide; else width = 1; } // setx: sets the x coordinates of the shape. public void setx( int xb ) { x = xb; } // sety: sets the y coordinates of the shape. public void sety( int yb ) { y = yb; } // getHeight: returns the height of the shape. public int getHeight() { return height; } // getWidth: returns the width of the shape. public int getWidth() { return width; } // getxcoord: returns the x coordinates of the shape. public int getxcoord() { return x; } // getycoord: returns the y coordinates of the shape. public int getycoord() { return y; } // draw: abstract method that must be defined in subclass. This is done because // different shapes will require different graphics calls in order to draw the shape. public abstract void draw( Graphics g, int X_BASE, int Y_BASE ); // getArea: abstract method that must be defined in subclass. This is done because // different shapes have different formulas for the calculation of area. public abstract double getArea(); } import java.awt.* ; public class Circle extends TwoDimensionalShape { private int x ; private int y ; private int wide ; private int high ; private Color color ; public Circle( int x , int y , int wide , int high , Color color ) { this.x = x ; this.y = y ; this.wide = wide ; this.high = high ; this.color = color ; } public double getArea() { return Math.PI * 0.5 * getHeight() * 0.5 * getWidth(); } public void draw( Graphics g, int x, int y ) { Color temp = g.getColor(); g.setColor( getColor() ); g.fillOval( x , y , wide , high ); g.setColor( temp ); } } import java.awt.* ; public class Square extends TwoDimensionalShape { private int x ; private int y ; private int wide ; private int high ; private Color color ; public Square( int x , int y , int wide , int high , Color color ) { this.x = x ; this.y = y ; this.wide = wide ; this.high = high ; this.color = color ; } public double getArea() { return getHeight() * getWidth(); } public void draw( Graphics g, int x, int y ) { Color temp = g.getColor(); g.setColor( getColor() ); g.fillRect( x , y , wide , high ); g.setColor( temp ); } } import java.awt.*; public class Triangle extends TwoDimensionalShape { private final int numPoints = 3; private int xValues[] = new int[ numPoints ]; private int yValues[] = new int[ numPoints ]; private int x ; private int y ; private int wide ; private int high ; private Color color ; // constructor that allows initial color to be set by calling method. public Triangle( int x, int y, int high, int wide, Color color ) { this.x = x ; this.y = y ; this.wide = wide ; this.high = high ; this.color = color ; } public double getArea() { return 0.5 * getHeight() * getWidth(); } public void draw( Graphics g, int x, int y ) { Color temp = g.getColor(); // set up the (X,Y) coordinates for the polygon. // note how we indicate the X-Y pairs with our coding format... xValues[0] = x; yValues[0] = y; xValues[1] = x; yValues[1] = y + getHeight(); xValues[2] = x + getWidth(); yValues[2] = y + getHeight(); g.setColor( getColor() ); g.fillPolygon( xValues, yValues, numPoints ); g.setColor( temp ); } }
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
I like...
posted 21 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
you'll have to give a bit more detail about the problem and less about the code.
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Polymorphism 2!?!!??!
Polymorphism issue in the test code
Null Pointer Exception
Polymorphism!?!!??!
Parallel dragging
More...