Java创建图形对象,用于从工作方法中绘制复杂图形

时间:2013-05-09 19:19:19

标签: java object graphics methods

我对java OO有些新意。使用Java的经典过程方面相当简单,包括处理甚至Swing。但我在使用对象时遇到了一些麻烦。我在Eclipset中编写了一个小程序,它使用事件处理来检测applet查看器中的鼠标移动和点击。我甚至在鼠标点击时在观察者中画了一张脸。面部绘制代码在程序中包含的方法中完成,该方法在调用'drawFace()'方法时将绘制的面返回为作为参数传递的Graphics对象'g'。 'drawFace()'方法返回一个Graphics类型,程序运行正常!然后我尝试在Eclipse中创建一个单独的类,以便在鼠标单击时创建一个新的'drawFace'对象,就像前面的代码一样,并通过'new'实例化它。面子 没画。我确信我没有正确定义Face构造函数类或正确调用它。我首先使用方法('DrawFaceMethod')附加工作代码,使用主代码和第二个构造函数类将非工作代码附加到face('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 

0 个答案:

没有答案