【问题标题】:JavaApplet extra circles printing on screenJava Applet 额外的圆圈在屏幕上打印
【发布时间】:2015-09-13 23:51:26
【问题描述】:

我是新来的。我正在尝试在 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);
    }
}

【问题讨论】:

    标签: java applet


    【解决方案1】:

    您的主要问题是您在绘画方法中调用resize(...) 而没有调用超级的绘画方法。话虽如此,我的建议是:

    • 切勿在顶级窗口(例如 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);
        }
    }
    

    【讨论】:

    • 我对此进行了测试并进行了一些修改以添加更多形状,并且效果很好,谢谢。我唯一的其他问题是如何将窗口大小设置为 400x300,因此在运行小程序时它将始终以该大小打开。这就是我尝试在我的第一个代码中使用 resize() 的原因。
    • @adrian 小程序大小在 HTML 代码中设置,而不是在 Java 代码中。
    【解决方案2】:

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

    由于在您进行任何自定义绘制之前未能调用super.paint,您已经阻止了小程序执行它设计的许多关键任务(其中之一是用背景颜色填充Graphics 上下文)。

    请查看Painting in AWT and SwingPerforming Custom Painting,了解有关在 Swing 和 AWT 中如何进行绘画的更多详细信息。

    现在,您可以简单地调用super.paint,但通常不鼓励覆盖像JApplet 这样的顶级容器的paint 方法,它实际上包含一个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 的使用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-30
      相关资源
      最近更新 更多