开发者

Problem with drawing

For now the following code is supposed to draw a circle onMouseDragged over the black Canvas. Unfortunately it doesn't (= Do I miss something?

Thanks in advance...

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.applet.*;
import java.awt.Graphics.*;



public class IdrawApplet extends Applet  {  


    int mosX;
    int mosY;
    Panel pGadgets;
    Canvas myCanvas;
    Label lRed;
    TextField tfRed;
    TextField tfGreen;
    TextField tfBlue;
    Label lGreen;
    Label lBlue;

    Graphics g;

  public void init() {

    g = getGraphics();
    lRed = new Label("Red-value: ");
    lBlue = new Label("Blue-value: ");
    lGreen = new Label("Green-value: ");
    tfRed = new TextField();
    tfRed.setText("255");
    tfGreen = new TextField();
    tfGreen.setText("255");
    tfBlue = new TextField();
    tfBlue.setText("255");

    pGadgets = new Panel();
    pGadgets.setLayout(new GridLayout(1, 6, 5, 5));
    add(pGadgets);
    pGadgets.add(lRed);
    pGadgets.add(tfRed);
    pGadgets.add(lGreen);
    pGadgets.add(tfGreen);
    pGadgets.add(lBlue);
    pGadgets.add(tfBlue);

    myCanvas = new Canvas();
    myCanvas.setBackground(new Color(0,0,0));
    myCanvas.setBounds(0, 0, 600, 400);

    add(myCanvas);
    myCanvas.addMouseMotionListener(new MouseMotionListener() {

        public void mouseMoved(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        public void mouseDragged(MouseEvent e) {
            // TODO Auto-generated method stub
            paint(g);

        }
    });


  }

  publ开发者_StackOverflowic void paint(Graphics g) {
      g.setColor(Color.green);
      g.fillOval(mosX, mosY, 30, 30);

      }





}


Two things:

public void mouseDragged(MouseEvent e) 
{
   mosX = e.getX();
   mosY = e.getY();
   repaint(); //NOT paint(g);
}

//...
public void paint(Graphics g) 
{
    //draw on the canvas not on the component 
    //since the canvas gets drawn over it
    Graphics g2 = myCanvas.getGraphics();
    g2.setColor(Color.green);
    g2.fillOval(mosX, mosY, 30, 30);
}

If you have anything more complex, you should subclass Canvas and override it's paint() method.


Try this:

public void mouseDragged(MouseEvent e) 
{
    mosX = e.getX();
    mosY = e.getY();

    repaint();
}

The problem was that you were not updating your coordinates for the mouseDragged event. Also instead of calling paint() directly, you should call repaint(). Calling repaint() will ensure your paint() is called automatically.

EDIT:

To paint on Canvas object, override paint() of the Canvas object.

myCanvas = new Canvas()
{
    public void paint(Graphics g) 
    {
        g.setColor(Color.green);
        g.fillOval(mosX, mosY, 30, 30);
    }    
};

Now your mouseDragged method becomes :

public void mouseDragged(MouseEvent e) 
{
    mosX = e.getX();
    mosY = e.getY();

    myCanvas.repaint();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