help why doesn't my program work


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: help why doesn't my program work

  1. #1
    Ray Guest

    help why doesn't my program work


    import java.awt.Color;
    import java.awt.*;
    import java.awt.event.MouseListener;
    import javax.swing.*;
    import java.awt.Graphics;
    import java.awt.Shape;
    import java.awt.Graphics2D;
    import java.awt.event.MouseEvent;
    import java.awt.Dimension;
    import java.awt.Paint;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.Rectangle;

    public class MouseTracker extends JPanel implements MouseListener {

    private JPanel p;
    private JFrame f;
    private Rectangle square;

    public void run( )
    {
    f = new JFrame( "Graphics" );
    f.setSize( 500, 500 );
    f.setVisible( true );



    // adding event listener using inner class
    f.addWindowListener( new WindowAdapter( )
    {
    public void windowClosing( WindowEvent e )
    {
    System.exit( 0 );
    }
    });


    p = new JPanel( );
    p.setSize(500,500);
    f.setContentPane( p );
    p.addMouseListener(this);

    }



    // MouseListener event handlers
    public void mouseClicked( MouseEvent e )
    {

    int x = e.getX();
    int y = e.getY();

    Graphics g = this.getGraphics();

    Graphics2D g2 = (Graphics2D)g;


    if (e.isMetaDown())
    {
    p.getContentPane().add(square);

    square = new Rectangle(20,20);

    square.setLocation(x, y);

    g2.draw(square);


    }
    else
    {
    g2.drawOval(x, y, 20, 20);
    //p.repaint();
    }
    }

    public void mousePressed( MouseEvent e ){}
    public void mouseReleased( MouseEvent e ){}

    public void mouseEntered( MouseEvent e )
    {
    p.setBackground(Color.pink);
    p.repaint();
    }

    public void mouseExited( MouseEvent e )
    {
    p.setBackground(Color.lightGray);
    p.repaint();
    }

    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);

    }

    public static void main( String args[] )
    {
    MouseTracker app = new MouseTracker();

    app.run();
    }
    }



  2. #2
    Kent Guest

    Re: help why doesn't my program work


    Firstly, your question should be "why doesn't my program compile?"

    The answer is because of this line:

    p.getContentPane().add(square);

    You're trying to add a non-component (Rectangle) to a container.


    Secondly, you're trying to draw on a panel in a mouse event. You need to
    do this in the paint() method for the panel.

    Take a look at this and see if it makes sense. If not, post again:




    /////////////////////////
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class MouseTracker extends JFrame {
    public static void main(String[] args) {
    MouseTracker mt = new MouseTracker();
    mt.setVisible(true);
    }

    public MouseTracker() {
    this.getContentPane().add(new MouseTrackerPanel());
    this.addMouseListener(new MyMouseListener());
    this.pack();
    this.setSize(300,300);
    }

    private class MouseTrackerPanel extends JPanel {
    public MouseTrackerPanel() {
    }

    public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;

    if (MouseTracker.this.x != -1) {
    Rectangle r = new Rectangle(10, 10);
    r.setLocation(x, y);
    g2d.draw(r);
    }
    }
    }

    private class MyMouseListener extends MouseAdapter {
    public void mouseClicked(MouseEvent e) {
    MouseTracker.this.x = e.getX();
    MouseTracker.this.y = e.getY();
    MouseTracker.this.repaint();
    }
    }

    private int x = -1;
    private int y = -1;
    }
    ////////////////

    Regards,
    Kent











    "Ray" <figueror@fiu.edu> wrote:
    >
    >import java.awt.Color;
    >import java.awt.*;
    >import java.awt.event.MouseListener;
    >import javax.swing.*;
    >import java.awt.Graphics;
    >import java.awt.Shape;
    >import java.awt.Graphics2D;
    >import java.awt.event.MouseEvent;
    >import java.awt.Dimension;
    >import java.awt.Paint;
    >import java.awt.event.WindowAdapter;
    >import java.awt.event.WindowEvent;
    >import java.awt.Rectangle;
    >
    >public class MouseTracker extends JPanel implements MouseListener {
    >
    > private JPanel p;
    > private JFrame f;
    > private Rectangle square;
    >
    > public void run( )
    > {
    > f = new JFrame( "Graphics" );
    > f.setSize( 500, 500 );
    > f.setVisible( true );
    >
    >
    >
    > // adding event listener using inner class
    > f.addWindowListener( new WindowAdapter( )
    > {
    > public void windowClosing( WindowEvent e )
    > {
    > System.exit( 0 );
    > }
    > });
    >
    >
    > p = new JPanel( );
    > p.setSize(500,500);
    > f.setContentPane( p );
    > p.addMouseListener(this);
    >
    > }
    >
    >
    >
    > // MouseListener event handlers
    > public void mouseClicked( MouseEvent e )
    > {
    >
    > int x = e.getX();
    > int y = e.getY();
    >
    > Graphics g = this.getGraphics();
    >
    > Graphics2D g2 = (Graphics2D)g;
    >
    >
    > if (e.isMetaDown())
    > {
    > p.getContentPane().add(square);
    >
    > square = new Rectangle(20,20);
    >
    > square.setLocation(x, y);
    >
    > g2.draw(square);
    >
    >
    > }
    > else
    > {
    > g2.drawOval(x, y, 20, 20);
    > //p.repaint();
    > }
    > }
    >
    > public void mousePressed( MouseEvent e ){}
    > public void mouseReleased( MouseEvent e ){}
    >
    > public void mouseEntered( MouseEvent e )
    > {
    > p.setBackground(Color.pink);
    > p.repaint();
    > }
    >
    > public void mouseExited( MouseEvent e )
    > {
    > p.setBackground(Color.lightGray);
    > p.repaint();
    > }
    >
    > public void paintComponent(Graphics g)
    > {
    > super.paintComponent(g);
    >
    > }
    >
    > public static void main( String args[] )
    > {
    > MouseTracker app = new MouseTracker();
    >
    > app.run();
    > }
    >}
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center