【问题标题】:Java: Calling repaint() in static context (Or how to avoid it)Java:在静态上下文中调用 repaint()(或如何避免它)
【发布时间】:2018-08-29 06:20:54
【问题描述】:

我正在开发一个简单的应用程序 Java/Swing,其中涉及让用户单击一个框并将其拖动。我无法理解如何使用重绘方法。我创建了这个问题的示例,其中绘制了一个正方形,然后在 mousePressed 上它获取单击的 x 坐标,并通过指针移动的量来替换原始图形。

我已经阅读了在 Swing 中绘制的常用指南,但我还没有看到关于如何编写一个包含 mouseMotion 和 mouseListener 的程序的问题的任何答案(据我所知,这意味着 mouseListener必须作为自己的类来实现,而不是将其合并到自定义 JPanel 类中的常见解决方案)并且还根据鼠标操作调用repaint()

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.MouseInputAdapter;


class drawTest extends JPanel {
    static int xpos_square = 200; 
    static int ypos_square = 200; 
    int width = 100;  
    int height = 100;

    static int x_init;
    static int y_init;

    public drawTest(){
        addMouseListener(new mouseListener());
        addMouseMotionListener(new mouseListener());
        setBackground(Color.BLACK); 
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        drawSquare(g);
    }

    public void drawSquare(Graphics g){
        g.setColor(Color.GREEN);
        g.fillRect(xpos_square, ypos_square, height, width);            
    }   

    public static void moveShape(int x, int y){
        xpos_square += x-x_init;
        ypos_square += y-y_init;
        repaint();
    }
    public static void getChord(int x, int y){
        x_init = x;
        y_init = y;     
    }
}   

class mouseListener extends MouseInputAdapter{

    public void mousePressed(MouseEvent e){     
        drawTest.getChord(e.getX(),e.getY());
    }   

    public void mouseDragged(MouseEvent e){
        drawTest.moveShape(e.getX(),e.getY());
    }       
}

public class myTest {

    JFrame myFrame = new JFrame();
    JPanel myDrawing = new drawTest();

    public myTest () {
        myFrame.add(myDrawing);
        myFrame.setSize(500,500);
        myFrame.setVisible(true);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }   

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

问题当然是不能在静态上下文中调用repaint()。但是,我不知道如何避免这种情况,因为如果我希望位置平滑更新,则必须通过 mouseDragged 方法调用它。

我还能如何使用repaint() 方法根据鼠标移动重绘?

【问题讨论】:

  • 有很多方法可以解决这个问题,当您在命令行应用程序中学习 Java OOP 概念时,所有这些方法都应该进行排序。
  • 请同时遵守 Java 的命名准则,其中包括以大写字母开头的类(例如 MyTest 超过 myTest 等)

标签: java swing awt


【解决方案1】:

所以我想出了一个解决方法,在 addMouseListener 中使用匿名方法。这绕过了重绘调用中对静态方法的需求。如果其他人有类似的问题,也许他们会觉得有帮助。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.MouseInputAdapter;


class DrawTest extends JPanel {
    static int xpos_square = 200; 
    static int ypos_square = 200; 
    int width = 100;  
    int height = 100;

    static int x_init;
    static int y_init;

    public DrawTest(){
        addMouseListener(new mouseListener(){   public void mousePressed(MouseEvent e){     
            getClick(e.getX(),e.getY());
        }});
        addMouseMotionListener(new mouseListener(){ public void mouseDragged(MouseEvent e){
            moveShape(e.getX(),e.getY());   
        }});
        setBackground(Color.BLACK); 
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        drawSquare(g);
    }

    public void drawSquare(Graphics g){
        g.setColor(Color.GREEN);
        g.fillRect(xpos_square, ypos_square, height, width);            
    }   

    public void moveShape(int x, int y){
        if((x >= xpos_square)&&(x <= xpos_square + width)&&(y >= ypos_square)&&(y <= ypos_square + height)){
            xpos_square += x-x_init;
            ypos_square += y-y_init;
            x_init = x;
            y_init = y; 
            repaint();
        }
    }
    public void getClick(int x, int y){
        x_init = x;
        y_init = y;     
    }
}

public class MyTest {

    JFrame myFrame = new JFrame();
    JPanel myDrawing = new DrawTest();

    public MyTest () {
        myFrame.add(myDrawing);
        myFrame.setSize(500,500);
        myFrame.setVisible(true);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }   

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

【讨论】:

  • 另外一个,不仅要找出一种方法来做到这一点,还要包括解决方案作为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
  • 2014-08-28
  • 2018-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多