In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to achieve small ball collisions and use buttons to control the number in Java. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
First, write a window according to the requirements of the program.
Package three.day; import java.awt.event.*;import javax.swing.*; public class Windows extends JFrame {DrowJPs jp=new DrowJPs (); public void init () {this.setSize (800500); this.setLocationRelativeTo (rootPane); this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); this.setTitle ("Skyfoam, Cold Maple") This.add (jp); this.setVisible (true);} public static void main (String [] args) {Windows win=new Windows (); win.init ();}}
Then write a drawing.
Package three.day;import java.awt.Color;import java.awt.Graphics;import javax.swing.JPanel; public class DrowJPs extends JPanel implements Runnable {int [] x=new int [1000], y=new int [1000], s=new int [1000], xt=new int [1000], yt=new int [1000]; int [] r=new int [1000], g=new int [1000], b=new int [1000]; int num=5; public DrowJPs () {for (int I = 0; I)
< 1000; i++) { x[i]=(int)(Math.random()*450); y[i]=(int)(Math.random()*230); r[i]=(int)(Math.random()*256); g[i]=(int)(Math.random()*256); b[i]=(int)(Math.random()*256); xt[i]=(int)(Math.random()*4+1); yt[i]=(int)(Math.random()*4+1); s[i]=(int)(Math.random()*200+20); } Thread t=new Thread(this); Thread t1=new Thread(this); t.start(); t1.start(); } public void paint(Graphics gr) { super.paint(gr); setBackground(Color.pink); for (int i = 0; i < num; i++) { gr.setColor(new Color(r[i],g[i],b[i])); gr.fillOval(x[i], y[i], s[i], s[i]); } } public void run() { while(true) { for (int i = 0; i < num; i++) { if(x[i]=(790-s[i]))xt[i]*=-1; if(y[i]=(465-s[i]))yt[i]*=-1; x[i]+=xt[i];y[i]+=yt[i]; try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } repaint(); } } }} 开了俩个线程,一个数量大了有点卡 这样运行就ok啦There is another expansion requirement.
Use mouse control to increase the number of balls
How can you just increase it? of course, you have to reduce it.
Then add it to the init function
JButton btn = new JButton ("add a ball"); JButton btn1 = new JButton ("reduce a ball"); btn.setBounds (0,0400,600); btn1.setBounds (400,0400,600); this.add (btn); this.add (btn1); btn.addActionListener (new MyListener ()) Btn1.addActionListener (new MyListener1 ())
Note that the canvas jp must be added at the back of the button
Otherwise, you can't see the canvas.
Just write two more monitors.
Class MyListener implements ActionListener {public void actionPerformed (ActionEvent e) {jp.addnum (0);}} class MyListener1 implements ActionListener {public void actionPerformed (ActionEvent e) {jp.addnum (1);}}
Pass 01 to facilitate the detection on the canvas side
Just add a function to set num on the canvas.
Public void addnum (int I) {if (iTunes 0) num++; else num--;}
Whoo-hoo, it's done, but it's a little annoying that the button flashes from time to time.
And if the ball is reduced to 0, the canvas will be gone.
On "how to achieve small ball collision in Java and use buttons to control the number of this article" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.