I need to make a checkers board that has to be also resizeable. At the moment I'm using JPanel and it's method paintComponent to paint the squares. But the problem is with the class test: the squares drawn have to be squares and they cant go out of proportion, so I thought that maybe the solution would be to allow window resizing but so that it would keep its' shape as well (width and heigth would be equal) but I dont know how to solve it. Can somebody help me?

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;

import javax.swing.JPanel;
public class Tahvel extends JPanel {
      public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g; 
            Double ellipse = new Rectangle2D.Double(0,0,getWidth()/10,getHeight()/10);
            g2.draw(ellipse);
            g2.setColor(new Color(25,25,112));
            g2.fill(ellipse);

          }
}



import java.awt.*;
import javax.swing.*;
public class Test extends JFrame {
    Test(){
        setTitle("XXXX");
        getContentPane().add(new Tahvel());
    }

    public static void main(String args[]){
        Test raam = new Test();
        raam.setSize(500,500);
        raam.setVisible(true);

    }
}

Recommended Answers

All 4 Replies

The Frame class has a method you can use to prevent the window being resized.

Thanks, but this would be my last option, because the window has to be resizable. And if an user changes the width or heigth of the windows incorrectly, the board would be quite ugly.

Another choice is a component listener. If the window's resizing will result in an ugly view, set the size to one that would be better.

Thanks! I will try this definitely.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.