2015-09-13 22 views
3

我是新来的。我试图在javaApplet中绘制一个圆圈,但不知何故在输出中显示了3个圆圈。任何想法?JavaApplet额外的圆圈在屏幕上打印

enter image description here

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); 
    } 
} 

回答

3

你的主要问题是你调用一个方法,画中resize(...),并且不调用超的画法。话虽如此,我的建议是:

  • 一个顶层窗口的(如JApplet的或Jframe的)paint方法中从不画。
  • 而是在顶层窗口内显示的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); 
    } 
} 
+0

我测试了这一点,并修改了一些以添加更多形状,它完美地工作,谢谢。我唯一的问题是如何将窗口大小设置为400x300,因此在运行小程序时,它总是以此大小打开。这是我在我的第一个代码中尝试使用resize()的原因。 – Meeeeee

+2

@adrian applet大小是在HTML代码中设置的,而不是在Java代码中设置的。 –

3

被传递到你的组件Graphics上下文是一个共享的资源,这意味着它会包含什么都事先被涂到它。

在你做任何自定义绘画之前未能调用super.paint,你已经阻止了applet执行它设计的许多关键任务(其中之一是用背景色填充Graphics上下文)。

查看Painting in AWT and SwingPerforming Custom Painting了解有关Swing和AWT中绘画的更多细节。现在

,你可以简单地调用super.paint,但它一般不提倡覆盖顶层容器的paint方法像JApplet,它实际上包含了JRootPane,其中包含了contentPane这可能会导致在绘画过程中的问题。

更好的解决方法是以JPanel开头,覆盖它的paintComponent方法(首先调用super.paintComponent)并在那里执行自定义绘画。然后你可以把它添加到你想要的任何容器中

你也应该避免调用任何可能导致重绘请求被绘制方法的方法,这将导致一个永不结束的重绘循环,你的CPU周期,把你的系统,它的膝盖

例如...

Applets

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在所有的日子,考虑几乎所有的浏览器都在积极地禁用它们以及引入它们的复杂性