怎样用Java用鼠标点击 画圈
展开全部
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
/*
* @(#)Sanke.java.java Test 2012-2-29
* Copyright (c) 2010-2020 Easy ITs, Inc. All rights reserved.
*/
/**
*/
public class Sanke extends JFrame {
Cavas cavas = new Cavas();
public Sanke() {
this.setSize(500, 400);
this.setBackground(Color.black);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(cavas);
cavas.addMouseListener(cavas);
cavas.addMouseMotionListener(cavas);
this.setVisible(true);
}
public static void main(String[] args) {
new Sanke();
}
}
class Cavas extends JComponent implements MouseListener, MouseMotionListener {
private Point begin;
private Point end;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (begin != null && end != null) {
int left = Math.min(begin.x, end.x);
int top = Math.min(begin.y, end.y);
int width = Math.abs(begin.x - end.x);
int height = Math.abs(begin.y - end.y);
int r = Math.min(width, height);
// 画圆
g.drawOval(left, top, r,r);
// 画楕圆
// g.drawOval(left, top, width,height);
}
}
@Override
public void mouseDragged(MouseEvent e) {
end = e.getPoint();
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
begin = e.getPoint();
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
/*
* @(#)Sanke.java.java Test 2012-2-29
* Copyright (c) 2010-2020 Easy ITs, Inc. All rights reserved.
*/
/**
*/
public class Sanke extends JFrame {
Cavas cavas = new Cavas();
public Sanke() {
this.setSize(500, 400);
this.setBackground(Color.black);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(cavas);
cavas.addMouseListener(cavas);
cavas.addMouseMotionListener(cavas);
this.setVisible(true);
}
public static void main(String[] args) {
new Sanke();
}
}
class Cavas extends JComponent implements MouseListener, MouseMotionListener {
private Point begin;
private Point end;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (begin != null && end != null) {
int left = Math.min(begin.x, end.x);
int top = Math.min(begin.y, end.y);
int width = Math.abs(begin.x - end.x);
int height = Math.abs(begin.y - end.y);
int r = Math.min(width, height);
// 画圆
g.drawOval(left, top, r,r);
// 画楕圆
// g.drawOval(left, top, width,height);
}
}
@Override
public void mouseDragged(MouseEvent e) {
end = e.getPoint();
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
begin = e.getPoint();
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
追问
谢谢你 ,我的那个 是 像找茬一样,点击错误的地方,画一个圈,坐标是确定的 那个应该怎么写呀?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询