这是我的第一个GUI练习。我试图用for循环画一条线,但由于某种原因,我还没有弄清楚为什么我只得到它的最后一个点(像素)。我猜repaint()
做了一些和我想象的不同的事情,但是我还不能弄清楚它是什么。
package com.mycompany;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MousePanel extends JPanel implements MouseListener{
int pointX, pointY, oldX, oldY;
public MousePanel(){
super();
addMouseListener(this);
}
public void mouseClicked(MouseEvent mouse){
// Tell the panel that we need to redraw things.
oldX=pointX;
oldY=pointY;
// Get the location of the current mouse click.
pointX = mouse.getX();
pointY = mouse.getY();
// Tell the panel that we need to redraw things.
for (int i=0 ; i<50 ; i++)
{
pointX ++;
repaint();
}
System.out.println("x:"+pointX+", y:"+pointY);
}
public void paintComponent(Graphics g){
g.fillOval(pointX, pointY, 5, 5);
}
public void mouseEntered(MouseEvent mouse){ }
public void mouseExited(MouseEvent mouse){ }
public void mousePressed(MouseEvent mouse){ }
public void mouseReleased(MouseEvent mouse){ }
public static void main(String arg[]){
JFrame frame = new JFrame("MousePanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640,400);
MousePanel panel = new MousePanel();
frame.setContentPane(panel);
frame.setVisible(true);
}
}
Java代码:
for (int i=0 ; i<50 ; i++)
{
pointX ++;
repaint();
}
我只得到它的最后一个点(像素)。
这就是你在paintComponent()方法中绘制的所有内容。
如果你想要所有的椭圆,那么你需要在每次调用paintComponent()方法时重新绘制所有的椭圆。
请参阅自定义绘画方法,了解两种常见的方法:
- 在ArrayList中跟踪所有要绘制的对象,然后在每次调用paintComponent()时遍历该列表
- 绘制对象到BufferedImage,然后在paintComponent()方法中绘制BufferedImage。
重绘将始终绘制组件的固定最终状态。
并且,由于您在单个回调中请求重绘,因此您将得到以下
x=1
repaint
x=2
repaint
...
etc.
在完成循环之前,重绘本身不会发生,并且可以处理下一个UI事件(这是您的重绘请求)。这50个左右的重绘请求可能合并为一个,再次调用paintComponent。
现在油漆看到它应该画一个5px的椭圆形使用当前的x值,并这样做。
所以你可能会用一个重绘请求替换for循环,并将paintComponent更改为在oldX,oldY和pointX,pointY之间绘制
你重画了50次椭圆。我不太确定你想做什么。如果你想填充一个宽度/高度为50像素的椭圆,你可以更新方法paintComponent
public void paintComponent(Graphics g){
g.fillOval(pointX, pointY, 50, 50);
}
在这种情况下,不需要for循环,只需要重新绘制即可。
如果你想画一条线,有drawLine方法。我看到你存储了旧的像素位置所以你可以输入
public void paintComponent(Graphics g){
g.drawLine(pointX, pointY, oldX, oldY);
}
同样,只需要重新绘制,不需要循环。我把代码贴在下面
public class MousePanel extends JPanel implements MouseListener {
int pointX, pointY, oldX, oldY;
public MousePanel(){
super();
addMouseListener(this);
}
public void mouseClicked(MouseEvent mouse){
// Tell the panel that we need to redraw things.
oldX=pointX;
oldY=pointY;
// Get the location of the current mouse click.
pointX = mouse.getX();
pointY = mouse.getY();
// Tell the panel that we need to redraw things.
repaint();
System.out.println("x:"+pointX+", y:"+pointY);
}
public void paintComponent(Graphics g){
g.drawLine(pointX, pointY, oldX, oldY);
}
public void mouseEntered(MouseEvent mouse){ }
public void mouseExited(MouseEvent mouse){ }
public void mousePressed(MouseEvent mouse){ }
public void mouseReleased(MouseEvent mouse){ }
public static void main(String arg[]){
JFrame frame = new JFrame("MousePanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640,400);
MousePanel panel = new MousePanel();
frame.setContentPane(panel);
frame.setVisible(true);
}
}