Results 1 to 2 of 2
  1. #1
    idreamavi is offline Member
    Join Date
    Feb 2015
    Posts
    3
    Rep Power
    0

    Default my program is executing properly but not getting desired output

    my problem is to write a java program that allows the user to draw lines, rectangles and ovals and i have written code like this but program is executing properly and i am not getting desired output, so please help me out this and my code is


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class DrawShapes extends JApplet
    {
    int x1, y1, x2, y2;
    private JButton choices[];
    private String names[] = { "Line", "Rectangle", "Oval" };
    private JPanel buttonPanel;
    private DrawPanel drawingArea;
    private int width = 300, height = 200;
    public void init()
    {
    drawingArea = new DrawPanel( width, height );
    choices = new JButton[ names.length ];
    buttonPanel = new JPanel();
    buttonPanel.setLayout( new GridLayout( 1, choices.length ) );
    ButtonHandler handler = new ButtonHandler();
    for ( int i = 0; i < choices.length; i++ )
    {
    choices[ i ] = new JButton( names[ i ] );
    buttonPanel.add( choices[ i ] );
    choices[ i ].addActionListener( handler );
    }
    Container c = getContentPane();
    c.add( buttonPanel, BorderLayout.NORTH );
    c.add( drawingArea, BorderLayout.CENTER );
    }
    public void setWidth( int w )
    {
    width = ( w >= 0 ? w : 300 );
    }
    public void setHeight( int h )
    {
    height = ( h >= 0 ? h : 200 );
    }
    public static void main( String args[] )
    {
    int width, height;
    if ( args.length != 2 )
    {
    width = 300;
    height = 200;
    }
    else
    {
    width = Integer.parseInt( args[ 0 ] );
    height = Integer.parseInt( args[ 1 ] );
    }
    JFrame applicationWindow = new JFrame( "An applet running as an application" );
    applicationWindow.addWindowListener( new WindowAdapter()
    {
    public void windowClosing( WindowEvent e )
    {
    System.exit( 0 );
    }
    } );
    DrawShapes appletObject = new DrawShapes();
    appletObject.setWidth( width );
    appletObject.setHeight( height );
    appletObject.init();
    appletObject.start();
    applicationWindow.getContentPane().add( appletObject );
    applicationWindow.setSize( width, height );
    applicationWindow.show();
    }
    private class ButtonHandler implements ActionListener
    {
    public void actionPerformed( ActionEvent e )
    {
    for ( int i = 0; i < choices.length; i++ )
    if ( e.getSource() == choices[ i ] )
    {
    drawingArea.setCurrentChoice( i );
    break;
    }
    }
    }
    }
    class DrawPanel extends JPanel implements MouseListener,MouseMotionListener
    {
    private int x1, y1, x2, y2;
    private int currentChoice = -1; // don't draw first time
    private int width = 100, height = 100;
    public DrawPanel( int w, int h )
    {
    width = ( w >= 0 ? w : 100 );
    height = ( h >= 0 ? h : 100 );
    }
    public void paintComponent( Graphics g )
    {
    super.paintComponent( g );

    switch( currentChoice )
    {
    case 0:
    g.drawLine(x1,y1,x2,y2);
    break;
    case 1:
    g.drawRect( x1,y1,x2,y2 );
    break;
    case 2:
    g.drawOval( x1,y1,x2,y2 );
    break;
    }
    }
    public void setCurrentChoice( int c )
    {
    currentChoice = c;
    repaint();
    }
    @Override
    public void mouseClicked(MouseEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mousePressed(MouseEvent e)
    {
    x1=e.getX();
    y1 = e.getY();
    System.out.println(x1+""+y1);
    }

    @Override
    public void mouseReleased(MouseEvent e)
    {
    x2=e.getX();
    y2 = e.getY();
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseExited(MouseEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseDragged(MouseEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,904
    Rep Power
    30

    Default Re: my program is executing properly but not getting desired output

    program is executing properly and i am not getting desired output,
    Please explain. What does the program does now?
    What do you want the program to do differently?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. linked stack when print does read desired output
    By Zompire in forum New To Java
    Replies: 6
    Last Post: 07-28-2016, 11:59 PM
  2. Replies: 1
    Last Post: 11-27-2014, 01:14 AM
  3. Replies: 3
    Last Post: 11-15-2013, 10:03 AM
  4. Output to file not writing out desired string
    By alex.start in forum New To Java
    Replies: 11
    Last Post: 10-02-2013, 08:58 AM
  5. Replies: 1
    Last Post: 02-13-2012, 03:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •