重绘问题()

时间:2017-12-18 21:20:24

标签: java repaint

我在使用重绘时遇到了一些麻烦。代码应该在光标后面画一条线。

package javaapplication6;
import java.awt.*;
import javax.swing.JFrame;
import java.awt.event.*;

class Test extends Canvas implements MouseListener, MouseMotionListener {
int x[]=new int[1024];
int y[]=new int[1024];
int size=0;

public void MouseDemo(){
    addMouseListener(this);
    addMouseMotionListener(this);
}

public void paint (Graphics g){
    g.setColor(Color.red);
    for (int i=1;i<size;i++) g.drawLine(x[i],y[i],x[i-1],y[i-1]);

}

public void mousePressed(MouseEvent e){
x[size]=e.getX();
y[size]=e.getY();
size++;

}

public void mouseDragged(MouseEvent e){
    y[size]=e.getY();
    x[size]=e.getX();
    size++;
    repaint();
}

public void mouseEntered(MouseEvent e){
    size=0;
    y[size]=e.getY();
    x[size]=e.getX();
    repaint();
}

public void mouseExited (MouseEvent e){
    size=0;
    repaint();

}

public void mouseReleased (MouseEvent e){}

public void mouseClicked (MouseEvent e){

}

public void mouseMoved (MouseEvent e){}
}

public class JavaApplication6 {


public static void main(String[] args) {

   JFrame win= new JFrame ("Target");
win.setSize(600,500);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.add(new Test());
win.setVisible(true);

  }}

这同样的代码在学校的计算机上工作,但当我回到家并尝试做另一个例子并且它没有真正起作用所以我决定测试这个并且它也没有用。我会非常感谢一些帮助和解释为什么重绘不会触发。

1 个答案:

答案 0 :(得分:1)

你的主要问题是......

public void MouseDemo() {
    addMouseListener(this);
    addMouseMotionListener(this);
}

这是一个方法,而不是构造函数,所以它永远不会注册你的监听器。相反,将其更改为......

public MouseDemo() {
    addMouseListener(this);
    addMouseMotionListener(this);
}

或者,基于你实际的示例代码......

public Test() {
    addMouseListener(this);
    addMouseMotionListener(this);
}

java.awt.Canvas是一个重量级的组件,AWT组件并不总是与Swing一起使用,因为他们没有Swing实现它的z-ordering概念。

JPanel开始你会更好。

按照惯例,您应该覆盖paintComponent方法并在其中执行自定义绘画,记住先调用super.paintComponent

由于框架边框已插入框架的可见区域,因此“可用”区域将小于setSize指定的区域,而应覆盖getPreferredSize方法JPanel 1}}并指定您喜欢的“可用”区域。从那里,您可以pack围绕它的框架。

为避免线程出现任何问题,您应该使用EventQueue.invokeLater启动

中的UI

例如......

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test extends JPanel implements MouseListener, MouseMotionListener {

    int x[] = new int[1024];
    int y[] = new int[1024];
    int size = 0;

    public Test() {
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(600, 500);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.red);
        for (int i = 1; i < size; i++) {
            g.drawLine(x[i], y[i], x[i - 1], y[i - 1]);
        }

    }

    @Override
    public void mousePressed(MouseEvent e) {
        x[size] = e.getX();
        y[size] = e.getY();
        size++;

    }

    public void mouseDragged(MouseEvent e) {
        y[size] = e.getY();
        x[size] = e.getX();
        size++;
        repaint();
    }

    public void mouseEntered(MouseEvent e) {
        System.out.println("Entered");
        size = 0;
        y[size] = e.getY();
        x[size] = e.getX();
        repaint();
    }

    public void mouseExited(MouseEvent e) {
        size = 0;
        repaint();

    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {

    }

    public void mouseMoved(MouseEvent e) {
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new Test());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}