Flask接口签名sign原理与实例代码浅析
206
2023-07-08
Java实力弹弹球实现代码
先看看效果图:
直接上代码了。
微调按钮加画布画几个圆,再实现监听。。。
package cn.hncu.threadDemo.thread2;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.jspinner;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class BallsJFrame extends JFrame implements ChangeListener{
private BallsCanvas ball;
private JSpinner spinner;
public BallsJFrame(){
super("弹弹球");
this.setBounds(300, 200, 400, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Color colors[] = {Color.red,Color.green,Color.blue,Color.magenta,Color.cyan};
ball = new BallsCanvas(colors,100);
this.getContentPane().add(ball);//默认是CENTER位置
JPanel panel = new JPanel();
this.getContentPane().add(panel,"South");
panel.add(new JLabel("Delay"));
spinner = new JSpinner();
spinner.setValue(100);
panel.add(spinner);
spinner.addChangeListener(this);
this.setVisible(true);
}
@Override
public void stateChanged(ChangeEvent e) {
int value = Integer.parseInt(""+spinner.getValue());
ball.setDelay(value);
}
public static void main(String[] args) {
new BallsJFrame();
}
}
class BallsCanvas extends Canvas implements ActionListener, FocusListener{
private Ball balls[];//存放所有的球
private Timer timer;//javax.swing.Timer
public BallsCanvas(Color colors[] ,int delay){
this.balls = new Ball[colors.length];
for(int i=0,x=40;i this.balls[i] = new Ball(x,x,colors[i]); } //让当前画布监听 焦点事件 this.addFocusListener(this); timer = new Timer(delay,this); timer.start(); } public void setDelay(int delay){ timer.setDelay(delay); } @Override public void paint(Graphics g) { for(int i=0;i g.setColor(balls[i].color); //让每个球的坐标变化一下---(x坐标) balls[i].x = balls[i].left ? balls[i].x-10:balls[i].x+10; //当球碰壁时,更改球的方向 if(balls[i].x<=0||balls[i].x>=this.getWidth()-24){ balls[i].left = !balls[i].left;//切换方向 } //让每个球的坐标变化一下---(y坐标) balls[i].y = balls[i].up ? balls[i].y-10:balls[i].y+10; //当球碰壁时,更改球的方向 if(balls[i].y<=0||balls[i].y>=this.getHeight()-22){ balls[i].up = !balls[i].up;//切换方向 } g.fillOval(balls[i].x, balls[i].y, 20, 20); } } @Override public void actionPerformed(ActionEvent e) { //System.out.println("aaa"); repaint();//刷新画布.调用paint(Graphics g) } @Override public void focusGained(FocusEvent e) { timer.stop(); } @Override public void focusLost(FocusEvent e) { timer.restart(); } private static class Ball{ int x,y; boolean up,left; Color color; public Ball(int x, int y, Color color) { this.x = x; this.y = y; this.color = color; up = left = false; } } } 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多http://支持我们。
this.balls[i] = new Ball(x,x,colors[i]);
}
//让当前画布监听 焦点事件
this.addFocusListener(this);
timer = new Timer(delay,this);
timer.start();
}
public void setDelay(int delay){
timer.setDelay(delay);
}
@Override
public void paint(Graphics g) {
for(int i=0;i g.setColor(balls[i].color); //让每个球的坐标变化一下---(x坐标) balls[i].x = balls[i].left ? balls[i].x-10:balls[i].x+10; //当球碰壁时,更改球的方向 if(balls[i].x<=0||balls[i].x>=this.getWidth()-24){ balls[i].left = !balls[i].left;//切换方向 } //让每个球的坐标变化一下---(y坐标) balls[i].y = balls[i].up ? balls[i].y-10:balls[i].y+10; //当球碰壁时,更改球的方向 if(balls[i].y<=0||balls[i].y>=this.getHeight()-22){ balls[i].up = !balls[i].up;//切换方向 } g.fillOval(balls[i].x, balls[i].y, 20, 20); } } @Override public void actionPerformed(ActionEvent e) { //System.out.println("aaa"); repaint();//刷新画布.调用paint(Graphics g) } @Override public void focusGained(FocusEvent e) { timer.stop(); } @Override public void focusLost(FocusEvent e) { timer.restart(); } private static class Ball{ int x,y; boolean up,left; Color color; public Ball(int x, int y, Color color) { this.x = x; this.y = y; this.color = color; up = left = false; } } } 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多http://支持我们。
g.setColor(balls[i].color);
//让每个球的坐标变化一下---(x坐标)
balls[i].x = balls[i].left ? balls[i].x-10:balls[i].x+10;
//当球碰壁时,更改球的方向
if(balls[i].x<=0||balls[i].x>=this.getWidth()-24){
balls[i].left = !balls[i].left;//切换方向
}
//让每个球的坐标变化一下---(y坐标)
balls[i].y = balls[i].up ? balls[i].y-10:balls[i].y+10;
//当球碰壁时,更改球的方向
if(balls[i].y<=0||balls[i].y>=this.getHeight()-22){
balls[i].up = !balls[i].up;//切换方向
}
g.fillOval(balls[i].x, balls[i].y, 20, 20);
}
}
@Override
public void actionPerformed(ActionEvent e) {
//System.out.println("aaa");
repaint();//刷新画布.调用paint(Graphics g)
}
@Override
public void focusGained(FocusEvent e) {
timer.stop();
}
@Override
public void focusLost(FocusEvent e) {
timer.restart();
}
private static class Ball{
int x,y;
boolean up,left;
Color color;
public Ball(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color = color;
up = left = false;
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多http://支持我们。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~