[英]Trouble Drawing Circle Centred On Mouse Click
我正在嘗試編寫一個程序,該程序將在鼠標單擊時繪制一個圓,並且該圓的中心位於鼠標單擊的坐標處。 我的圓圈將是50 x50。在我的鼠標偵聽器類中,我將x-25和y-25偏移,但是,該圓圈不會以我的點擊為中心。 它顯示在點擊下方。
public class CircleDraw extends JPanel
private JFrame frame;
private JPanel P1;
private int circlecount;
private int x, y;
MouseClicks minney;
ArrayList<Circle> circles = new ArrayList<Circle>();
public CircleDraw()
{
frame = new JFrame("CircleDraw");
frame.setSize(800,800);
minney = new MouseClicks();
//circles.add(new Circle(x,y));//This may be the original circle being added
this.setBackground(Color.BLACK);
this.setPreferredSize(new Dimension(800,800));
frame.add(this);
frame.pack();
frame.setVisible(true);
frame.addMouseListener(minney);
}
public class Circle
{
int x, y;
public Circle(int x, int y)
{
this.x = x; this.y = y;
}
public void draw(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.GREEN);
g2d.fillOval(x,y,50,50);
}
}
@Override
protected void paintComponent(Graphics g)
{
{
super.paintComponent(g);
for (Circle c : circles)
c.draw(g);
}
}
public class MouseClicks implements MouseListener
{
int x, y, b;
public void mouseClicked(MouseEvent m)
{
int x = m.getX(), y = m.getY(); b = m.getButton();
this.x = x;
this.y = y;
{
circles.add(new Circle(x-25, y-25));
CircleDraw.this.repaint();
}
}
public void mouseEntered(MouseEvent m) {}
public void mouseExited(MouseEvent m) {}
public void mousePressed(MouseEvent m) {}
public void mouseReleased(MouseEvent m) {}
}
}
您正在將MouseListener添加到錯誤的組件。 這里:
frame.addMouseListener(minney);
你將它添加到JFrame,這將轉化鼠標通過Jframe的標題欄的距離坐標下來,當你需要將它添加到,實際上是在做拉伸的組成部分, this
讓鼠標按下坐標匹配圖紙坐標:
addMouseListener(minney);
或明確地
this.addMouseListener(minney);
例如,與您類似的帶有可拖動圓圈的代碼:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
@SuppressWarnings("serial")
public class CircleDraw2 extends JPanel {
private static final int CIRCLE_RADIUS = 25;
private static final Color BACKGROUND = Color.BLACK;
private int prefW;
private int prefH;
List<Circle2> circles = new ArrayList<>();
public CircleDraw2(int prefW, int prefH) {
this.prefW = prefW;
this.prefH = prefH;
setBackground(BACKGROUND);
MyMouse myMouse = new MyMouse();
addMouseListener(myMouse);
addMouseMotionListener(myMouse);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(prefW, prefH);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for (Circle2 circle : circles) {
circle.draw(g2);
}
}
private class MyMouse extends MouseAdapter {
private Point p0 = null;
private Circle2 selection = null;
@Override
public void mousePressed(MouseEvent e) {
for (int i = circles.size() - 1; i >= 0; i--) {
if (circles.get(i).contains(e.getPoint())) {
// if mouse clicks on a circle that already exists
p0 = e.getPoint();
selection = circles.get(i); // select that pressed circle
// move it to the top
circles.remove(selection);
circles.add(selection);
repaint();
return;
}
}
// no circle selected, so create a new one
int centerX = e.getX();
int centerY = e.getY();
Color c = createRandomColor();
selection = new Circle2(centerX, centerY, CIRCLE_RADIUS, c);
circles.add(selection);
p0 = e.getPoint();
repaint();
}
private Color createRandomColor() {
double minRand = 0.7; // min saturation and brightness
float hue = (float) Math.random();
float saturation = (float)((1 - minRand) * Math.random() + minRand);
float brightness = (float)((1 - minRand) * Math.random() + minRand);
Color c = Color.getHSBColor(hue, saturation, brightness);
return c;
}
private void drag(Point p1) {
if (p0 == null) {
return;
}
selection.move(p0, p1);
p0 = p1;
repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
drag(e.getPoint());
}
@Override
public void mouseReleased(MouseEvent e) {
drag(e.getPoint());
p0 = null;
selection = null;
}
}
private static void createAndShowGui() {
int width = 800;
int height = width;
CircleDraw2 mainPanel = new CircleDraw2(width, height);
JFrame frame = new JFrame("CircleDraw2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
class Circle2 {
private Color color;
private Path2D path; // easily moveable with affine transform
public Circle2(int centerX, int centerY, int radius, Color color) {
this.color = color;
double x = centerX - radius;
double y = centerY - radius;
double w = 2 * radius;
double h = w;
Ellipse2D e2d = new Ellipse2D.Double(x, y, w, h);
path = new Path2D.Double(e2d);
}
public void move(Point p0, Point p1) {
// get the distance or "delta" of the move
int deltaX = p1.x - p0.x;
int deltaY = p1.y - p0.y;
// move the circle delta amount
path.transform(AffineTransform.getTranslateInstance(deltaX, deltaY));
}
public void draw(Graphics2D g2) {
g2.setColor(color);
g2.fill(path);
g2.setColor(Color.BLACK);
g2.draw(path);
}
public int getCenterX() {
return (int) path.getBounds().getCenterX();
}
public int getCenterY() {
return (int) path.getBounds().getCenterY();
}
public int getRadius() {
return (int) path.getBounds().getWidth() / 2;
}
public Color getColor() {
return color;
}
public Shape getEllipse() {
return path;
}
public boolean contains(Point p) {
return path.contains(p);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.