paintComponent正在执行两次 [英] paintComponent is executing twice

查看:103
本文介绍了paintComponent正在执行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是困扰我,我的代码工作和运行,但是当我去运行它,似乎是循环两次循环,任何人都可以帮助我的逻辑?谢谢...

  package pkgcirc; 
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt。*;
$ b / *
*注意:
*绘制20个圆圈
* radius /位置(x / y / r)全部随机
* if )是在所有值的半径pt(循环遍历)之间,如果它在
*之内,则将其绘制为青色,否则为黑色
*
* /
public class Main扩展JPanel {
int [] radius = new int [3];
int [] xArray = new int [3];
int [] yArray = new int [3];

public Main()
{
Random g = new Random();
setPreferredSize(new Dimension(300,200));
for(int i = 0; i< radius.length; i ++)
{
radius [i] = g.nextInt(50)+1;
xArray [i] = g.nextInt(250)+1;
yArray [i] = g.nextInt(150)+1;


$ b $ public void paintComponent(Graphics page)
{
super.paintComponent(page); (int j = 0; j (int i = 0; i {

{
int xpoint1 = xArray [i] + radius [i];
int ypoint1 = yArray [i] + radius [i];
int xpoint2 = xArray [j] + radius [j];
int ypoint2 = yArray [j] + radius [j];
int radius1 = radius [i];
int radius2 = radius [j];
布尔Collide = circlesCollide(xpoint1,ypoint1,radius1,radius2,xpoint2,ypoint2);

if(i!= j&& Collide == false)
{
page.setColor(Color.cyan);
page.fillOval(xArray [i],yArray [i],radius [i],radius [i]);
System.out.println(false);
}
else {
System.out.println(true);
page.setColor(Color.black);
page.drawOval(xArray [j],yArray [j],radius [j],radius [j]);
}
}
System.out.println(BREAK);



public boolean circlesCollide(double x1,double y1,double r1,double x2,double y2,double r2){
return(distance(x1 ,y1,x2,y2)<=(r1 + r2));


public double distance(double x1,double y1,double x2,double y2){
return Math.sqrt(((x2 - x1)*(x2 - x1 ))+((y2-y1)*(y2-y1)));


public static void main(String [] args)
{
JFrame frame = new JFrame(Circles);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane()。add(new Main());

frame.pack();
frame.setVisible(true);



$ div $解析方案

setPreferredSize() pack()会导致 paintComponent()被调用两次,因为显示需要重新绘制每个调用。



尝试删除 pack()和移动设置大小到底部,如下所示: -

  public Main(){
Random g = new Random );

// setPreferredSize(...); (int i = 0; i< radius.length; i ++){
radius [i] = g.nextInt(50)+1; //注释此行


xArray [i] = g.nextInt(250)+ 1;
yArray [i] = g.nextInt(150)+1;


$ b $ public void paintComponent(Graphics page){
...
}

public static void main (String [] args){
JFrame frame = new JFrame(Circles);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(300,200); //添加这一行

frame.getContentPane()。add(new Main());

// frame.pack(); //评论此行

frame.setVisible(true);



$ b

现在应该可以正常工作了。

This is bothering me, my code works and runs but when I went to run it, it seems to be looping my for loops twice, can anyone help me with my logic? Thanks...

package pkgcirc;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;

/*
* Notes:
* Draw 20 circles
* radius/location (x/y/r) all random
* if (circle) is between radii pt (step thru loop) of all values, if its within ,
*  draw it cyan if it overlaps, else black
*  
*/
public class Main extends JPanel {
    int[] radius = new int [3];
    int[] xArray = new int [3];
    int[] yArray = new int [3];

    public Main()
    {       
        Random g = new Random();
        setPreferredSize (new Dimension(300, 200));
        for(int i = 0; i < radius.length; i++)
        {
            radius[i] = g.nextInt(50)+1;
            xArray[i] = g.nextInt(250)+1;
            yArray[i] = g.nextInt(150)+1;
        }
    }

    public void paintComponent(Graphics page)
    {
        super.paintComponent(page);
        for(int i = 0; i < radius.length; i++)
        {
            for (int j = 0; j < radius.length; j++)
            {
                int xpoint1 = xArray[i]+radius[i];
                int ypoint1 = yArray[i]+radius[i];
                int xpoint2 = xArray[j]+radius[j];
                int ypoint2 = yArray[j]+radius[j];
                int radius1 = radius[i];
                int radius2 = radius[j];
                boolean Collide = circlesCollide(xpoint1, ypoint1, radius1, radius2, xpoint2, ypoint2);

                if (i != j && Collide == false)
                {
                    page.setColor(Color.cyan);
                    page.fillOval(xArray[i] ,yArray[i], radius[i], radius[i]);
                    System.out.println("false");
                }
                else{
                    System.out.println("true");
                    page.setColor(Color.black);
                    page.drawOval(xArray[j] ,yArray[j], radius[j], radius[j]);
                }
            }
            System.out.println("BREAK");    
        }
    }

    public boolean circlesCollide(double x1, double y1, double r1, double x2, double y2, double r2){
        return (distance(x1, y1, x2, y2) <= (r1 + r2));
    }

    public double distance(double x1, double y1, double x2, double y2) {
        return Math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
    }       

    public static void main (String[] args)
    {
        JFrame frame = new JFrame ("Circles");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add (new Main());

        frame.pack();
        frame.setVisible(true);
    }
}

解决方案

Calling setPreferredSize() and pack() will cause paintComponent() to be called twice because the display needs to be redrawn for each call.

Try removing pack() and move set size to the bottom, like this:-

public Main() {
    Random g = new Random();

    //setPreferredSize(...); // commented this line

    for (int i = 0; i < radius.length; i++) {
        radius[i] = g.nextInt(50) + 1;
        xArray[i] = g.nextInt(250) + 1;
        yArray[i] = g.nextInt(150) + 1;
    }
}

public void paintComponent(Graphics page) {
        ...
}

public static void main(String[] args) {
    JFrame frame = new JFrame("Circles");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setSize(300, 200); // added this line

    frame.getContentPane().add(new Main());

    // frame.pack(); // commented this line

    frame.setVisible(true);
}

This should work properly now.

这篇关于paintComponent正在执行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