JavaApplet额外的圆圈在屏幕上打印

我是新手。 我试图在javaApplet中画一个圆圈,但不知何故在输出中它显示3个圆圈。 任何想法?

在此处输入图像描述

import javax.swing.JApplet; import java.util.*; import java.awt.*; public class Shapes extends JApplet { public void paint (Graphics page) { resize(400,300); Random rand = new Random(); // Declare size constants final int circleMax = 160,circleMin = 40; // circle max and min diameter final int locMaxX = 360, locMaxY = 260; int radiusSize = 0, locationx = 0,locationy = 0 ; // Declare variables radiusSize = (rand.nextInt(circleMax)+ circleMin); locationx =20 ;//rand.nextInt(locMaxX)+ 20; locationy =20 ;// rand.nextInt(locMaxY) + 20; // Draw the circle 1 page.drawOval(locationx, locationy, radiusSize,radiusSize); } } 

你的主要问题是你在绘画方法中调用resize(...)而不是调用super的绘画方法。 话虽如此,我的建议是:

  • 永远不要在顶级窗口(例如JApplet或JFrame的)绘制方法中绘制。
  • 而是在顶级窗口中显示的JPanel的paintComponent方法中绘制。
  • 通常首先在绘画方法中调用super的方法。

例如

 import javax.swing.JApplet; import java.util.*; import java.awt.*; public class Shapes extends JApplet { @Override public void init() { add(new ShapesPanel()); } } class ShapesPanel extends JPanel { private Random rand = new Random(); // Declare size constants final int circleMax = 160,circleMin = 40; // circle max and min diameter final int locMaxX = 360, locMaxY = 260; int radiusSize = 0, locationx = 0,locationy = 0 ; public ShapesPanel() { radiusSize = (rand.nextInt(circleMax)+ circleMin); locationx =20 ;//rand.nextInt(locMaxX)+ 20; locationy =20 ;// rand.nextInt(locMaxY) + 20; } @Override protected void paintComponent (Graphics page) { super.paintComponent(page); // Draw the circle 1 page.drawOval(locationx, locationy, radiusSize,radiusSize); } } 

传递给组件的Graphics上下文是一个共享资源,这意味着它将包含以前绘制的内容。

如果在进行任何自定义绘制之前未能调用super.paint ,则会阻止applet执行其设计的许多关键任务(其中之一是使用背景颜色填充Graphics上下文)。

看看AWT和Swing中的 绘画以及执行自定义绘画 ,了解有关绘画如何在Swing和AWT中工作的更多细节。

现在,您可以简单地调用super.paint ,但通常不鼓励覆盖顶级容器(如JAppletpaint方法,它实际上包含一个JRootPane ,其中包含一个可能导致绘制过程中出现问题的contentPane

更好的解决方案是从JPanel开始,覆盖它的paintComponent方法(首先调用super.paintComponent )并在那里执行自定义绘制。 然后,您可以将其添加到您想要的容器中

你还应该避免调用任何可能导致使用paint方法进行重绘请求的方法,这将导致一个永无止境的重绘循环,这将消耗你的CPU周期并使你的系统瘫痪

例如…

小程序

 import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.Random; import javax.swing.JApplet; import javax.swing.JPanel; import test.Shapes.TestPane; public class Shapes extends JApplet { @Override public void init() { super.init(); add(new TestPane()); } public class TestPane extends JPanel { public TestPane() { } @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); Random rand = new Random(); // Declare size constants final int circleMax = 160, circleMin = 40; // circle max and min diameter final int locMaxX = 360, locMaxY = 260; int radiusSize = 0, locationx = 0, locationy = 0; // Declare variables radiusSize = (rand.nextInt(circleMax) + circleMin); locationx = 20;//rand.nextInt(locMaxX)+ 20; locationy = 20;// rand.nextInt(locMaxY) + 20; // Draw the circle 1 g2d.drawOval(locationx, locationy, radiusSize, radiusSize); g2d.dispose(); } } } 

考虑到几乎所有的浏览器都在积极地禁用它们以及它们带来的复杂性,我现在也会质疑JApplet使用情况。