繁体   English   中英

Java重绘方法不起作用,为什么?

[英]Java repaint method do not working ,why?

我的英语不太好,但是我会尽力解释。

我是第一班和第二班两个班(第二班叫“格拉菲卡”)。

我希望我的矩形移动到我被点击的位置,但是显然他不动,我不明白为什么,请帮助。

import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.*;
    public class Grafika extends JPanel implements MouseListener{
        static int x=0,y=0;
        @Override
    public void paintComponent(Graphics g){
        g.setColor(Color.BLACK);
        g.fillRect(x, y, 20, 30);
    }

    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub``
        x=arg0.getX();
        y=arg0.getY();
        this.repaint(x, y, 20, 30);
    }

我将向您展示一个完整的代码,它很小。这是第二类。而我(我认为)唯一的问题是repaint()方法。 为什么我不知道:D。

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class Grafika extends JPanel implements MouseListener{
    static int x=0,y=0;
    @Override
public void paintComponent(Graphics g){
    g.setColor(Color.BLACK);
    g.fillRect(x, y, 20, 30);
}

public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub
    x=arg0.getX();
    y=arg0.getY();
    this.repaint(x, y, 20, 30);
}

public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub
}

public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

}

现在,我将向您展示我从二等舱叫到的一等舱。

import java.awt.*;
import javax.swing.*;
public class Glavna extends Grafika {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
     Grafika g=new Grafika();
     JFrame wi=new JFrame("Grafika");
     wi.setBounds(50, 50, 500, 600);
     wi.add(g);
     wi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     wi.setVisible(true);


    }

}

您在该代码中遇到了几个问题:

  • 您正在创建一个MouseListener(仍然缺少接口的几种方法),但是在将其添加到GUI的任何地方都看不到。 您必须在某处具有addMouseListener(this); 在您的代码中使侦听器正常工作。
  • 重涂本身不会移动矩形。 更改X和Y确实可以,但是同样,必须先添加MouseListener。
  • 您可能想调用repaint(); 没有参数来绘制整个组件。 否则,旧矩形可能不会被擦除。
  • 您的paintComponent方法应调用super.paintComponent(g); 作为覆盖方法的第一个方法调用。 没有这个,您将不会擦除旧的矩形,并且会中断绘制链,这可能会在子组件中产生绘制副作用。

例如:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

public class Grafika extends JPanel {
    private static final int RECT_W = 20;
    private static final int RECT_H = 30;
    private static final int PREF_W = 600;
    private static final int PREF_H = PREF_W;
    private static final Color MY_COLOR = Color.RED;    
    private int myX = 0;
    private int myY = 0;

    public Grafika() {
        MyMouse myMouse = new MyMouse();
        addMouseListener(myMouse);  // add MouseListener
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);  // **MUST** call this 
        g.setColor(MY_COLOR);
        g.fillRect(myX, myY, RECT_W, RECT_H);
    }

    private class MyMouse extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
            myX = e.getX();
            myY = e.getY();
            repaint(); // repaint the whole JPanel
        }
    }

    @Override  // to make the GUI larger
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private static void createAndShowGui() {
        Grafika mainPanel = new Grafika();

        JFrame frame = new JFrame("Grafika");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            createAndShowGui();
        });
    }

}

如果您想变得更奇特并开始在周围拖动方块:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class Grafika extends JPanel {
    private static final int RECT_W = 20;
    private static final int RECT_H = RECT_W;
    private static final int PREF_W = 600;
    private static final int PREF_H = PREF_W;
    private static final Color MY_COLOR = Color.RED;
    private int myX = 0;
    private int myY = 0;

    public Grafika() {
        MyMouse myMouse = new MyMouse();
        addMouseListener(myMouse);
        addMouseMotionListener(myMouse);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(MY_COLOR);
        g.fillRect(myX, myY, RECT_W, RECT_H);
    }

    private class MyMouse extends MouseAdapter {

        public void mousePressed(MouseEvent e) {
            moveRect(e);
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            moveRect(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            moveRect(e);
        }

        private void moveRect(MouseEvent e) {
            myX = e.getX() - RECT_W / 2;
            myY = e.getY() - RECT_H / 2;
            repaint();
        }

    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private static void createAndShowGui() {
        Grafika mainPanel = new Grafika();

        JFrame frame = new JFrame("Grafika");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            createAndShowGui();
        });
    }

}

您可能需要将鼠标侦听器添加到组件

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class Grafika extends JPanel implements MouseListener{
    static int x=0,y=0;

public Grafika(){
    super();
    addMouseListener(this); // add to constructor
}

@Override
public void paintComponent(Graphics g){
    g.setColor(Color.BLACK);
    g.fillRect(x, y, 20, 30);
}

public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub``
    x=arg0.getX();
    y=arg0.getY();
    this.repaint(x, y, 20, 30);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM