JPanel 未按预期更新
每次单击我的应用程序上的图像时,我都希望界面右侧(底部)的面板刷新该特定位置的 RGB 值。
- 我知道它正在获取值,正如您在控制台中看到的那样,它打印了像素 1 和 2 的值和位置。
单击鼠标时,它会执行以下操作:
image_Display.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
x1 = x2;
y1 = y2;
colour1 = colour2;
x2 = e.getX();
y2 = e.getY();
colour2 = originalpixelValue(x2, y2);
//RGBValue pixelValue = originalpixelValue(x2, y2);
System.out.println(colour2.toString());
String pixel1 = x2 + ", " + y2;
//String pixel2 = previousX + ", " + previousY;
System.out.println("Pixel 1: " + pixel1);
System.out.println("\n");
Toolbar.repaint();
repaint();
}
}
涂装方法:
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(3));
Random r = new Random();
g2.setColor(new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)));
g2.drawOval(x1, y1, 10, 10);
g2.drawOval(x2, y2, 10, 10);
}
预期结果:一切正常,我只是在努力将 RGB 值打印到用户界面。我试过使用Repaint()
and Revalidate()
。
任何帮助将不胜感激