如何使用fillPolygon转换方块

时间:2017-04-08 00:12:03

标签: java

我必须将这个程序中的方块转换为" cups"在新课程中使用fillPolygon方法,但不能在我的生活中解决这个问题。我必须将每侧的顶部宽度减少5。我在搜索中发现的一切都向我展示了如何制作三角形或更复杂的形状,但没有像这种形状。任何帮助将不胜感激。这是改变后的样子。

Screenshot of 'Cups'

package cs520.hw4.part1;



import javax.swing.JFrame;

import java.awt.Color;
import java.awt.Graphics;
@SuppressWarnings("serial")

public class Cups1 extends JFrame{

int startX, startY, cupWidth, cupHeight;
int baseLength;
int cupSpacing;


public Cups1() 
{

    super("My Squares");  


    startX = 100;
    startY = 300;
    cupWidth = 25;
    cupHeight = 40;
    baseLength = 7;
    cupSpacing = 6;

}


public void paint( Graphics g ) 
{

    super.paint(g);     




    for (int row = 0; row < baseLength; row++) {
        for (int col = 0; col < (baseLength - row); col++)
        {


            if ((row) % 2 == 0){
                g.setColor(Color.RED);}
            else
                g.setColor(Color.BLUE);


            g.fillRect(startX + (row * ((cupWidth + cupSpacing) / 2)) + (col * (cupSpacing + cupWidth)), startY - row*cupHeight, cupWidth, cupHeight);
        }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }




public static void main(String[] args) 
{
    Cups1 app = new Cups1();

    app.setSize(550,550);
    app.setVisible(true);

    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
}


}

2 个答案:

答案 0 :(得分:0)

您可以尝试创建一些圈子并隐藏其中的一部分以实现您的需求

void drawCircleByCenter(Graphics g, int x, int y, int radius){
         //g.setColor(Color.LIGHT_GRAY);
         g.drawOval(x-radius, y-radius, 2*radius, 2*radius);
     }

或者你可以上传你想要画的内容的截图。

答案 1 :(得分:0)

最好创建一个方法,绘制一个带有颜色(红色/蓝色)作为争论的大矩形,然后在内部绘制一个大矩形,然后在其上面绘制一个带有背景颜色的小矩形。然后在main方法中创建调用大数字的逻辑。