1

我对 java OO 有点陌生。使用 Java 的经典过程方面相当简单,包括事件处理甚至 Swing。但是我在处理对象时遇到了一些问题。我在 Eclipse 中编写了一个小程序,它使用事件处理来检测小程序查看器中的鼠标移动和点击。我什至通过鼠标点击在查看器中绘制一张脸。面部绘制代码是在程序中包含的方法中完成的,该方法在调用“drawFace()”方法时将绘制的面部作为图形对象“g”作为参数传递。'drawFace()' 方法返回一个 Graphics 类型并且程序运行良好!然后,我尝试在 Eclipse 中创建一个单独的类,以在单击鼠标时创建一个新的“drawFace”对象,就像前面的代码一样,并通过“new”实例化它。脸没有画出来。我确定我没有正确定义 Face 构造函数类或正确调用它。我首先使用方法('DrawFaceMethod')附加工作代码,使用主代码和第二个构造函数类('DrawFaceObject')附加非工作代码。两个代码都相当短且相似。任何帮助表示赞赏。

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class AppletDrawFaceMethod extends Applet implements MouseListener ,
MouseMotionListener{

    int mouseX = 0, mouseY = 30; // display area where coordinates of mouse displayed
    int width = 0, height = 0;   //  size of applet viewing area   
    String msg = "";
    boolean mouseclick = false;  //need to test for mouse click on TRUE
    Font font = new Font("Serif", Font.BOLD, 36);


    public void init() {
      width = getSize().width;   //get applet viewing size
      height = getSize().height;
      System.out.println("Applet dimensions: " + width + " , " + height);     
     //addMouseListeners
      addMouseListener(this);
      addMouseMotionListener(this);
    } // end init()


    // paint method executed on every event
    public void paint(Graphics g) {
        //need to associate font with paint method
        g.setFont(font);

        //test mouseclick and set boolean
        if (mouseclick == true){
        //calls method 'drawFace', passes mouseclick x,y coordinates
        // and Graphics object 'g'
        //the 'drawFace' method returns a Graphic object of a face drawn
            drawFace(mouseX, mouseY, g);
            msg = "Face drawn at mouse click " + mouseX + ", " + mouseY;
            //re-initialize variables 
            mouseclick = false;
            mouseX = 0;
            mouseY = 30;

        } // end of 'if mouseclick == true' face drawing code

        g.drawString(msg, mouseX, mouseY);

    } // end of paint method    

    // Following methods are mouse 'Listener' methods to detect mouse events  

    // Handle mouse moved.
    public void mouseMoved(MouseEvent me) {
        // show status
        msg = "Moving mouse at " + me.getX() + ", " + me.getY();
        //showStatus("Moving mouse at " + me.getX() + ", " + me.getY());
        repaint();
    }

    // Handle mouse clicked.
    public void mouseClicked(MouseEvent me) {
        mouseclick = true;  //set boolean 'mouseclick' to TRUE  
        //save coordinates
        mouseX = me.getX();
        mouseY = me.getY();
        repaint();
    }

    // Handle mouse entered.
    public void mouseEntered(MouseEvent me) {
        // save coordinates
        //mouseX = 0;
        //mouseY = 30;
        msg = "Mouse entered.";
        repaint();
    }

    // Handle mouse exited.
    public void mouseExited(MouseEvent me) {
        // save coordinates
        //mouseX = 0;
        //mouseY = 30;
        msg = "Mouse exited applet viewer.";
        repaint();
    }

    //Other method mouse event methods required even if not used
    // Handle button pressed.
    public void mousePressed(MouseEvent me) {}
    // Handle button released.
    public void mouseReleased(MouseEvent me) {}
    // Handle mouse dragged.
    public void mouseDragged(MouseEvent me) {}

    ///////////////////////////////////////////////////////////
    //
    // 'drawFace method' receive a Graphics object parameter g' 
    //  it must return a Graphics object which the method draws 
    //
    ///////////////////////////////////////////////////////////
    public Graphics drawFace(int xStart,int yStart, Graphics g)  {

        g.drawOval (mouseX, mouseY, 120, 150);      //  Head.   
        g.drawOval ((mouseX + 45), (mouseY + 60), 30, 30); //  nose
            g.fillArc((mouseX + 20), (mouseY + 85), 80, 40, 180, 180); //mouth.
        g.drawOval ((mouseX + 17),(mouseY + 35), 30, 20);  //Left eye.
        g.drawOval ((mouseX + 70),(mouseY + 35), 30, 20);  //Right eye.
            g.fillOval ((mouseX + 28), (mouseY + 41), 10, 10); //Left pupil.
            g.fillOval ((mouseX + 81), (mouseY + 41), 10, 10); //Right pupil.
            g.drawOval ((mouseX - 15), (mouseY + 52), 15, 30); //Left ear. 
            g.drawOval ((mouseX + 120), (mouseY + 52), 15, 30); //Right ear. 
            g.drawArc ((mouseX + 15), (mouseY + 25),35,15,0,180);//Left brow.
            g.drawArc ((mouseX + 67), (mouseY + 25), 35, 15, 0, 180);//Right.

        return g;   //returns pointer to drawn graphics face

    } //end of drawFace method

}   // End of class


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//
//
//   'drawFaceObject' Applet code , followed by 'drawFace' constructor class 
//
//  
//////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////



