1

私はJava OOに少し慣れていません。イベント処理や Swing など、Java の従来の手続き型の側面を使用するのはかなり簡単です。しかし、オブジェクトに問題があります。イベント処理を使用してマウスの動きとアプレット ビューアーでのクリックを検出する小さなプログラムを Eclipse で作成しました。マウスをクリックするだけでビューアーに顔を描くことさえできます。顔描画コードは、プログラムに含まれるメソッドで実行されます。このメソッドは、描画された顔を、「drawFace()」メソッドを呼び出すときにパラメーターとして渡される Graphics オブジェクト「g」として返します。「drawFace()」メソッドは Graphics 型を返し、プログラムは正常に動作します! 次に、前のコードと同様に、Eclipse で別のクラスを作成して、マウス クリックがあったときに新しい「drawFace」オブジェクトを作成し、「new」を介してインスタンス化します。顔は描いていません。Face コンストラクター クラスを正しく定義していないか、正しく呼び出していないことは確かです。最初にメソッド ('DrawFaceMethod') を使用して動作するコードをアタッチし、メイン コードと 2 番目のコンストラクター クラスを使用して顔に描画する非動作コード ('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