gpt4 book ai didi

java - PaintComponent() 方法未通过 mouseMoved() 调用

转载 作者:行者123 更新时间:2023-12-01 08:53:57 25 4
gpt4 key购买 nike

我正在尝试编写一个程序,允许用户通过单击窗口中的 JButton,然后单击窗口中的某个位置(而不是按钮上)来创建正方形或圆形。

我目前正在为不同的按钮使用两个actionListener,这些按钮将方形或圆形添加到形状类型的数组列表(方形和圆形都实现的接口(interface))。我有一个名为frameListener的类,它实现了MouseListener和MouseMotionListener,当鼠标移动时更新正方形或圆形的位置,如果鼠标离开窗口则删除正方形或圆形,或者当单击鼠标时停止更新它(使得它是永久的)。

我还有一个名为 MyPanel 的类,它扩展了 JPanel,并使用 PaintComponent(Graphics g) 方法在背景上绘制正方形和圆形。

package smys01;

import java.util.ArrayList;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;




public class SMYS01 extends JFrame {

private Color c = new Color(0, 0, 0);
private static ArrayList<Shape> shapes = new ArrayList();
private static ArrayList<Shape> deleted = new ArrayList();

private Point a;
private boolean makeSquare = false, makeCircle = false, makeSquiggle = false;

public static void main(String[] args) {
SMYS01 window = new SMYS01();

}
//good main

//0 is empty for making things, 1 is having a square selected to change, 2 is background;

private JButton makeSquareB = new JButton("New Square" /*, add icon later*/);

private JButton makeCircleB = new JButton("New Circle");

private Color background = new Color(0, 150, 0);



public SMYS01() {
makeSquareB.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("making square");
if(!makeCircle&&!makeSquare){
makeSquare = true;

shapes.ensureCapacity(shapes.size());
shapes.add(new Square((int) a.getX(), (int) a.getY(), c));
}else if(makeSquare){

}else if(makeCircle){
makeCircle=false;
makeSquare=true;
shapes.remove(shapes.size()-1);
shapes.ensureCapacity(shapes.size());
shapes.add(new Square((int) a.getX(), (int) a.getY(), c));
}
}
});


makeCircleB.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("making circle");
if(!makeCircle&&!makeSquare){
makeCircle = true;
shapes.ensureCapacity(shapes.size());
shapes.add(new Circle((int) a.getX(), (int) a.getY(), c));
}else if(makeCircle){

}else if(makeSquare){
makeSquare=false;
shapes.remove(shapes.size()-1);
shapes.ensureCapacity(shapes.size());
shapes.add(new Circle((int) a.getX(), (int) a.getY(), c));
}
}
});
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane();
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
MyPanel thing = new MyPanel();


frame.addMouseListener(new FrameListener());
frame.setContentPane(thing);

frame.setSize(thing.getPreferredSize());

frame.setTitle("Art!");

frame.addMouseMotionListener(new FrameListener());
}


private class MyPanel extends JPanel {

public MyPanel() {

GroupLayout layout = new GroupLayout(this);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addComponent(makeCircleB)
.addComponent(makeSquareB)
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(makeCircleB)
.addComponent(makeSquareB)
)
);

}

@Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(background);
g.drawRect((int)g.getClipBounds().getX(),(int)g.getClipBounds().getY(),(int)g.getClipBounds().getWidth(),(int)g.getClipBounds().getHeight());
g.fillRect((int)g.getClipBounds().getX(),(int)g.getClipBounds().getY(),(int)g.getClipBounds().getWidth(),(int)g.getClipBounds().getHeight());


System.out.println(shapes.size());
for (Shape i : shapes) {

int[] a = i.getDim();
g.setColor(new Color(a[4], a[5], a[6], a[7]));

if(i instanceof Square){
g.drawRect(a[0], a[1], a[2], a[3]);
g.fillRect(a[0], a[1], a[2], a[3]);
}else if(i instanceof Circle){
g.drawOval(a[0], a[1], a[2], a[3]);
g.fillOval(a[0], a[1], a[2], a[3]);
}

}

}
}


