在 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中文网其他相关文章!