简体   繁体   English

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

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

Well my English is not so good but i will try to explain. 我的英语不太好,但是我会尽力解释。

I was make two class,First Class and Second Class(Second was named "Grafika"). 我是第一班和第二班两个班(第二班叫“格拉菲卡”)。

I want that my rectangle move to position where i was clicked , but obviously he dont move and i cant understand why, please help . 我希望我的矩形移动到我被点击的位置,但是显然他不动,我不明白为什么,请帮助。

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);
    }

i will show you a full code, it was small .This was second class.And my(i think) only problem is repaint() method . 我将向您展示一个完整的代码,它很小。这是第二类。而我(我认为)唯一的问题是repaint()方法。 Why i dont know :D . 为什么我不知道: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

}

} }

Now i will show you first class from where i was calling second class. 现在,我将向您展示我从二等舱叫到的一等舱。

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);


    }

}

You've several problems in that code: 您在该代码中遇到了几个问题:

  • You're sort of creating a MouseListener (you're still missing several of the interface's methods), but I don't see anywhere that you've added it to your GUI. 您正在创建一个MouseListener(仍然缺少接口的几种方法),但是在将其添加到GUI的任何地方都看不到。 You must somewhere have addMouseListener(this); 您必须在某处具有addMouseListener(this); in your code for the listener to work. 在您的代码中使侦听器正常工作。
  • Repaint by itself doesn't move the rectangle. 重涂本身不会移动矩形。 Changing the X and Y does, but again, not without adding the MouseListener first. 更改X和Y确实可以,但是同样,必须先添加MouseListener。
  • You probably want to call repaint(); 您可能想调用repaint(); without parameters to paint the whole component. 没有参数来绘制整个组件。 Otherwise the old rectangle might not get erased. 否则,旧矩形可能不会被擦除。
  • Your paintComponent method should call the super.paintComponent(g); 您的paintComponent方法应调用super.paintComponent(g); as the first method call of your override method. 作为覆盖方法的第一个方法调用。 Without this you won't erase the old rectangle, and you break the painting chain which could have painting side effects in child components. 没有这个,您将不会擦除旧的矩形,并且会中断绘制链,这可能会在子组件中产生绘制副作用。

For example: 例如:

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();
        });
    }

}

If you want to get fancier and start dragging the square around: 如果您想变得更奇特并开始在周围拖动方块:

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();
        });
    }

}

you have to add mouse listener to component porobably 您可能需要将鼠标侦听器添加到组件

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