实验五 Java图形界面及多线程

  • 格式:doc
  • 大小:56.00 KB
  • 文档页数:7

实验五图形界面及多线程【实验目的】一、掌握Jpanel的使用方法;二、掌握JAVA简单图形界面的设计;三、掌握JAVA多线程控制。

【实验内容】一、在一个Jpanel中显示一个小球,当点击鼠标时,开始移动小球;当小球撞到Jpanel的边框时,小球将反弹回去继续移动。

小球用一个线程来控制其移动;二、每点击一次鼠标,增加一个移动的小球,最多产生20个小球,随机选择小球的颜色;三、增加功能,当两个小球相撞时,它们将各自反弹。

【实验结果】//小球类import java.awt.Color;import java.awt.Point;import java.awt.Rectangle;import java.awt.Shape;import java.awt.geom.Ellipse2D;import java.util.Random;public class Ball {private double x;private double y;private double xR=30;private double yR=30;private double xMove;private double yMove;private Color color;private Ellipse2D shape;private static Random random=new Random();public Ball(){color=newColor(random.nextInt(256),random.nextInt(256),random.nextInt(256));xMove=Math.cos(random.nextDouble()*6.24)*10;yMove=Math.cos(random.nextDouble()*6.24)*10;}public void setLocation(Point point){x=point.getX();y=point.getY();}public void move(Rectangle rect) {x+=xMove;y+=yMove;if(x<rect.getMinX()){x=rect.getMinX();xMove=-xMove;}else if((x+xR)>rect.getMaxX()){x=rect.getMaxX()-xR;xMove=-xMove;}if(y<rect.getMinX()){y=rect.getMinY();yMove=-yMove;}else if((y+yR)>rect.getMaxY()){y=rect.getMaxY()-yR;yMove=-yMove;}}public void hit(Ball ball){double temp;if(((this.x-ball.x)*(this.x-ball.x)+(this.y-ball.y)*(this.y-ball. y))<xR*yR){if(((this.xMove<=0&&ball.xMove>=0)||(this.xMove>=0&&ball.xMove<=0 ))&&((this.yMove>=0&&ball.yMove>=0)||(this.yMove<=0&&ball.yMove<=0) )){temp=this.xMove;this.xMove=ball.xMove;ball.xMove=temp;}elseif(((this.xMove<=0&&ball.xMove<=0)||(this.xMove>=0&&ball.xMove>=0))&&((this.yMove<=0&&ball.yMove>=0)||(this.yMove>=0&&ball.yMove<=0) )){temp=this.yMove;this.yMove=ball.yMove;ball.yMove=temp;}else{temp=this.xMove;this.xMove=ball.xMove;ball.xMove=temp;temp=this.yMove;this.yMove=ball.yMove;ball.yMove=temp;}}}public Shape getShape(){if(shape==null)shape=new Ellipse2D.Double(x, y, xR, yR);elseshape.setFrame(x, y, xR, yR);return shape;}public Color getColor(){return color;}}//绘制小球类import java.awt.Graphics;import java.awt.Graphics2D;import java.util.ArrayList;import java.util.List;import javax.swing.ImageIcon;import javax.swing.JPanel;public class BallPanel extends JPanel{private static final long serialVersionUID = 2L;public List<Ball> balls;public BallPanel(){balls=new ArrayList<Ball>();startPaintThread();new ImageIcon("292-11020215103447.jpg").getImage();}private void startPaintThread() {new Thread(){public void run() {while(true) {repaint();try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}}}}.start();}protected void paintComponent(Graphics g) {super.paintComponent(g);ImageIcon image = newImageIcon(this.getClass().getResource("zm.jpg"));g.drawImage(image.getImage(),-255,-150,null);Graphics2D g2 = (Graphics2D)g;for(int i=0;i<balls.size()-1;i++)for(int j=i+1;j<balls.size();j++)balls.get(i).hit(balls.get(j));for (Ball ball : balls) {g2.setColor(ball.getColor());g2.fill(ball.getShape());g2.setBackground(ball.getColor());}}public void addBall(Ball ball) {balls.add(ball);}}//小球线程类import java.awt.Rectangle;public class BallThread extends Thread{private Ball ball;private Rectangle rect;public BallThread(Ball ball, Rectangle rect) {this.ball = ball;this.rect = rect;}public void run() {while(true){ball.move(rect);try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}}}}//主界面类import java.awt.BorderLayout;import java.awt.Color;import java.awt.Point;import java.awt.Toolkit;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;public class BallFrame extends JFrame {private static final long serialVersionUID = 1L;private BallPanel ballPanel;private JLabel Jshow=new JLabel("制作:章进兴 0941901228");public BallFrame(){ballPanel =new BallPanel();this.add(ballPanel, BorderLayout.CENTER);actionHandler();showMe();}private void actionHandler(){ballPanel.addMouseListener(new MouseAdapter() {public void mousePressed(MouseEvent e) {if (e.getButton() == MouseEvent.BUTTON1) {if (ballPanel.balls.size()<20) {Ball ball = new Ball();Point point = e.getPoint();ball.setLocation(point);new BallThread(ball,ballPanel.getBounds()).start();ballPanel.addBall(ball);} else {JOptionPane.showMessageDialog(null,"球太多!");}} else {if (ballPanel.balls.size() < 1) {JOptionPane.showMessageDialog(null,"已无球!");return;}ballPanel.balls.remove(0);ballPanel.repaint();}}});}private void showMe(){Color color=new Color(255, 255, 255);this.setTitle("图形界面及多线程:小球碰撞(鼠标右击增加,左击减少)");this.setSize(500, 500);ballPanel.setBackground(color);ballPanel.add(Jshow);ballPanel.setLayout(null);Jshow.setBounds(340, 450, 200, 20);Toolkit toolkit = Toolkit.getDefaultToolkit();double width = toolkit.getScreenSize().getWidth();double height = toolkit.getScreenSize().getHeight();this.setLocation((int)(width-this.getWidth())/2,(int)(height-this .getHeight())/2);this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}}//主函数类public class BallMain {public static void main(String[] args) {new BallFrame();}}【实验小结】通过本次实验我掌握了Jpanel的使用方法,并通过次方法设置了简单的图形界面,绘画出简单的小球模型。