English 中文(简体)
java在 mo点击时 drawing一个圈子
原标题:java drawing a circle when mouse clicked

i 撰写节目,在点燃时,将进行圈子。 The following Code i ve have contributed to this summary.

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.event.*;
import java.awt.geom.*;

public class test extends JFrame implements ActionListener, MouseListener {
    Shape circle = new Ellipse2D.Float(10, 10, 10, 10);

    public test () {
        setSize(250,150);
        addMouseListener(this);
    }

    public static void main(String[] args) {
        //TODO code application logic here
        java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                   test frame = new test();
                   frame.setVisible(true);
              }
        });
    }

    public void actionPerformed(ActionEvent ae) {

    }

    public void drawCircle(int x, int y) {
        Graphics g = this.getGraphics();
        g.drawOval(x, y, x, y);
        g.setColor(Color.BLACK);
        g.fillOval(x, y, 2, 2);
    }

    public void mouseClicked(MouseEvent e) {
        drawCircle(e.getX(), e.getY());
        repaint();
    }

    public void mouseExited(MouseEvent e) {

    }

    public void mousePressed(MouseEvent e) {

    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {

    }
}

守则是400X400格框架,在半秒点点点击露圈时,问题在于,一旦释放 mo,圈就会消失。 为什么?

最佳回答

更改<代码>mouseClick(......)至:

int x, y;

public void mouseClicked(MouseEvent e) {
    x = e.getX();
    y = e.getY();

    repaint();
}

<paint(...):

@Override
public void paint(Graphics g) {
    drawCircle(x, y);
}
问题回答

当你打电话repaint()时,该部件重新油漆,从。 你们的圈子消失了。 您将想凌驾于<代码>paintComponent(Graphics),每当该部件被涂上时,就将其称作该编码。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...