绘图在Java绘制方法中的while循环之后擦除

时间:2017-07-25 20:26:40

标签: java swing paint

我正在开发一个小程序,使用嵌套循环控制的绘制方法绘制堆叠杯。我有一切工作,除了外部While循环完成时,绘图擦除。

该程序包含两个While循环。第一个用于递增绘图的行,第二个用于绘制行中的杯子。

在调试模式下观察程序,在外部While循环的最终执行之后,外部While语句被评估(while(baseLength> 0)为false,然后程序上升到int counter2 = 0,图纸消失,程序退出。

我也尝试使用For循环而不是While来构建它,并且我得到了相同的效果。一旦外部循环被评估为false,绘图就会消失。

看来paint(g)方法做了一些我不太了解导致绘图擦除的东西。有什么想法吗?

import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color;

public class Cups1 extends JFrame {

    /*
     * Declaring instance variables. startX and startY represent the top left coordinates
     * of the first cup block in the bottom row. cupWidth and cupHeight represent the width
     * and height of each row.
     */
    int startX, startY, cupWidth, cupHeight;
    int baseLength; //number of cups in bottom row
    int cupSpacing; //spacing between adjacent cups

        //Constructor calls constructor from JFrame class, argument is window title
        public Cups1()
        {
        super("Example");
        startX = 100;
        startY = 300;
        cupWidth = 25;
        cupHeight = 40;
        baseLength = 7;
        cupSpacing = 6;
        }

        //Paint method fills pattern from bottom to top
        public void paint(Graphics g)
        {
            //Call paint method of JFrame
            super.paint(g);
            //startX and startY are variables to set original point of reference
            //drawX and drawY are local variables for each cup instance
            int drawY = startY, drawX = startX;
            int counter1 = 0;
            while (baseLength > 0)
            {
                int counter2 = 0;
                //Control number of cups in level 
                while (baseLength > counter2) 
                {

                    //Make odd levels red and even levels blue to alternate colors
                    if (baseLength % 2 == 0)
                    {
                        g.setColor(Color.blue);
                    } else {
                        g.setColor(Color.red);
                    }

                    //Draw cup shapes   
                g.fillRect(drawX, drawY, cupWidth, cupHeight);
                drawX = drawX + cupWidth + cupSpacing;
                counter2++;
                }
            //Decrement base length to reduce number of cups on next level   
            baseLength--;

            counter1++;
            //Shift x by 1/2 of the total value of cupWidth and cupSpacing
            drawX = startX + (((cupWidth + cupSpacing)/2) * counter1);
            //Raise height of next level by one cup height
            drawY = startY - (cupHeight * counter1);   
            }       
        }

        public static void main(String[] args) {

            //Create application object with 550 x 550 size and visibility = true
            Cups1 cup = new Cups1();
            cup.setSize(550,550);
            cup.setVisible(true);
            //Close application by clicking "x"
            cup.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

}

1 个答案:

答案 0 :(得分:1)

只要Swing确定应该绘制组件,就会不断重新绘制Swing组件。

组件在绘制时所做的第一件事就是清除它自己的背景,然后再重绘一次。

因此,变量总是需要重置为起始值(在绘制方法中),因此循环可以正确执行。例如,在你的情况下,在绘制方法中需要将“baseLength”变量伤口设置为“7”,否则第一次调用该方法时它被设置为0,因此从不需要进行绘画试。

此外,通过覆盖JPanel(或JComponent)的paintComponent(...)方法来完成自定义绘制。然后将面板添加到框架中。阅读Custom Painting上Swing教程中的部分,了解更多信息和工作示例。