Java 如何在鼠标点击时调用 repaint()
Java How to call repaint() on mouse click
所以,我的代码基本上是这样的:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test extends JPanel{
boolean circle = false;
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
if(circle){
g.drawRect(150,150,100,100);
}
}
public static void main(String[] args){
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(400,400);
Test test = new Test();
f.add(test);
}
}
我希望当我单击鼠标时,circle
变量发生变化并调用 repaint()
。
我该怎么做?谢谢
JPanel.addMouseListener就可以实现了。并注意 final
关键字在 jdk8 中是可选的,当内部 class.
中的变量访问时
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test extends JPanel {
boolean circle = false;
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
if (circle) {
g.drawRect(150, 150, 100, 100);
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(400, 400);
final Test test = new Test();
test.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
test.circle = true;
test.repaint();
}
});
f.add(test);
}
}
这是我曾经写过的一个小EventHandler...
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class EventHandler implements KeyListener,MouseListener,MouseWheelListener {
public JFrame pane;
public int mouse_x;
public int mouse_y;
public int[] mouse=new int[5];
public int[] keys=new int[256];
@Override
public void mousePressed(MouseEvent m) {
//mouse[Math.abs(Math.min(3,m.getButton()-1))]=1;
switch (m.getButton()) {
case (MouseEvent.BUTTON1) :
mouse[2]=1;
case (MouseEvent.BUTTON2) :
mouse[3]=1;
case (MouseEvent.BUTTON3) :
mouse[4]=1;
}
}
@Override
public void mouseClicked(MouseEvent m) {
//mouse[Math.abs(Math.min(3,m.getButton()-1))]=2;
switch (m.getButton()) {
case (MouseEvent.BUTTON1) :
mouse[2]=2;
case (MouseEvent.BUTTON2) :
mouse[3]=2;
case (MouseEvent.BUTTON3) :
mouse[4]=2;
}
}
@Override
public void mouseReleased(MouseEvent m) {
//mouse[Math.abs(Math.min(3,m.getButton()-1))]=3;
switch (m.getButton()) {
case (MouseEvent.BUTTON1) :
mouse[2]=3;
case (MouseEvent.BUTTON2) :
mouse[3]=3;
case (MouseEvent.BUTTON3) :
mouse[4]=3;
}
}
@Override
public void mouseEntered(MouseEvent m) {
}
@Override
public void mouseExited(MouseEvent m) {
}
@Override
public void keyTyped(KeyEvent e) {
keys[e.getKeyCode()]=2;
}
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()]=3;
}
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()]=1;
}
public void updateKeys() {
for (int i=0; i < keys.length; i++) {
if (keys[i]==2 || keys[i]==3) {
keys[i]=0;
}
}
}
public void getMouse() {
Component[] rel;
rel = pane.getComponents();
mouse_x=mouse[0]=MouseInfo.getPointerInfo().getLocation().x-rel[0].getLocationOnScreen().x;
mouse_y=mouse[1]=MouseInfo.getPointerInfo().getLocation().y-rel[0].getLocationOnScreen().y;
}
public void updateMouse() {
for (int i=0; i < mouse.length; i++) {
if (mouse[i]==3 || mouse[i]==2) {
mouse[i]=0;
}
}
}
public void mouseWheelMoved(MouseWheelEvent w) {
mouse[3]=w.getWheelRotation();
}
public boolean isDown(int key) {
if (keys[key]==1 || keys[key]==2) {
return true;
}
else {
return false;
}
}
public void work() {
getMouse();
}
public void reset() {
updateKeys();
updateMouse();
}
public EventHandler(JFrame plane) {
this.pane=plane;
pane.requestFocus();
pane.addKeyListener(this);
pane.addMouseListener(this);
}
}
应用于您的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test extends JPanel{
boolean circle = false;
public EventHandler events; //EventHandler
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
if(circle){
g.drawRect(150,150,100,100);
}
}
public static void main(String[] args){
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(400,400);
//
events=new EventHandler(f);
Test test = new Test();
//f.add(test);
f.setContentPane(test);
while (true) {
events.reset();
events.work();
dpane.repaint();
if (events.mouse[2] != 0) {
if (circle) {
circle=false;
}
else {
circle=true;
}
}
}
}
}
所以,我的代码基本上是这样的:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test extends JPanel{
boolean circle = false;
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
if(circle){
g.drawRect(150,150,100,100);
}
}
public static void main(String[] args){
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(400,400);
Test test = new Test();
f.add(test);
}
}
我希望当我单击鼠标时,circle
变量发生变化并调用 repaint()
。
我该怎么做?谢谢
JPanel.addMouseListener就可以实现了。并注意 final
关键字在 jdk8 中是可选的,当内部 class.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test extends JPanel {
boolean circle = false;
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
if (circle) {
g.drawRect(150, 150, 100, 100);
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(400, 400);
final Test test = new Test();
test.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
test.circle = true;
test.repaint();
}
});
f.add(test);
}
}
这是我曾经写过的一个小EventHandler...
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class EventHandler implements KeyListener,MouseListener,MouseWheelListener {
public JFrame pane;
public int mouse_x;
public int mouse_y;
public int[] mouse=new int[5];
public int[] keys=new int[256];
@Override
public void mousePressed(MouseEvent m) {
//mouse[Math.abs(Math.min(3,m.getButton()-1))]=1;
switch (m.getButton()) {
case (MouseEvent.BUTTON1) :
mouse[2]=1;
case (MouseEvent.BUTTON2) :
mouse[3]=1;
case (MouseEvent.BUTTON3) :
mouse[4]=1;
}
}
@Override
public void mouseClicked(MouseEvent m) {
//mouse[Math.abs(Math.min(3,m.getButton()-1))]=2;
switch (m.getButton()) {
case (MouseEvent.BUTTON1) :
mouse[2]=2;
case (MouseEvent.BUTTON2) :
mouse[3]=2;
case (MouseEvent.BUTTON3) :
mouse[4]=2;
}
}
@Override
public void mouseReleased(MouseEvent m) {
//mouse[Math.abs(Math.min(3,m.getButton()-1))]=3;
switch (m.getButton()) {
case (MouseEvent.BUTTON1) :
mouse[2]=3;
case (MouseEvent.BUTTON2) :
mouse[3]=3;
case (MouseEvent.BUTTON3) :
mouse[4]=3;
}
}
@Override
public void mouseEntered(MouseEvent m) {
}
@Override
public void mouseExited(MouseEvent m) {
}
@Override
public void keyTyped(KeyEvent e) {
keys[e.getKeyCode()]=2;
}
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()]=3;
}
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()]=1;
}
public void updateKeys() {
for (int i=0; i < keys.length; i++) {
if (keys[i]==2 || keys[i]==3) {
keys[i]=0;
}
}
}
public void getMouse() {
Component[] rel;
rel = pane.getComponents();
mouse_x=mouse[0]=MouseInfo.getPointerInfo().getLocation().x-rel[0].getLocationOnScreen().x;
mouse_y=mouse[1]=MouseInfo.getPointerInfo().getLocation().y-rel[0].getLocationOnScreen().y;
}
public void updateMouse() {
for (int i=0; i < mouse.length; i++) {
if (mouse[i]==3 || mouse[i]==2) {
mouse[i]=0;
}
}
}
public void mouseWheelMoved(MouseWheelEvent w) {
mouse[3]=w.getWheelRotation();
}
public boolean isDown(int key) {
if (keys[key]==1 || keys[key]==2) {
return true;
}
else {
return false;
}
}
public void work() {
getMouse();
}
public void reset() {
updateKeys();
updateMouse();
}
public EventHandler(JFrame plane) {
this.pane=plane;
pane.requestFocus();
pane.addKeyListener(this);
pane.addMouseListener(this);
}
}
应用于您的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test extends JPanel{
boolean circle = false;
public EventHandler events; //EventHandler
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
if(circle){
g.drawRect(150,150,100,100);
}
}
public static void main(String[] args){
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(400,400);
//
events=new EventHandler(f);
Test test = new Test();
//f.add(test);
f.setContentPane(test);
while (true) {
events.reset();
events.work();
dpane.repaint();
if (events.mouse[2] != 0) {
if (circle) {
circle=false;
}
else {
circle=true;
}
}
}
}
}