JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java SE 討論區 » Swing/AWT/SWT/JFace  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to postflat modego to previous topicgo to next topic
話題被移動
該話題已被移動 - morchory , 2007-09-30 17:15
如果您尚不清楚該話題被移動的原因,請參考論壇規則以及本版公告或者聯系本版版主。
本主題所含的標籤
無標籤
作者 Re:視窗框架的問題 ....急 [Re:rukawa11]
prins





發文: 73
積分: 0
於 2007-11-13 11:08 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我是用區塊比對是否有交集的上判斷圖是否超過邊界。所以我先設了四個邊界Rectangle,並且在key event裡面判斷移動後的位置是否會碰到邊界,如果有就還原圖的位置不移動。
實際上跑還是會跟視窗有點誤差,不果你可以調整邊界Rectangle讓他看起來符合frame的邊框。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import java.awt.*;
import java.awt.event.*;
 
public class work23_3 extends Frame implements Runnable
{
    int posX = 150, posY = 200, dx, dy, num = 0, flag = 0, frame_size = 350;
    int b0posX, b0posY, b0run = 0;
    int b1posX, b1posY, b1run = 0;
    int b2posX, b2posY, b2run = 0;
    int b3posX, b3posY, b3run = 0;
    int b4posX, b4posY, b4run = 0;
    int b5posX, b5posY, b5run = 0;
 
    Image img;
    Image bufferPage = null;
    //四個邊界區塊
    int wedge = 1;
    Rectangle top = new Rectangle(0, 0, frame_size, wedge);
    Rectangle bottom = new Rectangle(0, frame_size - wedge, frame_size, wedge);
    Rectangle left = new Rectangle(0, 0, wedge, frame_size);
    Rectangle right = new Rectangle(frame_size - wedge, 0, wedge, frame_size);
 
    public static void main(String args[])
    {
        work23_3 ClientStart = new work23_3();
    }
 
    public work23_3()
    {
        super("work23_3");
        setSize(frame_size, frame_size);
 
        img = Toolkit.getDefaultToolkit().getImage(getClass().getResource("car090.gif"));
 
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
 
        enableEvents(AWTEvent.KEY_EVENT_MASK);
        setVisible(true);
        new Thread(this).start();
    }
 
