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

JavaApplet在屏幕上打印额外的圆圈,java,applet,Java,Applet,我是新来的。我试图在javaApplet中画一个圆,但不知怎的,在输出中它显示了3个圆。有什么想法吗 您的主要问题是您正在呼叫resize。。。在一个绘画方法中,我们不调用super的绘画方法。话虽如此,我的建议如下: 切勿在顶级窗口(如JApplet或JFrame的绘制方法)内绘制。 而是在顶级窗口中显示的JPanel的paintComponent方法中绘制。 通常先在你的绘画方法中调用super的方法。 比如说 import javax.swing.JApplet; import java.

我是新来的。我试图在javaApplet中画一个圆,但不知怎的,在输出中它显示了3个圆。有什么想法吗


您的主要问题是您正在呼叫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);
    }
}
传递到组件的图形上下文是一个共享资源,这意味着它将包含以前绘制到它的内容

如果在进行任何自定义绘制之前未能调用super.paint,则会阻止小程序执行其设计的许多关键任务,其中之一就是用背景色填充图形上下文

查看和了解更多有关Swing和AWT中绘画工作原理的详细信息

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

更好的解决方案是从JPanel开始,首先重写调用super.paintComponent的paintComponent方法,然后在那里执行自定义绘制。然后你可以把它添加到你想要的容器中

您还应该避免调用任何可能导致使用paint方法从发出重新绘制请求的方法,这将导致无休止的重新绘制循环,这将消耗您的CPU周期并使您的系统瘫痪

例如


考虑到几乎所有的浏览器都在积极地禁用JApplet,以及它在开发过程中带来的复杂性,我还对JApplet的使用提出了质疑

我对此进行了测试,并进行了一些修改,添加了更多的形状,非常感谢。我唯一的其他问题是如何将窗口大小设置为400x300,这样在运行小程序时,它将始终以该大小打开。这就是我试图在第一段代码中使用resize的原因。@adrian applet size是在HTML代码中设置的,而不是在Java代码中设置的。
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);
    }
}
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();
        }

    }
}