有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

触摸位置的java Blackberry触摸事件检测对象

这是我的第一个黑莓应用,我正在尝试为触摸屏设备制作一个简单的游戏。 这是典型的图像分割成瓷砖,你必须解读它们,使瓷砖的顺序正确,并显示图片。 这样做的目的是,用户将能够在屏幕上的任何位置“拖动”一个磁贴,并将其与放置的磁贴交换位置

我正在为我的互动程序使用位图按钮

    HorizontalFieldManager hfm = new HorizontalFieldManager();
    add(hfm);
    hfm.add(button1);
    hfm.add(button2);
        etc...

我有一个叫做拼图屏幕的课程,如下所示:

class PuzzleScreen extends MainScreen implements FieldChangeListener {

我添加了以下内容,以尝试检测屏幕上触摸的人员的移动

protected boolean touchEvent(TouchEvent event) { 
case TouchEvent.MOVE:

            PuzzleTile fieldMove = (PuzzleTile) this.getLeafFieldWithFocus();

            // Dialog.alert("MOVE");

            int[] x_points;
            int[] y_points;
            int[] time_points;
            int size = event.getMovePointsSize();
            x_points = new int[size];
            y_points = new int[size];
            time_points = new int[size];
            event.getMovePoints(1, x_points, y_points, time_points);
                            return true;
        }

        return false;
    }

我需要找到哪个图像块(bitmapbutton)位于upevent的位置。 我有上面的坐标(我想),但不确定如何找到与之相关的瓷砖

贝克斯


共 (1) 个答案

  1. # 1 楼答案

    像这样的

    static class TestScreen extends MainScreen  {
    
        private static String down;
    
        public TestScreen () {
            HorizontalFieldManager hfm = new HorizontalFieldManager();
            add(hfm);
            hfm.add(new TestButton("bt1"));
            hfm.add(new TestButton("bt2"));
            hfm.add(new TestButton("bt3"));
            hfm.add(new TestButton("bt4"));
            hfm.add(new TestButton("bt5"));
            hfm.add(new TestButton("bt6"));
        }
    
        public static void touchDown(TestButton tb) {
            down = tb.getText();
            System.out.println("DOWN in " + tb.getText());
        }
    
        public static void touchUp(TestButton tb) {
            System.out.println("UP in " + tb.getText());
            if(!down.equals(tb.getText()))
                System.out.println("swap " + down + " and " + tb.getText());
            down = "";
        }
    
    
    
    
    class TestButton extends LabelField {
    
        public TestButton (String label) {
            super(label);
        }
    
        protected boolean touchEvent(TouchEvent event) { 
            if(event.getEvent() == TouchEvent.UP)
                TestScreen.touchUp(this);
            else if(event.getEvent() == TouchEvent.DOWN)
                TestScreen.touchDown(this);
           return super.touchEvent(event);
        }
    
    
    }
    
    }