Tiyana
شنبه 10 خرداد 1393, 14:30 عصر
سلام من یه جدول شبیه صفحه شطرنج ساختم که با کلیک روی هر کدوم از خونه های جدول خونه سفید رو مشکی میکنه برای این کار یه کلاس برای کشیدن جدول ساختم و یه کلاس دیگه برای مشکی کردن مربع ها و اکشن موس رو هم توی کلاس جدول تعریف کردم و جدول رو به عنوان یه پنل توی فریم add کردم حالا من دو تا مشکل دارم یکی اینکه چون برای هر کدوم setBounds های مختلف تعریف کردم بعد هر بار کلیک کردن جدول و تمام محتویات فریم کلا جابهجا میشه و مشکل دوم اینکه اگه فریم minimize بشه کل مربع هایی که پر شده بودن میپرن و جدول به حالت اولیه برمیگرده حالا باید برای این دو تا چیکارکرد؟
حالت اولیه:
119734
حالت به هم ریخته بعد یک با کلیک:
119735
کانستراکتور کلاس main:
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch(Exception e)
{
e.printStackTrace();
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(350, 100, 700, 600);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
JButton randomButton = new JButton("ایجاد نقشه تصادفی");
randomButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
}
});
randomButton.setFont(new Font("Times New Roman", Font.PLAIN, 13));
randomButton.setBounds(335, 217, 116, 38);
contentPane.add(randomButton);
JSlider barrierRateSlider = new JSlider();
barrierRateSlider.setMinorTickSpacing(10);
barrierRateSlider.setPaintTicks(true);
barrierRateSlider.setSnapToTicks(true);
barrierRateSlider.setBounds(471, 237, 180, 21);
contentPane.add(barrierRateSlider);
JLabel barrierRateLabel = new JLabel("میزان موانع ایجاد شده");
barrierRateLabel.setFont(new Font("SansSerif", Font.PLAIN, 14));
barrierRateLabel.setBounds(545, 209, 116, 16);
contentPane.add(barrierRateLabel);
MapPanel map=new MapPanel(20);
map.setBounds(22,270,641, 281);
contentPane.add(map);
کلاس جدول:
public void paintComponent(Graphics g)
{
countVerticalSquare=(int) this.getBounds().getHeight()/squareLength;
countHorizonalSequare=(int) this.getBounds().getWidth()/squareLength;
super.paintComponent(g);
curserPosition=new MousePosition();
drawSquare=new DrawSquare(curserPosition);
drawSquare.setBounds(0,0,641, 281);
add(drawSquare);
addMouseListener(new MouseClickHandler());
g.setColor(Color.BLACK);
int xPosition=0;
int yPosition=0;
for(int i=0;i<=countVerticalSquare;i++)
{
g.drawLine( 0, yPosition, (int)this.getBounds().getWidth(),yPosition );
yPosition+=squareLength;
}
for(int i=0;i<=countHorizonalSequare;i++)
{
g.drawLine(xPosition, 0,xPosition, (int) this.getBounds().getHeight());
xPosition+=squareLength;
}
}
public int getLengthSquare() {
return squareLength;
}
public void setLengthSquare(int lengthSquare) {
this.squareLength = lengthSquare;
}
private class MouseClickHandler extends MouseAdapter
{
public void mouseClicked(MouseEvent event)
{
curserPosition.setxPos((event.getX()/MapPanel.squareLength)*MapPanel.squareLength);
curserPosition.setyPos((event.getY()/MapPanel.squareLength)*MapPanel.squareLength);
drawSquare.setPos(curserPosition);
repaint();
}
}
حالت اولیه:
119734
حالت به هم ریخته بعد یک با کلیک:
119735
کانستراکتور کلاس main:
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch(Exception e)
{
e.printStackTrace();
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(350, 100, 700, 600);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
JButton randomButton = new JButton("ایجاد نقشه تصادفی");
randomButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
}
});
randomButton.setFont(new Font("Times New Roman", Font.PLAIN, 13));
randomButton.setBounds(335, 217, 116, 38);
contentPane.add(randomButton);
JSlider barrierRateSlider = new JSlider();
barrierRateSlider.setMinorTickSpacing(10);
barrierRateSlider.setPaintTicks(true);
barrierRateSlider.setSnapToTicks(true);
barrierRateSlider.setBounds(471, 237, 180, 21);
contentPane.add(barrierRateSlider);
JLabel barrierRateLabel = new JLabel("میزان موانع ایجاد شده");
barrierRateLabel.setFont(new Font("SansSerif", Font.PLAIN, 14));
barrierRateLabel.setBounds(545, 209, 116, 16);
contentPane.add(barrierRateLabel);
MapPanel map=new MapPanel(20);
map.setBounds(22,270,641, 281);
contentPane.add(map);
کلاس جدول:
public void paintComponent(Graphics g)
{
countVerticalSquare=(int) this.getBounds().getHeight()/squareLength;
countHorizonalSequare=(int) this.getBounds().getWidth()/squareLength;
super.paintComponent(g);
curserPosition=new MousePosition();
drawSquare=new DrawSquare(curserPosition);
drawSquare.setBounds(0,0,641, 281);
add(drawSquare);
addMouseListener(new MouseClickHandler());
g.setColor(Color.BLACK);
int xPosition=0;
int yPosition=0;
for(int i=0;i<=countVerticalSquare;i++)
{
g.drawLine( 0, yPosition, (int)this.getBounds().getWidth(),yPosition );
yPosition+=squareLength;
}
for(int i=0;i<=countHorizonalSequare;i++)
{
g.drawLine(xPosition, 0,xPosition, (int) this.getBounds().getHeight());
xPosition+=squareLength;
}
}
public int getLengthSquare() {
return squareLength;
}
public void setLengthSquare(int lengthSquare) {
this.squareLength = lengthSquare;
}
private class MouseClickHandler extends MouseAdapter
{
public void mouseClicked(MouseEvent event)
{
curserPosition.setxPos((event.getX()/MapPanel.squareLength)*MapPanel.squareLength);
curserPosition.setyPos((event.getY()/MapPanel.squareLength)*MapPanel.squareLength);
drawSquare.setPos(curserPosition);
repaint();
}
}