Hi,
I have created an applet in which it is possible to select a shape from the combo box and the the shape moves with the mouse.Everything works just fine except a few problems.
1. The combo box is only displayed when mouse is moved over it.
2. The triangle moves according to how the mouse moves, but there is a lot of distance between the mouse pointer and the triangle.

Can anyone please help me solve this problem.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JComboBox;
public class MyShape extends Applet
implements MouseListener, MouseMotionListener,ItemListener{
    int width, height;
    int mx, my;
    boolean isButtonPressed = false;
    String[] description = {"Square","Circle","Triangle","Rectangle" };
    JComboBox c = new JComboBox();
     public void init()
     {
      width = getSize().width;
      height = getSize().height;
      setBackground( Color.black );
      
      addMouseListener( this );
      addMouseMotionListener( this );
      int count = 0;
      for (int i = 0; i < 4; i++){
       c.addItem(description[count++]);
      }
      c.addItemListener(this);
      add(c);
    }
   public void itemStateChanged(ItemEvent e){

   }

   public void mouseEntered( MouseEvent e ) {}
   public void mouseExited( MouseEvent e ) {}
   public void mouseClicked( MouseEvent e ) {}
   public void mousePressed( MouseEvent e ) {
      isButtonPressed = true;
      setBackground( Color.white );
      repaint();
      e.consume();
   }
   public void mouseReleased( MouseEvent e ) { 
      isButtonPressed = false;
      setBackground( Color.black );
      repaint();
      e.consume();
   }
   public void mouseMoved( MouseEvent e ) {
      mx = e.getX();
      my = e.getY();
      showStatus( "Mouse at (" + mx + "," + my + ")" );
      repaint();
      e.consume();
   }
   public void mouseDragged( MouseEvent e ) {
      mx = e.getX();
      my = e.getY();
      showStatus( "Mouse at (" + mx + "," + my + ")" );
      repaint();
      e.consume();
   }
   public void paint( Graphics g ) {

      if (isButtonPressed) {
         g.setColor( Color.black);
      }
      else {
         g.setColor( Color.white);
      }
      int pointx[]={mx-100,mx-50,mx-150};
      int pointy[]={my-100,my-200,my-200};
      if(c.getSelectedItem()=="Square")
             g.fillRect( mx-20, my-20, 40, 40 );
      if(c.getSelectedItem()=="Circle")
             g.fillArc(mx-20,my-20,40,40, 360, 360);
      if(c.getSelectedItem()=="Rectangle")
             g.fillRect( mx-20, my-20, 80, 40 );
      if(c.getSelectedItem()=="Triangle")
             g.fillPolygon(pointx, pointy,3);
   }
}

1. By overriding paint(Graphics g) you have taken responsibility for painting the whole applet - including the combo box.
Normal solution is to call super.paint(g) as the first line of your paint so everything "normal" gets painted, then go on to draw your shapes.

2. the triangle is offset because you draw it at int pointx[]={mx-100,mx-50,mx-150}; int pointy[]={my-100,my-200,my-200};

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.