j_draw.java.bak

来自「一个十分好的java基础学习的课件」· BAK 代码 · 共 75 行

BAK
75
字号
// ////////////////////////////////////////////////////////
// 
// J_Draw.java
// 
// Created by Jun-Hai Yong,    on XX xx, xxxx (Date)
// ////////////////////////////////////////////////////////
// Readme:
//      Example of event-handling model.
// ////////////////////////////////////////////////////////
// Using this example, please explicitly refer to the book:
//     Jun-Hai Yong. Programming in Java. 
//     Beijing: Tsinghua University Press, 2004.
// 使用本例子,请注明引用:
//     雍俊海. Java 程序设计. 北京: 清华大学出版社, 2004.
// Please note that the author and publisher make no 
// warranty of any kind on the examples provided.
// ////////////////////////////////////////////////////////

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

public class J_Draw extends JFrame
{
    int     m_x= 0,  m_y= 0;
    int     m_oldX= 0,  m_oldY= 0;

    public J_Draw( )
    {
        super("Example of mouse event handling");
        addMouseListener( new MouseAdapter( )
            {
                public void mousePressed(MouseEvent e)
                {
                    m_x= e.getX( );
                    m_y= e.getY( );
                    m_oldX= m_x;
                    m_oldY= m_y;
                    repaint( );
                } // End of method: mousePressed
            } // End of the anonymous inner class, which implements MouseAdapter
        ); // End of invoking addMouseAdapter

        addMouseMotionListener( new MouseMotionAdapter( )
            {
                public void mouseDragged(MouseEvent e) 
                {
                    m_oldX= m_x;
                    m_oldY= m_y;
                    m_x= e.getX( );
                    m_y= e.getY( );
                    repaint( );
                } // End of method: mouseDragged
            } // End of the anonymous inner class, which implements MouseMotionAdapter
        ); // End of invoking addMouseMotionAdapter
        
        setSize(250, 150);
        setVisible(true);
    } // End of constructor: J_Draw

    public void paint(Graphics g)
    {
        g.drawLine(m_oldX, m_oldY, m_x, m_y);
    } // End of method: paint

    public static void main( String args[] )
    { 
        JFrame app = new J_Draw( );

        Container cp = app.getContentPane( );
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    } // End of method: main

} // End of class: J_Draw

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?