Java小程序本身显示多次 [英] Java applet displays itself multiple times
问题描述
我正在上学,其功能是随机选择六个号码的三个点坐标和它们连接起来组成一个三角形的Java小程序。它是只应该绘制一个三角形,然后找到边的长度。然而,当我把它放在我的网站会多次重绘自身。
我做了另一个小程序,比较简单,只有4选择随机数的坐标来画线。同样的问题。
的重新划分问题似乎当用户移动在屏幕上,例如发生当我滚动或当我调整Eclipse中的小应用程序查看器。我的源$ C $ C被张贴在这里。
我AP preciate任何帮助!谢谢!
进口javax.swing.JApplet中;
进口java.awt中的*。@燮pressWarnings(串行)
公共类LineApplet扩展JApplet的{ / **
*创建的小程序。
* / 静态INT宽度;
INT高度;
公共无效的init(){ 。宽度=的getSize()宽度;
。HEIGHT =的getSize()高度; } 公共静态INT [] randomLine(){
INT [] pointArray =新INT [4];
INT X;
对(INT I = 0; I&下; 4;我++){
X =((int)的(的Math.random()*(宽度/ 10-2)))* 20±10;
pointArray [I] = X;
}
返回pointArray;
} 公共无效漆(图形G){
g.setColor(Color.blue);
INT [] COORDS =新INT [4];
COORDS = randomLine();
g.drawLine(COORDS [0],COORDS [1],COORDS [2],COORDS [3]);
g.drawString(COORDS [0] / 10 +,+ COORDS [1] / 10,COORDS [0],COORDS [1]);
g.drawString(COORDS [2] / 10 +,+ COORDS [3] / 10,COORDS [2],COORDS [3]);
INT midpointx =(COORDS [0] + COORDS [2])/ 2;
INT midpointy =(COORDS [1] + COORDS [3])/ 2;
g.drawString(midpointx / 10 +,+ midpointy / 10,midpointx,midpointy);
}
}
您的每一次计算新的坐标的paint()
被调用。
的paint()
被称为每次小程序窗口大小或重新获得焦点的时间。
要解决,你可以做
INT [] = COORDS新INT [4];
一类的成员变量和移动
COORDS = randomLine();
你的的init()
的方法,这将在初始化时仅调用一次。
附录:
-
总是调用
super.paint(G);覆盖时
的paint()
。 -
有关使用秋千风俗画,preferred方法是扩展了
的JComponent
组件利用使用的paintComponent
。
有关更多查看:表演风俗画
I am making a Java applet for school whose function is to randomly select six numbers for coordinates of three points and connect them to make a triangle. It is only supposed to draw one triangle and find the "length of the sides". However when I put it on my website it will redraw itself multiple times.
I made another applet, simpler, that only selects 4 random numbers for coordinates to draw a line. Same problem.
The redrawing problem seems to happen when the user moves the screen, e.g. when I scroll or when I resize the applet viewer in Eclipse. My source code is posted here.
I appreciate any help! Thanks!
import javax.swing.JApplet;
import java.awt.*;
@SuppressWarnings("serial")
public class LineApplet extends JApplet {
/**
* Create the applet.
*/
static int width;
int height;
public void init() {
width = getSize().width;
height = getSize().height;
}
public static int[] randomLine() {
int[] pointArray = new int[4];
int x;
for (int i = 0; i < 4; i++) {
x = ((int)(Math.random()*(width/10-2)))*20+10;
pointArray[i] = x;
}
return pointArray;
}
public void paint(Graphics g) {
g.setColor(Color.blue);
int[] coords = new int[4];
coords = randomLine();
g.drawLine(coords[0], coords[1], coords[2], coords[3]);
g.drawString(coords[0]/10 + ", " + coords[1]/10, coords[0], coords[1]);
g.drawString(coords[2]/10 + ", " + coords[3]/10, coords[2], coords[3]);
int midpointx = (coords[0] + coords[2])/2;
int midpointy = (coords[1] + coords[3])/2;
g.drawString(midpointx/10 + ", " + midpointy/10, midpointx, midpointy);
}
}
You are calculating new co-ordinates every time paint()
is called.
paint()
is called every time the applet window is resized or regains focus.
To fix, you could make
int[] coords = new int[4];
a class member variable and move
coords = randomLine();
to your init()
method, which will only be called once upon initialization.
Addendum:
Always call
super.paint(g);
when overridingpaint()
.For custom painting using Swing, the preferred approach is to extends a
JComponent
component leveraging the enhanced paint functionality offered by usingpaintComponent
.
For more see: Performing Custom Painting.
这篇关于Java小程序本身显示多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!