Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » Java与OOP初步  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:java.lang.NullPointerException [Re:linqiuppp]
linqiuppp





发贴: 8
积分: 0
于 2005-10-25 00:48 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
哪位大吓愿意指点小弟一下!下面是源程序。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.geom.*;

public class BoundThread
{
public static void main(String []args)
{
BoundFrame frame=new BoundFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();  
}  
}

class BoundFrame extends JFrame
{
public BoundFrame()
{
setTitle("BoundThread");
setSize(500,400);
panel=new BoundPanel();
Container contentPane=getContentPane();
contentPane.add(panel,BorderLayout.CENTER);
JButton start=new JButton("Start");
JButton close=new JButton("Close");
start.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Ball ball=new Ball(panel);
panel.add(ball);
BallThread thread=new BallThread(ball);
thread.start();
  
}  
});  

close.addActionListener(new ActionListener()
{
  public void actionPerformed(ActionEvent event)
   {
     System.exit(0);
   }
     
});
  JPanel buttonPanel=new JPanel();
  buttonPanel.add(start);
  buttonPanel.add(close);
  contentPane.add(buttonPanel,BorderLayout.SOUTH);
}   

private BoundPanel panel;
}

class BallThread extends Thread
{
public BallThread(Ball aball)
{
  ball=aball;
  
}  
public void run()
{
try{
  
  for(int i=0;i<10000;i++)
   {
      ball.move();
      sleep(5);
   }
   }
  catch( InterruptedException e){}  
}

private Ball ball;
}

class BoundPanel extends JPanel
{
public void add(Ball aball)
{
balls.add(aball);  
}

public void paintComponent(Graphics g)
{
super.paintComponentPresent;

Graphics2D g2=(Graphics2D)g;

for(int i=0;i<balls.size();i++)
{
Ball ball=(Ball)balls.getLight Bulb;
g2.fill(new Ellipse2D.Double(ball.x,ball.y,ball.Scale,ball.Scale));  
}  
}  
private ArrayList balls;
}

class Ball
{
public Ball(Component apanel)
{
panel=(BoundPanel)apanel;
  
}  
public void move()
{
x=x+dx;
y=y+dy;
if(x<0){x=0;dx=-dx;}
if(y<0){y=0;dy=-dy;}
if(x+Scale>panel.getWidth()) {x=panel.getWidth()-Scale;dx=-dx;}
if(y+Scale>panel.getHeight()) {y=panel.getHeight()-Scale;dy=-dy;}
panel.repaint();  
}
private BoundPanel panel;
private int dx=2;
private int dy=2;
public int x=0;
public int y=0;
public int Scale=10;

}




有了Swing为什么还要SWT?

话题树型展开
人气 标题 作者 字数 发贴时间
21287 java.lang.NullPointerException linqiuppp 49 2005-10-25 00:46
19176 Re:java.lang.NullPointerException linqiuppp 2539 2005-10-25 00:48
19028 Re:java.lang.NullPointerException linqiuppp 29 2005-10-25 15:11
19558 Re:java.lang.NullPointerException andony756 156 2005-11-27 14:33
18784 Re:java.lang.NullPointerException why 233 2005-11-27 19:15
18848 Re:java.lang.NullPointerException youyigong 146 2005-11-28 13:56
18939 Re:java.lang.NullPointerException andony756 1368 2005-11-28 21:21
19121 Re:java.lang.NullPointerException ntshenwh 128 2005-11-28 22:09
18820 Re:java.lang.NullPointerException andony756 156 2005-11-28 22:32
18765 Re:java.lang.NullPointerException why 561 2005-11-29 18:22
18825 Re:java.lang.NullPointerException andony756 113 2005-11-29 20:37
18660 Re:java.lang.NullPointerException why 340 2005-11-30 17:50
18789 Re:java.lang.NullPointerException wangyabing 49 2005-12-29 16:22
18807 Re:java.lang.NullPointerException why 131 2005-12-29 20:46

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923