用java编写程序,用鼠标移动圆圈

时间:2014-11-24 07:53:13

标签: java applet jframe

我已尝试在第一个Applet中编写它并且它工作但是我在drawOval行之后使用了repaint()方法并且它闪烁太多我无法看到..请帮我修复它。

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;


public class MouseEvents extends JFrame implements MouseListener, MouseMotionListener {

  JFrame frame;

  public static void main(String args[]){

  MouseEvents obj=new MouseEvents();

  obj.go();

}

public void go(){

  frame=new JFrame();

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  frame.setSize(300,300);

  frame.setVisible(true);

  addMouseListener(this);

  addMouseMotionListener(this);

}

public void mouseClicked(MouseEvent me) {

}

public void mouseEntered(MouseEvent me) {

}

public void mouseExited(MouseEvent me) {

}

public void mousePressed(MouseEvent me) {

}

public void mouseReleased(MouseEvent me) {

}

public void mouseDragged(MouseEvent me) {

  repaint();

}

public void mouseMoved(MouseEvent me) {

  Graphics g=getGraphics();

  g.drawOval(me.getX(), me.getY(), 30,30);

}

}

1 个答案:

答案 0 :(得分:0)

  1. 这不是applet,它是框架
  2. 了解如何在Swing中进行绘画(即不使用getGraphics)。有关详细信息,请参阅Painting in AWT and SwingPerforming Custom Painting
  3. 首先创建一个从JPanel扩展的类。为此,注册MouseMoitionListener以跟踪当前鼠标位置。覆盖它的paintComponent以绘制鼠标移动的圆圈......

    public class CirclePane extends JPanel {
    
        private Point p = new Point(100, 100);
    
        public CirclePane() {
            MouseAdapter mouseHandler = new MouseAdapter() {
    
                @Override
                public void mouseMoved(MouseEvent e) {
                    p = e.getPoint();
                    repaint();
                }
    
            };
            addMouseMotionListener(mouseHandler);
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.drawOval(p.x - 50, p.y - 50, 100, 100);
            g2d.dispose();
        }
    
    }
    

    将此添加到您的相框......

    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    
    public class MouseEvents {
    
        public static void main(String[] args) {
            new MouseEvents();
        }
    
        public MouseEvents() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new CirclePane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
    }