Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
// This is part of the program used in lab exercise 02 // This part does not include the code generated by Netbeans // Please read the slides and watch the video for explanation // // This file is for reference only. It is your responsibility // to write a functional program. // // Yung-Hsiang Lu, ECE Purdue // public class BallPanel extends JPanel { public BallPanel(int width, int height, JLabel location, JLabel direction) { bpWidth = width; bpHeight = height; // ball's initial location is near the center bpBallX = width / 2; bpBallY = height / 2; do { bpBallVX = Math.random(); } while (bpBallVX < 0.1); bpBallVY = Math.sqrt(1 - bpBallVX * bpBallVX); bpPaddleHeight = height / 7; bpPaddleTop = bpPaddleHeight; setSize(width, height); // System.out.println("width and height " + width + " " + height); bpLocationLabel = location; bpDirectionLabel = direction; } public void setPaddle(int top) { if (top < bpPaddleTop) { bpPaddleTop--; } if (top > bpPaddleTop) { bpPaddleTop++; } if (bpPaddleTop < 0) { bpPaddleTop = 0; } if (bpPaddleTop >= (bpHeight - bpPaddleHeight)) { bpPaddleTop = bpHeight - bpPaddleHeight; } repaint(); // draw the new paddle's location } public void paintComponent(Graphics gfx) { // System.out.println("paintComponent"); // update labels bpLocationLabel.setText("location " + (int) bpBallX + " , " + (int) bpBallY); int vx = (int) (bpBallVX * 1000); int vy = (int) (bpBallVY * 1000); bpDirectionLabel.setText("direction " + vx + " , " + vy); // erase everything gfx.setColor(bpBackgroundColor); gfx.fillRect(0, 0, bpWidth - 1, bpHeight - 1); // draw ball gfx.setColor(bpBallColor); gfx.fillOval((int) bpBallX, (int) bpBallY, bpBallSize, bpBallSize); // draw paddle gfx.setColor(bpPaddleColor); gfx.fillRect(0, bpPaddleTop, bpPaddleWidth, bpPaddleHeight); // update ball's location bpBallX += bpBallVX; bpBallY += bpBallVY; // check whether the ball hits a wall if (bpBallX < 0) { bpBallX = 0; bpBallVX = -bpBallVX; } if (bpBallX > (bpWidth - bpBallSize)) { bpBallX = bpWidth - bpBallSize; bpBallVX = -bpBallVX; } if (bpBallY < 0) { bpBallY = 0; bpBallVY = -bpBallVY; } if (bpBallY > (bpHeight - bpBallSize)) { bpBallY = bpHeight - bpBallSize; bpBallVY = -bpBallVY; } } // all attributes start with "bp" (BallPanel) private int bpWidth; private int bpHeight; private Color bpBackgroundColor = Color.white; private Color bpBallColor = Color.black; private Color bpPaddleColor = Color.green; private int bpBallSize = 10; private double bpBallX; private double bpBallY; private double bpBallVX; // (VX, VY) is a unit vector private double bpBallVY; private int bpPaddleHeight; private int bpPaddleTop; private int bpPaddleWidth = 5; private JLabel bpLocationLabel; private JLabel bpDirectionLabel; } class UpdatePanelThread extends Thread { public UpdatePanelThread(BallPanel bp) { uptPanel = bp; } public void setDelay(int delay) { uptDelay = delay; // does not have to be sychronized } public void run() { while (uptDelay > 0) { uptPanel.repaint(); try { sleep(uptDelay); } catch (InterruptedException ie) { // do nothing here } } } BallPanel uptPanel; int uptDelay; // negative to stop updating } public class NewJFrame extends javax.swing.JFrame { /** Creates new form NewJFrame */ public NewJFrame() { initComponents(); njfBP = new BallPanel(jPanel1.getWidth(), jPanel1.getHeight(), locationLabel, directionLabel); jPanel1.add(njfBP); njfThread = new UpdatePanelThread(njfBP); int sliderValue = speedSlider.getValue(); speedLabel.setText("Speed " + sliderValue); njfThread.setDelay(speedSlider.getMaximum() + 1 - sliderValue); njfThread.start(); } private void initComponents() { int sliderValue = speedSlider.getValue(); speedLabel.setText("Speed " + sliderValue); njfThread.setDelay(speedSlider.getMaximum() + 1 - sliderValue); } private void speedSliderStateChanged(javax.swing.event.ChangeEvent evt) { int sliderValue = speedSlider.getValue(); speedLabel.setText("Speed " + sliderValue); njfThread.setDelay(speedSlider.getMaximum() + 1 - sliderValue); } BallPanel njfBP; UpdatePanelThread njfThread; }