Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java使用现有代码创建函数_Java - Fatal编程技术网

Java使用现有代码创建函数

Java使用现有代码创建函数,java,Java,我正在尝试将一些(难看的)代码转换成函数,这样一切都可以更好地管理。然而,我遇到了一个问题,当前代码: int red = card.getColor().getRed(); int green = card.getColor().getGreen(); int blue = card.getColor().getBlue(); Color color = new Color(red, green, blue); int width; int h

我正在尝试将一些(难看的)代码转换成函数,这样一切都可以更好地管理。然而,我遇到了一个问题,当前代码:

    int red = card.getColor().getRed();
    int green = card.getColor().getGreen();
    int blue = card.getColor().getBlue();
    Color color = new Color(red, green, blue);

    int width;
    int height;
    int xOffset;
    int yOffset;
    int border;

    //Whole card
    width = CARD_DIMENSION.width;
    height = CARD_DIMENSION.height;
    xOffset = 0;
    yOffset = 0;
    border = 5;
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            int dx = Math.min(x, width - 1 - x);
            int dy = Math.min(y, height - 1 - y);
            if (dx < border || dy < border) {
                g2d.setColor(new Color(red, green, blue, 255 - Math.min(dx, dy)));
            }
            else {
                g2d.setColor(new Color(red, green, blue, 192 - Math.min(dx, dy)));
            }
            g2d.drawLine(x + xOffset, y + yOffset, x + xOffset, y + yOffset);
        }
    }
int red=card.getColor().getRed();
int green=card.getColor().getGreen();
int blue=card.getColor().getBlue();
颜色=新颜色(红色、绿色、蓝色);
整数宽度;
内部高度;
int-xOffset;
内偏移;
国际边界;
//整张卡片
宽度=卡片尺寸。宽度;
高度=卡片尺寸高度;
xOffset=0;
yOffset=0;
边界=5;
对于(int x=0;x
每次我想画一些新的矩形时,我都要复制这个。乍一看,将其放在函数中并不难,但是以下几行:

            if (dx < border || dy < border) {
                g2d.setColor(new Color(red, green, blue, 255 - Math.min(dx, dy)));
            }
            else {
                g2d.setColor(new Color(red, green, blue, 192 - Math.min(dx, dy)));
            }
if(dx
因为我在那里使用了dx和dy,所以他们很难做到

现在,我提出了一个建议的解决方案,我希望使用匿名内部类并覆盖两个函数:

private abstract class CustomRectangle {
    protected final int width;
    protected final int height;
    protected final int xOffset;
    protected final int yOffset;
    protected final int borderSize;

    public CustomRectangle(final int width, final int height, final int xOffset, final int yOffset, final int borderSize) {
        this.width = width;
        this.height = height;
        this.xOffset = xOffset;
        this.yOffset = yOffset;
        this.borderSize = borderSize;
    }

    abstract void inBorder();

    abstract void outBorder();

    public void draw(Graphics2D g2d) {
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int dx = Math.min(x, width - 1 - x);
                int dy = Math.min(y, height - 1 - y);
                if (dx < borderSize || dy < borderSize) {
                    inBorder();
                }
                else {
                    outBorder();
                }
                g2d.drawLine(x + xOffset, y + yOffset, x + xOffset, y + yOffset);
            }
        }
    }
}
私有抽象类CustomRectangle{
保护最终整数宽度;
保护最终整数高度;
受保护的最终int xOffset;
受保护的最终内偏移量;
受保护的最终整数边框大小;
公共自定义矩形(最终整数宽度、最终整数高度、最终整数xOffset、最终整数yOffset、最终整数边框大小){
这个。宽度=宽度;
高度=高度;
this.xOffset=xOffset;
this.yOffset=yOffset;
this.borderSize=borderSize;
}
抽象无效序();
抽象void outBorder();
公共空心图(Graphics2D g2d){
对于(int x=0;x
但我仍然担心,要用dx和dy(for循环的两个局部变量)输入公式即使不是不可能,也是很困难的

如果有人对一般问题或我自己的方法有任何建议,我将不胜感激,因为有几个基本相同的代码块是不可接受的


关于。

因此将
dx,dy
作为参数传递给该方法…?谢谢,我认为这起到了关键作用:)您使用的是什么IDE?我只是想问一下,Eclipse有一些很棒的快捷键,可以为您自动将内容拆分成不同的方法。看看这篇博文,当您突出显示时,netbeans可以自动将代码拆分成方法,我发现它在Eclipse中进行重构时非常有用,以确保我不会破坏任何东西:)@david99world您是否错过了您所指的链接?