Block
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class Block extends JPanel implements MouseListener, MouseMotionListener
{
private static final long serialVersionUID = 1L;
private static final int STANDARD_WIDTH = 300, STANDARD_HEIGHT = 70;
private int blockWidth, blockHeight;
private Block upperSibling;
private BlocksPane parent;
public Block(BlocksPane parent){
this.parent = parent;
setBackground(Color.BLACK);
setBlockSize(STANDARD_WIDTH, STANDARD_HEIGHT);
setSize(getBlockWidth(), getBlockHeight());
addMouseListener(this);
addMouseMotionListener(this);
setMouseOverBorder(false);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(parent.getComponentCount() > 1)
setBounds( 0, getUpperSibling() == null ? 0 : getUpperSiblingsHeight() - 1, parent.getWidth() - 1, getBlockHeight() );
else
setSize(parent.getWidth() - 1, parent.getHeight() - 1);
}
public void setBlockSize(int blockWidth, int blockHeight){
this.blockWidth = blockWidth;
this.blockHeight = blockHeight;
}
public int getBlockWidth(){
return blockWidth;
}
public int getBlockHeight(){
return blockHeight;
}
public void setUpperSibling(Block upperSibling){
this.upperSibling = upperSibling;
}
public Block getUpperSibling(){
return upperSibling;
}
public int getUpperSiblingsHeight(){
int allBlocksHeight = 1;
if(getUpperSibling() != null)
allBlocksHeight = getUpperSibling().getBlockHeight() + getUpperSibling().getUpperSiblingsHeight();
return allBlocksHeight;
}
public void setMouseOverBorder(boolean setBorder){
if(setBorder)
setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
else
setBorder(BorderFactory.createLineBorder(Color.WHITE));
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){
setMouseOverBorder(true);
}
public void mouseExited(MouseEvent e){
setMouseOverBorder(false);
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
}
BlocksPane
import java.awt.Graphics;
import java.awt.GridLayout;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JPanel;
public class BlocksPane extends JPanel
{
private static final long serialVersionUID = 1L;
private Block lastAddedBlock = null;
private JPanel drawPanel = null;
private List<Block> children = new LinkedList<Block>();
private int width, height;
private int mX = 0, mY = 0;
public BlocksPane(JPanel drawPanel){
this.drawPanel = drawPanel;
setLayout(new GridLayout());
setBackground(drawPanel.getBackground());
setVisible(true);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
mX = (getParent().getWidth() - getWidth()) / 2;
mY = (getParent().getHeight() - getHeight()) / 2;
setBounds(mX, mY, width, height);
for(Block block : children)
block.paintComponent(block.getGraphics());
}
public void addBlock(Block block){
if(getComponentCount() > 0)
setHeight(getHeight() + block.getHeight());
else{
setWidth(block.getWidth() + 1);
setHeight(block.getHeight() + 1);
}
add(block);
children.add(block);
if(lastAddedBlock != null)
block.setUpperSibling(lastAddedBlock);
lastAddedBlock = block;
}
public void setMatrixLocation(int x, int y){
mX = x;
mY = y;
}
public List<Block> getChildrenList(){
return children;
}
public int getWidth(){
return width;
}
public void setWidth(int width){
this.width = width;
}
public int getHeight(){
return height;
}
public void setHeight(int height){
this.height = height;
}
public void setMatrixSize(int width, int height){
this.width = width;
this.height = height;
}
public int getmX(){
return mX;
}
public void setmX(int x){
mX = x;
}
public int getmY(){
return mY;
}
public void setmY(int y){
mY = y;
}
}
TestWindow
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestWindow extends JFrame
{
private static final long serialVersionUID = 1L;
private static final int BLOCK_COUNT = 4;
private JPanel drawPanel = null;
public TestWindow(){
setTitle("TEST");
setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.BLACK);
setVisible(true);
add(getDrawPanel());
createBlocks();
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new TestWindow();
}
});
}
public JPanel getDrawPanel(){
if(drawPanel == null){
drawPanel = new JPanel(true);
drawPanel.setBackground(Color.ORANGE);
}
return drawPanel;
}
public void createBlocks()
{
BlocksPane matrix = new BlocksPane(drawPanel);
for(int i = 0; i < BLOCK_COUNT; i++)
matrix.addBlock(new Block(matrix));
add(matrix);
}
}
Mam kilka pytań do powyższego kodu. Najpierw chciałbym rozwiązać pierwszą kwestię.
W klasie BlocksPane w metodzie paintComponent używam takiego fragmentu kodu:
for(Block block : children)
block.paintComponent(block.getGraphics());
Nie jest to najlepszy pomysł, ale jeśli nie użyję powyższego kodu, to wewnątrz panelu rysują się tylko 2 bloki, a ostatnie 2 nie są odmalowywane. Nie rozumiem dlaczego. W momencie kiedy wykonywany jest paint klasy BlocksPane powinna też być odrysowana cała jego zawartość, ale nie jest - w związku z czym ostatnie 2 bloki są rysowane poza obrębem panelu i nie są przez to widoczne.