此問題涉及 Java 應用程序,該應用程式旨在在介面上呈現多個從邊框彈起的球。用戶已成功實現了單個球的繪製,但在嘗試添加第二個球時遇到了問題,因為它覆蓋了第一個球。
為了解決此問題,可以建立一個球列表並循環遍歷繪製每個球,但用戶在將兩個球添加到內容窗格時遇到困難。
主要問題是兩個球的放置不透明的組件彼此重疊,可能會導致一個組件遮蓋另一個組件。若要解決此問題:
合併這些的改進程式碼範例修改:
public class Balls { public Balls() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new TheBalls()); frame.setSize(400, 400); frame.setVisible(true); } }); } public class TheBalls extends JPanel { public TheBalls() { setLayout(null); // Randomize the speed and direction... add(new Ball("red", 10 - (int) Math.round((Math.random() * 20)), 10 - (int) Math.round((Math.random() * 20)))); add(new Ball("blue", 10 - (int) Math.round((Math.random() * 20)), 10 - (int) Math.round((Math.random() * 20)))); } } public class Ball extends JPanel implements Runnable { Color color; int diameter; long delay; private int vx; private int vy; public Ball(String ballcolor, int xvelocity, int yvelocity) { if (ballcolor == "red") { color = Color.red; } else if (ballcolor == "blue") { color = Color.blue; } else if (ballcolor == "black") { color = Color.black; } else if (ballcolor == "cyan") { color = Color.cyan; } else if (ballcolor == "darkGray") { color = Color.darkGray; } else if (ballcolor == "gray") { color = Color.gray; } else if (ballcolor == "green") { color = Color.green; } else if (ballcolor == "yellow") { color = Color.yellow; } else if (ballcolor == "lightGray") { color = Color.lightGray; } else if (ballcolor == "magenta") { color = Color.magenta; } else if (ballcolor == "orange") { color = Color.orange; } else if (ballcolor == "pink") { color = Color.pink; } else if (ballcolor == "white") { color = Color.white; } diameter = 30; delay = 100; vx = xvelocity; vy = yvelocity; new Thread(this).start(); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; int x = getX(); int y = getY(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(color); g.fillOval(0, 0, 30, 30); //adds color to circle g.setColor(Color.black); g2.drawOval(0, 0, 30, 30); //draws circle } @Override public Dimension getPreferredSize() { return new Dimension(30, 30); } public void run() { try { // Randamize the location... SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { int x = (int) (Math.round(Math.random() * getParent().getWidth())); int y = (int) (Math.round(Math.random() * getParent().getHeight())); setLocation(x, y); } }); } catch (InterruptedException exp) { exp.printStackTrace(); } catch (InvocationTargetException exp) { exp.printStackTrace(); } while (isVisible()) { try { Thread.sleep(delay); } catch (InterruptedException e) { System.out.println("interrupted"); } try { SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { move(); repaint(); } }); } catch (InterruptedException exp) { exp.printStackTrace(); } catch (InvocationTargetException exp) { exp.printStackTrace(); } } } public void move() { int x = getX(); int y = getY(); if (x + vx < 0 || x + diameter + vx > getParent().getWidth()) { vx *= -1; } if (y + vy < 0 || y + diameter + vy > getParent().getHeight()) { vy *= -1; } x += vx; y += vy; // Update the size and location... setSize(getPreferredSize()); setLocation(x, y); } } }
或者,可以創建一個容器來容納球,其中球本身不是組件,而是作為虛擬概念存在,具有足夠的信息以實現彈跳Surfaces。
需要注意的是,所提供的程式碼片段每個球使用單獨的線程,當球數量增加時,這可能會影響系統資源。為了解決這個可擴展性問題,可以使用單一 BounceEngine 線程來處理所有球的運動。
以上是如何防止 Java 應用程式中彈跳球重疊?的詳細內容。更多資訊請關注PHP中文網其他相關文章!