无法解析方法addPaintListener

时间:2017-04-30 20:26:16

标签: java canvas methods resolve

我试图通过在Main文件中添加addPaintListeners函数来绘制画布,如下所示,但我收到错误'无法解析方法' addPaintListener(eos.listeners) .PaintListener)

使用PaintListener的Canvas。此画布与官方Java.AWT.Canvas类的代码相同。

我们已经在画布上添加了一个缓冲区,因此我们可以绘制并添加了一个PaintListener属性:

为画布添加了一个addPaintListeners函数

import    package eos;

import eos.listeners.PaintListener;
import eos.utility.Util;
import javax.swing.*;

import java.awt.*;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;


public class Main {
private static JLabel splash = new JLabel(Util.loadIcon("http://www.runescape.com/img/game/splash.gif"));

private static void displayFrame(String world, int width, int height) {
    JFrame frame = new JFrame("Eos");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);

    if (splash != null) {
        frame.add(splash);
        frame.pack();
        frame.setVisible(true);
    }

    centerFrame(frame);

    ClientApplet applet = new ClientApplet(world, width, height);
    frame.add(applet);
    applet.start();
    if (splash != null) {
        frame.remove(splash);
    }
    frame.revalidate();
    frame.pack();

    centerFrame(frame);

    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            frame.setVisible(false);
            applet.stop();
            frame.dispose();
            super.windowClosed(e);
        }
    });
}


private static void centerFrame(JFrame frame) {
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    int cX = (toolkit.getScreenSize().width / 2) - (frame.getWidth() / 2);
    int cY = (toolkit.getScreenSize().height / 2) - (frame.getHeight() / 2);
    frame.setMinimumSize(frame.getSize());
    frame.setLocation(cX, cY);
    frame.setVisible(true);
    frame.setResizable(true);
}

public static void notifyCanvasReady(Canvas canvas) {
    canvas.addPaintListener((PaintListener) g -> {
        g.setColor(Color.white);
        g.drawString("HELLO WORLD", 50, 50);




    });
}

public static void main(String[] args) {
    displayFrame("http://runescape.com", 800, 600);
}
}
package java.awt;

import eos.Main;
import eos.listeners.PaintListener;

import javax.accessibility.Accessible;
import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleRole;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.peer.CanvasPeer;


public class Canvas extends Component implements Accessible {
private static final long serialVersionUID = -2284879212465893870L;
private static int nameCounter = 0;
private static final String base = "canvas";

private BufferedImage debugImage;
private transient PaintListener paintListener;

public Canvas() {
    Main.notifyCanvasReady(this);
}

public Canvas(GraphicsConfiguration config) {
    this();
    setGraphicsConfiguration(config);
}

public void addPaintListener(PaintListener listener) {
    paintListener = (PaintListener)AWTEventMulticaster.addInternal(paintListener, listener);
}

@Override
public Graphics getGraphics() {
    if (debugImage == null || debugImage.getWidth() != getWidth() || debugImage.getHeight() != getHeight()) {
        debugImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
    }

    Graphics g = debugImage.getGraphics();

    if (paintListener != null) {
        paintListener.onPaint(g);
    }

    super.getGraphics().drawImage(debugImage, 0, 0, null);

    return g;
}

@Override
public void setVisible(boolean visible) {
    super.setVisible(visible);
}

@Override
void setGraphicsConfiguration(GraphicsConfiguration gc) {
    synchronized(getTreeLock()) {
        CanvasPeer peer = (CanvasPeer)getPeer();
        if (peer != null) {
            gc = peer.getAppropriateGraphicsConfiguration(gc);
        }
        super.setGraphicsConfiguration(gc);
    }
}

@Override
String constructComponentName() {
    synchronized (Canvas.class) {
        return base + nameCounter++;
    }
}

@Override
public void addNotify() {
    synchronized (getTreeLock()) {
        if (peer == null)
            peer = getToolkit().createCanvas(this);
        super.addNotify();
    }
}

@Override
public void paint(Graphics g) {
    g.clearRect(0, 0, width, height);
}

@Override
public void update(Graphics g) {
    g.clearRect(0, 0, width, height);
    paint(g);
}

@Override
boolean postsOldMouseEvents() {
    return true;
}

@Override
public void createBufferStrategy(int numBuffers) {
    super.createBufferStrategy(numBuffers);
}

@Override
public void createBufferStrategy(int numBuffers, BufferCapabilities caps) throws AWTException {
    super.createBufferStrategy(numBuffers, caps);
}

@Override
public BufferStrategy getBufferStrategy() {
    return super.getBufferStrategy();
}

@Override
public AccessibleContext getAccessibleContext() {
    if (accessibleContext == null) {
        accessibleContext = new AccessibleAWTCanvas();
    }
    return accessibleContext;
}

protected class AccessibleAWTCanvas extends AccessibleAWTComponent {
    private static final long serialVersionUID = -6325592262103146699L;

    @Override
    public AccessibleRole getAccessibleRole() {
        return AccessibleRole.CANVAS;
    }
}
}
 package eos.listeners;

import java.awt.*;
import java.util.EventListener;




public interface PaintListener extends EventListener {
public void onPaint(Graphics g);
}

1 个答案:

答案 0 :(得分:0)

也许问题在于你在Main中调用此方法的方式。试试这种方法:

public static void notifyCanvasReady(Canvas canvas) {
    canvas.addPaintListener(new PaintListener() {
        @Override
        public void onPaint(Graphics g) {
            g.setColor(Color.white);
            g.drawString("HELLO WORLD", 50, 50);            
        }
    });
}