import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class DrawFaceObject extends Applet implements MouseListener ,
MouseMotionListener{

    int mouseX = 0, mouseY = 30; // display mouse coordinates
    int width = 0, height = 0;   //  size of applet viewing area   
    String msg = "";
    boolean mouseclick = false;  //need to test for mouse click on TRUE
    Font font = new Font("Serif", Font.BOLD, 36);


    public void init() {
        width = getSize().width;   //get applet viewing size
        height = getSize().height;
        System.out.println("Applet dimensions: " + width + " , " + height);   

        //addMouseListeners
        addMouseListener(this);
        addMouseMotionListener(this);
    } // end init()


    // paint method executed on every event
    public void paint(Graphics g) {
        //need to associate font with paint method
        g.setFont(font);

                //test mouseclick and set boolean
        if (mouseclick == true){
            //creates object'drawFace', passes mouseclick x,y coordinates
            // and Graphics object 'g'

            Class_DrawFace face = new Class_DrawFace(g);

            msg = "Face drawn at mouse click " + mouseX + ", " + mouseY;
            //re-initialize variables 
            mouseclick = false;
            mouseX = 0;
        mouseY = 30;

        } // end of 'if mouseclick == true' face drawing code

    g.drawString(msg, mouseX, mouseY);

    } // end of paint method    


    // Following are mouse 'Listener' methods to detect mouse events  


    // Handle mouse moved.
    public void mouseMoved(MouseEvent me) {
        // show status
        msg = "Moving mouse at " + me.getX() + ", " + me.getY();
        //showStatus("Moving mouse at " + me.getX() + ", " + me.getY());
        repaint();
    }

    // Handle mouse clicked.
    public void mouseClicked(MouseEvent me) {
        mouseclick = true;  //set boolean 'mouseclick' to TRUE  
        //save coordinates
        mouseX = me.getX();
        mouseY = me.getY();
        repaint();
    }

    // Handle mouse entered.
    public void mouseEntered(MouseEvent me) {
        // save coordinates
        //mouseX = 0;
        //mouseY = 30;
        msg = "Mouse entered.";
        repaint();
    }

    // Handle mouse exited.
    public void mouseExited(MouseEvent me) {
        // save coordinates
        //mouseX = 0;
        //mouseY = 30;
        msg = "Mouse exited applet viewer.";
        repaint();
    }

    //Other method mouse event methods required even if not used
    // Handle button pressed.
    public void mousePressed(MouseEvent me) {}
    // Handle button released.
    public void mouseReleased(MouseEvent me) {}
    // Handle mouse dragged.
    public void mouseDragged(MouseEvent me) {}


}   // End of class



/////////////////////////////////////////////////////////////
//
// 'drawFace' Constructor
////////////////////////////////////////////////////////////

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Class_DrawFace extends Applet{     
//instance variables
     public Graphics g;
     public int x, y;

Class_DrawFace(Graphics g) { 
    int x = 0;
    int y = 0;
}
Class_DrawFace(int mouseX, int mouseY, Graphics g) { 
    int x = mouseX;
    int y = mouseY;
}

 public Graphics drawFace(int x, int y, Graphics g) {
    g.drawOval (x, y, 120, 150);  //  Head. 
    g.drawOval ((x + 45), (y + 60), 30, 30); //  nose
        g.fillArc ((x + 20), (y + 85),80,40,180, 180);//mouth.
    g.drawOval ((x + 17),(y + 35), 30, 20);//Left eye.
    g.drawOval ((x + 70),(y + 35), 30, 20);//Right eye.
        g.fillOval ((x + 28), (y + 41),10,10);//Left pupil.
        g.fillOval ((x + 81), (y + 41),10,10);//Right pupil.
        g.drawOval ((x - 15), (y + 52), 15, 30);//Left ear. 
        g.drawOval ((x + 120), (y + 52), 15, 30);//Right ear. 
        g.drawArc ((x + 15), (y + 25),35,15,0,180);//Left eyebrow.
        g.drawArc ((x + 67), (y + 25),35,15,0,180);//Right eyebrow.

    return g;   //return drawnFace Graphics object 

} //end of drawFace class constructor

}//end of class 
4

0 回答 0