JavaApplet额外圈丝印 [英] JavaApplet extra circles printing on screen
本文介绍了JavaApplet额外圈丝印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这个新的。我想画一个javaApplet了一圈,但不知何故在输出它显示了3圈。任何想法?
进口javax.swing.JApplet中;
进口的java.util。*;
进口java.awt中的*。公共类形状扩展JApplet的
{
公共无效漆(图形页)
{
调整(400300);
随机兰特=新的随机(); //声明常量的大小
最终诠释circleMax = 160,circleMin = 40; //圈最大和最小直径
最终诠释locMaxX = 360,locMaxY = 260;
INT radiusSize = 0,locationx = 0,locationy = 0; //声明变量
radiusSize =(rand.nextInt(circleMax)+ circleMin);
locationx = 20; // rand.nextInt(locMaxX)+ 20;
locationy = 20; // rand.nextInt(locMaxY)+ 20; //绘制圆1
page.drawOval(locationx,locationy,radiusSize,radiusSize);
}
}
解决方案
您主要问题是,我们在调用调整(...)
绘画方法中和不调用超的画法。话虽如此,我的建议是:
- 不要在一个顶层窗口的(如JApplet的或JFrame中的)paint方法中绘图。
- 相反,它是顶级窗口内显示的JPanel的paintComponent方法中绘图。
- 调用超类的方法,你的画法里面,通常先。
例如
进口javax.swing.JApplet中;
进口的java.util。*;
进口java.awt中的*。公共类形状扩展JApplet的{ @覆盖
公共无效的init(){
添加(新ShapesPanel());
}}类ShapesPanel继承JPanel {
私人随机兰特=新的随机();
//声明常量的大小
最终诠释circleMax = 160,circleMin = 40; //圈最大和最小直径
最终诠释locMaxX = 360,locMaxY = 260;
INT radiusSize = 0,locationx = 0,locationy = 0; 公共ShapesPanel(){
radiusSize =(rand.nextInt(circleMax)+ circleMin);
locationx = 20; // rand.nextInt(locMaxX)+ 20;
locationy = 20; // rand.nextInt(locMaxY)+ 20;
} @覆盖
保护无效paintComponent(图形页){
super.paintComponent方法(页);
//绘制圆1
page.drawOval(locationx,locationy,radiusSize,radiusSize);
}
}
I am new in this. I am trying to draw a circle in a javaApplet but somehow in the output it shows 3 circles. Any idea?
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);
}
}
解决方案
Your main problems are that you're calling resize(...)
within a painting method and are not calling the super's painting method. Having said that, my recommendations are:
- Never draw within a top level window's (such as a JApplet or JFrame's) paint method.
- Instead draw within the paintComponent method of a JPanel that is displayed within the top-level window.
- Call the super's method inside of your painting method, usually first.
For example
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);
}
}
这篇关于JavaApplet额外圈丝印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文