简体   繁体   中英

how to resize a JComponent on repaint?

I have a main class that extends jpanel,and an inner class Brick that uses a graphics parameter to draw rectangle on screen,i am trying to make these rectangle resizeable when the panel size change these Bricks are drawn in the method paintComponent() also their width and height are assigned in the same method,I call paintComponent() every 30millis brick width is a percentage of the panel width so i'm trying to save the old brick width in a variable and increasing the x coordinate of the brick by the difference between the old and new brick width but it wouldn't work

I have the following code:

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.
       }

}

thanks for your help

"i am trying to make these rectangle resizeable when the panel size change"

Pass the panel as a parameter to the Brick constructor, and then use the panel's getWidth() and getHeight() methods to draw your bricks. For example

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);
    }
}

The getWidth() and getHeight() will size your bricks according to the current size the panel. You can determine the size by dividing the panel size by the number of bricks per column/row. You will probably have to restructure your code a bit. Now this is all assuming the bricks are in a grid like structure

Also Don't set the component state inside the paintComponent method. If you want to (some weird reason) want to resize the panel, do it in the timer method.

Handover it to layout manager to manage the position and size of components. Don't set it manually.

Sample code with 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);
    }

}

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM