Java Source - Drawing a Face v2


Application which simply displays Ms Paclady.
Một ứng dụng đơn giản thể hiện Ms Paclady (à nhân vậy trong game Pacman đó mờ ^^!).




Main Application.

// File   : GUI-lowlevel/face2/Face2.java - Example app with graphics component.
// Description: Application which simply displays Ms Paclady.
// Illustrates: 1. Basic JFrame subclass style of creating window.
//              2. Subclassing JComponent and overriding JComponent to
//                 do graphics.
// Author : Fred Swartz - 2007-01-20 - Placed in public domain.

import java.awt.*;
import javax.swing.*;

///////////////////////////////////////////////////////////////////////// Face
public class Face2 extends JFrame {
    // ================================================================== main
    public static void main(String[] args) {
        Face2 window = new Face2();
        window.setVisible(true);
    }
   
    // ============================================================ constructor
    public Face2() {
        JPanel content = new JPanel();              // Create content panel.
        content.setLayout(new BorderLayout());
       
        PacLady drawing = new PacLady();            // Create a PacLady
        content.add(drawing, BorderLayout.CENTER);  // Put in expandable center.
       
        this.setContentPane(content);
        this.setTitle("Ms Paclady");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();                                // Finalize window layout
        this.setLocationRelativeTo(null);           // Center window on screen.
    }
}


Class draw Ms Paclady.

// File   : PacLady.java
// Purpose: Graphics component to display Ms Paclady.
// Author : Fred Swartz - 1996 ... 2006-12-17 - Placed in public domain.

import java.awt.*;
import javax.swing.*;

///////////////////////////////////////////////////////// class PacLady
    // This class extends JComponent and overrides paintComponent to
    // create a component for drawing - in this case a face.
    // It doesn't do anything useful.

public class PacLady extends JComponent {
    //====================================================== constants
    private static final int BORDER = 8;  // Border in pixels.
   
    //===================================================== constructor
    PacLady() {
        this.setPreferredSize(new Dimension(400, 400));  // size
    }
   
    //================================================== paintComponent
    @Override public void paintComponent(Graphics g) {
        int w = getWidth();
        int h = getHeight();
       
        //... Draw background.
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(0, 0, w, h);
       
        //... Draw the head with open mouth and 8 pixel border
        g.setColor(Color.PINK);
        g.fillArc(BORDER, BORDER, w - 2*BORDER, h - 2*BORDER, 30, 300); 
       
        //... Draw the eye
        g.setColor(Color.MAGENTA);
        int eyeSize = w / 7;
        g.fillOval(w / 2, h / 5, eyeSize, eyeSize);
    }
}


Kết quả - Result




Source: leepoint







No comments:

Post a Comment