    public void processKeyEvent(KeyEvent e)
    {
        Rectangle rec = new Rectangle(posX, posY, img.getWidth(this), img.getHeight(this));
        
        if(e.getID() == KeyEvent.KEY_PRESSED) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_RIGHT:
                    //System.out.println("rec[" + rec.toString() + "] right[" + right.toString() + "]");
                    dx = 5;
                    dy = 0;
                    posX = posX + dx;
                    posY = posY + dy;
                    if (rec.intersects(right)) {
                        //不能往右-位置還原
                        System.out.println("intersect with right!!!!");
                        posX -= dx;
                        posY -= dy;
                    }
                    break;
                case KeyEvent.VK_LEFT:
                    //System.out.println("rec[" + rec.toString() + "] left[" + left.toString() + "]");
                    dx = -5;
                    dy = 0;
                    posX = posX + dx;
                    posY = posY + dy;
                    if (rec.intersects(left)) {
                        //不能往左-位置還原
                        System.out.println("intersect with left!!!!");
                        posX -= dx;
                        posY -= dy;
                    }
                    break;
                case KeyEvent.VK_UP:
                    //System.out.println("rec[" + rec.toString() + "] top[" + top.toString() + "]");
                    dx = 0;
                    dy = -5;
                    posX = posX + dx;
                    posY = posY + dy;
                    if (rec.intersects(top)) {
                        //不能往上-位置還原
                        System.out.println("intersect with top!!!!");
                        posX -= dx;
                        posY -= dy;
                    }
                    break;
                case KeyEvent.VK_DOWN:
                    System.out.println("rec[" + rec.toString() + "] bottom[" + bottom.toString() + "]");
                    dx = 0;
                    dy = 5;
                    posX = posX + dx;
                    posY = posY + dy;
                    if (rec.intersects(bottom)) {
                        //不能往下-位置還原
                        System.out.println("intersect with bottom!!!!");
                        posX -= dx;
                        posY -= dy;
                    }
                    break;
                case 32:
                    flag = num % 6; // bposX bposY為子彈的位置坐標
                    if(flag == 0) { // posX posY為射擊體的位置
                        b0posX = posX + 30;
                        b0posY = posY;
                        b0run = 1;
                    }
                    else if(flag == 1) { // flag為彈道數
                        b1posX = posX + 30;
                        b1posY = posY;
                        b1run = 1;
                    }
                    else if(flag == 2) {
                        b2posX = posX + 30;
                        b2posY = posY;
                        b2run = 1;
                    }
                    if(flag == 3) {
                        b3posX = posX + 30;
                        b3posY = posY;
                        b3run = 1;
                    }
                    else if(flag == 4) {
                        b4posX = posX + 30;
                        b4posY = posY;
                        b4run = 1;
                    }
                    else if(flag == 5) {
                        b5posX = posX + 30;
                        b5posY = posY;
                        b5run = 1;
                    }
                    num = num + 1;
                    break;
            }
 
        }
    }
 
    public void run()
    {
        while (true) { // 每次射擊時使用一組彈道
            if(flag == 0) {
                if(b0posY <= 0)
                    b0run = 0;
            }
            else if(flag == 1) {
                if(b1posY <= 0)
                    b1run = 0;
            }
            else if(flag == 2) {
                if(b2posY <= 0)
                    b2run = 0;
            }
            if(flag == 3) {
                if(b3posY <= 0)
                    b3run = 0;
            }
            else if(flag == 4) {
                if(b4posY <= 0)
                    b4run = 0;
            }
            else if(flag == 5) {
                if(b5posY <= 0)
                    b5run = 0;
            }
            if(b0run == 1)
                b0posY = b0posY - 10;
            if(b1run == 1)
                b1posY = b1posY - 10;
            if(b2run == 1)
                b2posY = b2posY - 10;
            if(b3run == 1)
                b3posY = b3posY - 10;
            if(b4run == 1)
                b4posY = b4posY - 10;
            if(b5run == 1)
                b5posY = b5posY - 10;
            repaint();
            try {
                Thread.sleep(50);
            }
            catch (InterruptedException e) {
                ;
            }
        }
    }
 
    public void update(Graphics g)
    {
        paint(g);
    }
 
    public void paint(Graphics g)
    {
        Graphics bufferg;
        if(bufferPage == null)
            bufferPage = createImage(frame_size, frame_size);
        bufferg = bufferPage.getGraphics(); // 緩衝頁避免閃爍
        bufferg.setColor(Color.black);
        bufferg.drawImage(img, posX, posY, this);
        if(b0run == 1) {
            bufferg.setColor(Color.black); // 黑色繪出彈道
            bufferg.fillRect(b0posX, b0posY, 3, 5); // 子彈軌跡 3為寬度 5為長度
            bufferg.setColor(Color.white); // 再以白色隨後拭淨彈道
            bufferg.fillRect(b0posX, b0posY + 5, 3, 25);
        }
        if(b1run == 1) {
            bufferg.setColor(Color.black);
            bufferg.fillRect(b1posX, b1posY, 3, 5);
            bufferg.setColor(Color.white);
            bufferg.fillRect(b1posX, b1posY + 5, 3, 25);
        }
        if(b2run == 1) {
            bufferg.setColor(Color.black);
            bufferg.fillRect(b2posX, b2posY, 3, 5);
            bufferg.setColor(Color.white);
            bufferg.fillRect(b2posX, b2posY + 5, 3, 25);
        }
        if(b3run == 1) {
            bufferg.setColor(Color.black);
            bufferg.fillRect(b3posX, b3posY, 3, 5);
            bufferg.setColor(Color.white);
            bufferg.fillRect(b3posX, b3posY + 5, 3, 25);
        }
        if(b4run == 1) {
            bufferg.setColor(Color.black);
            bufferg.fillRect(b4posX, b4posY, 3, 5);
            bufferg.setColor(Color.white);
            bufferg.fillRect(b4posX, b4posY + 5, 3, 25);
        }
        if(b5run == 1) {
            bufferg.setColor(Color.black);
            bufferg.fillRect(b5posX, b5posY, 3, 5);
            bufferg.setColor(Color.white);
            bufferg.fillRect(b5posX, b5posY + 5, 3, 25);
        }
        bufferg.dispose();
        g.drawImage(bufferPage, getInsets().left, getInsets().top, this);
    }
}


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
1715 視窗框架的問題 ....急 rukawa11 6517 2007-09-30 16:40
1322 Re:視窗框架的問題 ....急 prins 8656 2007-11-13 11:08
» JWorld@TW »  Java SE 討論區 » Swing/AWT/SWT/JFace

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8