-1

我正在为可联网的战舰类游戏编写 gui(是的,我也是)。如果我右键单击设置一个航点,所有的 mapLabels 都会消失并且我没有得到它,因为所有右键单击应该做的就是向服务器发送一些字符串,而不是以任何方式影响显示本身。如果我调用 panel.repaint(); 窗格.验证();设置航路点后,所有内容都会再次显示,但性能接近于不可能像那样慢。

我意识到,这是很多代码,但我无法想象 FieldListener 中的问题,尽管它必须是。因此,我不知道要发布什么。如果你想看别的东西,你可以要求它......

这是我们代码的一部分,很可能是造成问题的原因:

/**
 * The listener for a field.
 * @author Andris
 *
 */
private class FieldListener implements MouseListener {

    private int[] xy;
    private int[] pressedPos = new int[] {-1,-1};


    @Override
    public void mousePressed(MouseEvent evt) {
        Field field = map.getField(xy[0],xy[1]);
        pressedPos = xy.clone();
        switch(evt.getButton()){
        case MouseEvent.BUTTON1://-------well, BUTTON1......
            break;
        case MouseEvent.BUTTON3:
            switch(selBtn){
            case 2://---------------this is the case
                client.Out("some string");// this sends to the server, nothing else...
                break;
            }
            break;
        }
    }

在服务器回答之后执行(在来自不同包的完全不同的类中,并且所有消失的字段都是私有的):

public class Server {
   client.Out(cfg.chat_server+"you have set a waypoint at ("+x+","+y+").");

public class ChatFrame extends JPanel {
    public void Out(String nextString){
        text.append(" "+nextString);
        text.append("\n");
        JScrollBar scroll = display.getVerticalScrollBar();
        if(!scroll.getValueIsAdjusting())scroll.setValue(Integer.MAX_VALUE);
    }

这是自定义绘制方法(也许这是因为 ImageObserver 是错误的):

private class MapLabel extends JLabel{ //------------- in MapFrame

    private Field field;
    private int[] xy;

    public void paint(Graphics g){
        Image image = getImageNsetToolTip();
        Graphics2D g2D=(Graphics2D)g;
        g2D.drawImage(image, 0, 0, actImgSize, actImgSize, this);
        g2D.setClip(0, 0, actImgSize, actImgSize);
        super.paint(g2D);
    }

    /**
     * gets the proper image and sets the tool-tip
     * @return
     */
    private Image getImageNsetToolTip(){
        Image result;
        String toolTip = "("+xy[0]+","+xy[1]+")";
        TileType type = field.getType();
        switch(type){
        case harbor:
            result=harborImg[0];
            break;
        case land: 
//------------------------------etc...


        this.setToolTipText(toolTip);
        return result;
    }

这是其余的一些:

...lots of imports...

/**
 * This is the frame in which the GameMap is displayed.
 * @author Andris
 *
 */
@SuppressWarnings("serial")
public class MapFrame extends JPanel {

...lots of variables...

    /**
     * Creates the frame, but doesn't make it visible yet.
     * @param window the GameWindow in which this frame will be embedded.
     * @param client the client who runs this.
     */
    public MapFrame(GameWindow window, Client client){

...lots of variables initialized...

        panel = new JPanel();
        pane = new JScrollPane(panel,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);


        buttons = new JButton[nButtons];
        buttonListener = new ButtonListener();
        for(int i=0; i<nButtons; i++){
            buttons[i] = new JButton(buttonTexts[i]);
            buttons[i].setName(buttonTexts[i].replace(' ', '_'));
            buttons[i].addActionListener(buttonListener);
            buttonPanel.add(buttons[i]);
        }
4

2 回答 2

2

有问题,你的油漆代码

public void paint(Graphics g){
    Image image = getImageNsetToolTip();
    Graphics2D g2D=(Graphics2D)g;
    g2D.drawImage(image, 0, 0, actImgSize, actImgSize, this);
    g2D.setClip(0, 0, actImgSize, actImgSize);
    super.paint(g2D);
 }

以前绘画super.paint可能会抹去你以前画的东西。发生这种情况是因为paint调用paintComponent和。paintBorderpaintComponents

的工作之一paintComponent是为绘画准备图形上下文(清理它)

您还应该避免弄乱剪辑。由于在 Swing 中绘制工作的方式,您冒着允许组件绘制超出其物理边界的风险。剪辑由重绘管理器设置为等于paint调用之前组件的大小

相反,您应该使用它paintComponent来执行您的自定义绘画

查看Perofming 定制绘画了解更多详情

现在,我想到的问题是JLabel,当 a 的功能之一JLabel是显示图标时,为什么要在 a 上绘制自定义图像?

于 2013-05-09T21:03:33.330 回答
0

我终于弄明白了:
有很多线程在运行,因为服务器、客户端、每个连接等等都在它们自己的线程中运行。
可能会导致摇摆事件队列发挥作用,结果是事情没有在应该执行的时候执行。
这通常仅在有许多其他线程时才会发生(因此不可能制作 SSCCE)。
为了防止这种情况,像 validate() 或 repaint() 这样的每个方法都应该作为 invokeLater 调用(因此由事件队列正确处理),特别是如果它们是从不同的线程调用的。
所以这就是我到处写的:

    SwingUtilities.invokeLater(new Runnable(){
        @override
        public void run(){
            panel.validate();
            panel.repaint();
            pane.validate();
        }
    });

或者:

    SwingUtilities.invokeLater(new Runnable(){
        @override
        public void run(){
            mapLabels[x][y].repaint();
        }
    });
于 2013-05-14T11:22:13.533 回答