class FrameListener implements MouseListener, MouseMotionListener {


public void mousePressed(MouseEvent e) {
System.out.println("pressed");
a = new Point(e.getPoint());
if (makeSquare) {
makeSquare = false;
System.out.println("square made!");
}
if (makeCircle) {
makeCircle = false;
System.out.println("cirlce made!");
}

//use this for sliders, nothing else.

}

public void mouseReleased(MouseEvent e) {
System.out.println("released");
a = new Point(e.getPoint());
//use for sliders, nothing else.

}

public void mouseClicked(MouseEvent e) {
System.out.println("clicked");
a = new Point(e.getPoint());
//don't need this
//makeSquare = false;
//makeCircle = false;

//makeSquiggle too.
}

public void mouseMoved(MouseEvent e) {

//showing up as false atm
a = new Point(e.getPoint());
System.out.println(a);
if (makeSquare||makeCircle) {
Shape b = shapes.get(shapes.size()-1);
b.updateLocation((int) a.getX(), (int) a.getY());

}


}

public void mouseDragged(MouseEvent e) {
//only check if over a JSlider
}

//complete methods:
public void mouseEntered(MouseEvent e) {
System.out.println("entered");
//nothing, Mouse re-entering doesn't effect the program.
}

public void mouseExited(MouseEvent e) {
System.out.println("exited");
makeSquare = false;
makeCircle = false;
makeSquiggle = false;
//if mouse leaves the window while trying to make one of these, stop it.

}
}



}

Shape 接口(interface)的代码:

package smys01;

public interface Shape {
public void updateLocation(int newXPos, int newYPos);
public void updateDimensions(int newXDim, int newYDim);
public void delete();
public void purge();
public void restore();
public int[] getDim();
}

广场代码:笔记!这段代码写得有点晚,有些部分根本不相关,甚至不完整(但完全未使用)。这是针对构造函数、updateDimensions() 方法和 updateLocation() 方法

package smys01;
import java.awt.Color;
public class Square implements Shape{
//tbr=to be returned.
boolean deleted=false;
boolean purged=false;
private int xPos, yPos, xDim, yDim;
private int saveXPos, saveYPos, saveXDim, saveYDim, saveR, saveG, saveB, saveA;
private Color fill;
public Square(int initX, int initY, Color a){
xPos=initX;
yPos=initY;
xDim=50;
yDim=50;
fill=a;
}
public void updateDimensions(int newXDim, int newYDim){
if(purged||deleted)
return;
if(newXDim<=2){
xDim=2;
}else{
xDim=newXDim;
}
if(newYDim<=2){
yDim=2;
}else{
yDim=newYDim;
}

}
public void updateLocation(int newXPos, int newYPos){
if(purged){
return;
}
if(newXPos<=5){
xPos=5;


}else{
xPos=newXPos;

}
if(newYPos<=25){
yPos=25;
}else{
yPos=newYPos;
}


}
public void delete(){
if(!deleted){
saveXPos=xPos;
saveYPos=yPos;
saveXDim=xDim;
saveYDim=yDim;
saveR=fill.getRed();
saveB=fill.getBlue();
saveG=fill.getGreen();
saveA=fill.getAlpha();
deleted=true;
xPos=-5;
yPos=-5;
xDim=1;
yDim=1;
}
}
public void purge(){
saveXPos=-10;
saveYPos=-10;
saveXDim=3;
saveYDim=3;
//use in an if statement, and if true (will be, set the square value to null, so this reference will go away, preventing data overflow
}
public void restore(){
xPos=saveXPos;
yPos=saveYPos;
xDim=saveXDim;
yDim=saveYDim;
deleted=false;
}
public int[] getDim(){
int[] tbr={xPos,yPos, xDim, yDim,fill.getRed(), fill.getBlue(), fill.getGreen(), fill.getAlpha()};
return tbr;
}
public String toString(){
String tbr="Square: ("+xPos+", "+yPos+"); ("+xDim+", "+yDim+"); ("+fill.toString();
return tbr;
}

}

Circle 类的代码完全相同,但对象类型允许 PaintComponent 方法区分它们。

我试图在调用 mouseMoved 时让它调用paintComponent,但是我似乎无法让它执行此操作。我尝试过使用 repaint(),并且我花了几个小时试图解决这个问题。

到目前为止,我已经看到,基本上,Swing 会在需要时调用 PaintComponent(当窗口大小更改时,或者窗口最小化并重新打开时)。

但是,我不确定如何在按下按钮或移动鼠标时调用它,并且 repaint() 对此不起作用。

如果有些代码马虎,我深表歉意,我重写了几次,有些部分是不必要的,但仍然存在。

有没有办法从匿名操作类或 FrameListener 类中主动调用 PaintComponent 方法?

最佳答案

经过 @MadProgrammer 的一些输入,我发现了什么不起作用。 mouseListener 附加到 JFrame 而不是 JPanel,因此事件被触发,但 PaintComponent(通过 repaint())没有被触发。我将其添加到面板中,并将 repaint() 添加到 mouseMoved() 方法的末尾,并且它运行良好。

关于java - PaintComponent() 方法未通过 mouseMoved() 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42181974/

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