如何在重绘时调整JComponent的大小?

时间:2014-05-07 11:47:33

标签: java swing jcomponent resizable

我有一个扩展jpanel的主类,以及一个使用图形参数的内部类Brick 在屏幕上绘制矩形,我试图在面板大小更改时使这些矩形可调整大小 这些砖在方法paintComponent()中绘制,它们的宽度和高度也分配在 同样的方法,我每30millis调用paintComponent() 砖宽度是面板宽度的百分比,所以我试图在变量中保存旧砖宽度,并通过新旧砖宽度之间的差异增加砖的x坐标但是它不会工作

我有以下代码:

int width,height,brickHeight,brickWidth,tempBWidth,diffrence;
ArrayList<Brick> listOfBricks = new ArrayList<Panel.Brick>();
Timer timer = new Timer(30,this);
boolean bricksFilled,resized;

public Panel() {
    setBackground(Color.BLACK);
    setVisible(true);
    timer.start();
}   //end constructor Panel().

protected void paintComponent(Graphics g) {
    width = getWidth(); height = getHeight();
    setSize(width,height);
    tempBWidth = brickWidth;
    brickHeight = height/20; brickWidth = width/10;
    if(width != 0 && tempBWidth != brickWidth) { diffrence = brickWidth - tempBWidth; }
    super.paintComponent(g);
    if(bricksFilled == false) { fillListOfBricks(); }
    drawBricks(listOfBricks, g);
    diffrence = 0;
}   //end method paintComponent().

public void actionPerformed(ActionEvent event) {
    repaint();
}   //end method actionPerformed().

void fillListOfBricks() {
    for (int row = 0;row < height/2;row += brickHeight+1 ) {
        for (int column = 0;column < width;column += brickWidth+1) {
            if(brickWidth != 0){ 
                listOfBricks.add(new Brick(column,row,true,brickWidth,brickHeight));
                bricksFilled = true;
            }   //end if.
        }   //end inner loop.
    }   //end outer loop.
}   //end method fillListOfBricks().

void drawBricks(ArrayList<Brick> listOfBricks,Graphics g) {
    for (Brick brick:listOfBricks) {
        if(listOfBricks.isEmpty() == false) {
            if (brick.visible) {
                    brick.draw(g);
            }   //end inner if.
        }   //end outer if.
    }   //end loop.
}   //end method drawBricks().

class Brick {
    int x,y;
    int width,height,xDiff,yDiff;
    boolean visible;
    Color randomColor;
    Brick(int x,int y,boolean visible,int width,int height) {
        this.x = x; this.y = y; this.visible = visible;
        this.width = width; this.height = height;
        this.randomColor = getRandomColor();
    }   //end constructor Brick().

    void draw(Graphics g) {  
        if(this.visible) {
            System.out.println(diffrence);
            g.setColor(this.randomColor);
            System.out.println(x+"             "+diffrence);
            g.fillRect(x+diffrence, y, brickWidth,brickHeight);
            g.setColor(Color.WHITE);
            g.drawRect(x-1, y-1, brickWidth+1, brickHeight+1);
        }   // end if block.
    }   //end method draw().

    Color getRandomColor() {
        int R = (int)(255*(Math.random()));
        int G = (int)(255*(Math.random()));
        int B = (int)(255*(Math.random()));
        return new Color(R,G,B);
    }   //end Method getRandomColor.
       }

}

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

  

“我正在尝试在面板大小更改时使这些矩形可调整大小”

将面板作为参数传递给Brick构造函数,然后使用面板的getWidth()getHeight()方法绘制砖块。例如

class Brick {
    private JComponent panel;

    public Brick(JComponent panel, ....) {
        this.panel = panel;
    }

    public void draw(Graphics g) {
        int height = (int)(panel.getHeight() / BRICK_ROWS);
        int width = (int)(panel.getWidth() / BRICK_COLS);
        g.fillRect(x, y, width, height);
    }
}

getWidth()getHeight()会根据面板的当前大小调整砖块的大小。您可以通过将面板大小除以每列/每行的砖数来确定大小。您可能需要稍微重构一下代码。现在这都是假设砖块处于网格状结构

同样不要paintComponent方法中设置组件状态。如果你想(一些奇怪的原因)想要调整面板大小,可以在timer方法中进行。

答案 1 :(得分:0)

将其移交给布局管理器以管理组件的位置和大小。不要手动设置。

包含GridLayout

的示例代码
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Maze {
    private JFrame frame = null;

    private Color[] colors = new Color[] { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW };

    public Maze(int length) {

        frame = new JFrame();

        JPanel panel = new JPanel(new GridLayout(length, length, 5, 5));           

        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                JPanel p2 = new JPanel();
                p2.setBackground(colors[(int) (Math.random() * colors.length)]);

                panel.add(p2);
            }
        }

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Maze Game");
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
    }

}

enter image description here