在JPanel 中創建儘管重繪調用仍保持可見的矩形,而不會因過多而減慢系統速度
利用BufferedImage 作為繪畫表面。
<code class="java">// Relevant JPanel subclass class MyPanel extends JPanel { private BufferedImage canvasImage; // Image for drawing rectangles // Draw a rectangle public void drawRect(int x, int y, int width, int height) { Graphics2D g = canvasImage.createGraphics(); g.setColor(Color.RED); g.fillRect(x, y, width, height); g.dispose(); repaint(); // Redraw the panel with updated canvasImage } // Update view @Override public void paint(Graphics g) { super.paint(g); g.drawImage(canvasImage, 0, 0, null); // Draw the canvasImage on the panel } }</code>
以上是如何在 JPanel 中繪製持久矩形而不降低效能?的詳細內容。更多資訊請關注PHP中文網其他相關文章!