关于 java 移动小球 问题....快崩溃咯..

香草芭芙 2012-12-11 01:36:43

package com.练习;

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;

public class Box
{
public static void main(String[] args)
{
new MyFrame();
}
}
class MyFrame extends JFrame
{
MyPanel mp = null;
public MyFrame()
{
mp = new MyPanel();
this.add(mp);
this.setSize(400, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyPanel extends JPanel implements Runnable
{
int x = 100, y = 100;
public MyPanel()
{
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(new Color(64, 64, 64));
g.fillRect(0, 0, 400, 300);
g.setColor(Color.YELLOW);
g.fillOval(x, y, 15, 15);
}
@Override
public void run()
{
}
}

救命啊... 写到这儿 卡住了, 脑筋怎么也转不过来,
想实现 运行 就在面板上 出现一个小球 自动移动 遇墙 则换方向,

我现在的问题是, 不知道线程放在哪
思路应该是 g.fillOval(x, y, 15, 15); 利用repaint();函数重绘 不知道找函数要放到哪去才合适
T.T x,y 也得放在线程里 , 结果不会取出来 -0 - 悲剧... 想撞墙!....

恳求高手们 指点明路...
...全文
210 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
香草芭芙 2012-12-11
  • 打赏
  • 举报
回复
import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class Temp { public static void main(String[] args) { new MyFrame(); } } class MyFrame extends JFrame { MyPanel mp = null; public MyFrame() { mp =new MyPanel(); this.add(mp); this.setSize(500,400); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class MyPanel extends JPanel { X x=null; Y y=null; public MyPanel() { x=new X(100); y=new Y(50); new Thread(x).start(); new Thread(y).start(); } public void paint(Graphics g) { super.paint(g); g.fillRect(0, 0, 400, 300); g.setColor(Color.BLUE); g.fillOval(x.getX(), y.getY(), 20, 20); this.repaint(); } } class X implements Runnable { int x; public X(int x) { this.x = x; } public int getX() { return x; } public void setX(int x) { this.x = x; } @Override public void run() { while (true) { try { Thread.sleep(20); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (x > 0) { while (x > 0) { try { Thread.sleep(20); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } x--; } } else if (x<=0) { while (x <=380) { try { Thread.sleep(20); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } x++; } } } } } class Y implements Runnable { int y; public Y(int y) { this.y = y; } public int getY() { return y; } public void setY(int y) { this.y = y; } @Override public void run() { while (true) { try { Thread.sleep(20); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (y > 0) { while (y > 0) { try { Thread.sleep(20); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } y--; } } else if (y<=0) { while (y <=280) { try { Thread.sleep(20); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } y++; } } } } } 自己憋出来的 -0 - ... 有没有更简单点的方法... paint 函数里 写 this.repaint(); 这么写 没关系吗, 到是可以实现 -0 -
自易而望 2012-12-11
  • 打赏
  • 举报
回复
给你参考下,一个用applet写的代码: 最下面那个类是主类,中间是主要; Ball类: import javax.swing.Timer; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Ball extends JPanel { private int delay = 10; // Create a timer with delay 1000 ms private Timer timer = new Timer(delay, new TimerListener()); private int x = 0; private int y = 0; // Current ball position private int radius = 5; // Ball radius private int dx = 2; // Increment on ball's x-coordinate private int dy = 2; // Increment on ball's y-coordinate public Ball() { timer.start(); } private class TimerListener implements ActionListener { /** Handle the action event */ public void actionPerformed(ActionEvent e) { repaint(); } } protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.red); // Check boundaries if (x < radius) dx = Math.abs(dx); if (x > getWidth() - radius) dx = -Math.abs(dx); if (y < radius) dy = Math.abs(dy); if (y > getHeight() - radius) dy = -Math.abs(dy); // Adjust ball position x += dx; y += dy; g.fillOval(x - radius, y - radius, radius * 2, radius * 2); } public void suspend() { timer.stop(); // Suspend timer } public void resume() { timer.start(); // Resume timer } public void setDelay(int delay) { this.delay = delay; timer.setDelay(delay); } } —————————————————————————————————————————— BallControl 类: mport javax.swing.*; import java.awt.event.*; import java.awt.*; public class BallControl extends JPanel { private Ball ball = new Ball(); private JButton jbtSuspend = new JButton("Suspend"); private JButton jbtResume = new JButton("Resume"); private JScrollBar jsbDelay = new JScrollBar(); public BallControl() { // Group buttons in a panel JPanel panel = new JPanel(); panel.add(jbtSuspend); panel.add(jbtResume); // Add ball and buttons to the panel ball.setBorder(new javax.swing.border.LineBorder(Color.red)); jsbDelay.setOrientation(JScrollBar.HORIZONTAL); ball.setDelay(jsbDelay.getMaximum()); setLayout(new BorderLayout()); add(jsbDelay, BorderLayout.NORTH); add(ball, BorderLayout.CENTER); add(panel, BorderLayout.SOUTH); // Register listeners jbtSuspend.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ball.suspend(); } }); jbtResume.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ball.resume(); } }); jsbDelay.addAdjustmentListener(new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent e) { ball.setDelay(jsbDelay.getMaximum() - e.getValue()); } }); } } ------------------------------- BounceBallApp 类: import java.awt.*; import javax.swing.*; public class BounceBallApp extends JApplet { public BounceBallApp() { add(new BallControl()); } public static void main(String[] args) { BounceBallApp applet = new BounceBallApp(); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("BounceBallApp"); frame.add(applet, BorderLayout.CENTER); frame.setSize(400, 320); frame.setVisible(true); } }

51,410

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