gpt4 book ai didi

java - 使用 MouseListener 和 MouseMotionListener 绘制矩形

转载 作者:行者123 更新时间:2023-12-01 05:47:57 26 4
gpt4 key购买 nike

我创建了两个包:Displaypaint_core,其中包含五个 Java 文件:Displayable.javaDrawing。 javaNewColutes.javaOldCitudes.javaMain.java

我面临一些坐标问题,并且输出未按预期输出。我的程序出了什么问题?

Main.java

package paint_core;

import Display.Displayable;

public class Main {

public Main() {
}

public static void main(String[] args) {
new Displayable();
}

}

Displayable.java

package Display;

import java.awt.Color;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;


public class Displayable extends JFrame implements MouseListener,MouseMotionListener{

Drawing dr;
int x,y;
OldCordinates op;

public Displayable()
{
setVisible(true);
dr = new Drawing();

add(dr);
dr.addMouseListener(this);
dr.addMouseMotionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void mouseDragged(MouseEvent e)
{

NewCordinates np = new NewCordinates();

np.setX(x);
np.setY(y);
np.setW(e.getX()-x);
np.setH(e.getY()-y);

op= dr.setXY(np.getX(),np.getY(),np.getW(),np.getH(),Color.RED,op);
}

public void mouseMoved(MouseEvent e)
{

}

public void mouseClicked(MouseEvent e)
{

}

public void mousePressed(MouseEvent e) {
x=e.getX();
y=e.getY();
op = new OldCordinates();
op.setX(x);
op.setY(y);
op.setW(x);
op.setH(y);
}

public void mouseReleased(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

}

Drawing.java

package Display;

import java.awt.*;

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.JPanel;

public class Drawing extends JPanel

{

private int x,y,w,h;
Color r;
OldCordinates op;

public OldCordinates setXY(int x,int y,int w,int h,Color r,OldCordinates op)

{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.r = r;
this.op = op;
//System.out.println("Old Cordinates" + op.getW()+" "+op.getY());
repaint();

op.setH(h);
op.setW(w);
//System.out.println("New Cordinates" + w+" "+h);

return op;

}
@Override
public void paint(Graphics g) {

g.drawRect(op.getX(),op.getY(),op.getW(),op.getH());
g.setColor(r);
g.drawRect(x,y,w,h);

}

}

旧坐标.java

package Display;

import java.awt.*;

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.JPanel;

public class Drawing extends JPanel
{
private int x,y,w,h;
Color r;
OldCordinates op;

public OldCordinates setXY(int x,int y,int w,int h,Color r,OldCordinates op)

{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.r = r;
this.op = op;
//System.out.println("Old Cordinates" + op.getW()+" "+op.getY());
repaint();

op.setH(h);
op.setW(w);
//System.out.println("New Cordinates" + w+" "+h);

return op;

}
@Override

public void paint(Graphics g) {

//g.drawRect(op.getX(),op.getY(),op.getW(),op.getH());
g.setColor(r);
g.drawRect(x,y,w,h);

}

}

NewColutes.java

package Display;


public class NewCordinates {
private int x;
private int y;
private int w;
private int h;

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public int getW() {
return w;
}

public void setW(int w) {
this.w = w;
}

public int getH() {
return h;
}

public void setH(int h) {
this.h = h;
}


}

最佳答案

您应该重写paintComponent而不是paint。如果这不是您的问题,请提供更多详细信息

关于java - 使用 MouseListener 和 MouseMotionListener 绘制矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5473958/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com