0

I wrote a checkerboard program (shown below). My problem is that I can't figure out how to center it with resize, and have it resize proportionately.

I added in a short statement. Int resize (shown below) I did something similiar with a previous program regarding a bullseye where I used a radius. I just haven't the slightest clue how to implement that in here.

import java.awt.*;
import javax.swing.JComponent;

 public class CheckerboardComponent extends JComponent {
@Override
public void paintComponent(Graphics g) {

    Graphics2D g2 = (Graphics2D) g;

    g2.setColor(Color.RED);

    int s = 12; 
    int x = s;
    int y = s;

   // int resize = Math.min(this.getHeight(), this.getWidth()) / 8 ;

    for (int i = 0; i < 8; i++) {
        // one row
        for (int j = 0; j < 8; j++) {
            g2.fill(new Rectangle(x, y, 4 * s, 4 * s) );
            x += 4 * s;
        if(g2.getColor().equals(Color.RED)){
            g2.setColor(Color.BLACK);
        }else{
            g2.setColor(Color.RED);
        }              
        }

        x = s; 
        y += 4 * s; 
        if(g2.getColor().equals(Color.RED)){
            g2.setColor(Color.BLACK);
        }else{
            g2.setColor(Color.RED);
        }
    }
 } 

 }

here is a viewer program

import javax.swing.*;


 public class CheckersViewer {

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(430, 450);
    frame.setTitle("Checkerboard");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    CheckerboardComponent component = new CheckerboardComponent();
    frame.add(component);

    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Frightlin
  • 161
  • 1
  • 2
  • 14
  • Check out [Making a robust, resizable Chess GUI](http://stackoverflow.com/questions/21142686/making-a-robust-resizable-chess-gui/21142687#21142687) – camickr Mar 23 '14 at 15:35

1 Answers1

0

Hmm... Here's one idea then, though it probably isn't a good one (I'm also not that good with jComponent and jFrame, so there's probably a better way and a more suited person)

I believe the component object has a built-in method called getSize(). If you can relate the size of the rectangle to the size of the window, then it could be resizable. Obviously there would be more code and arguments, but for example:

public void drawStuff(Component c)
{
       ...
       Dimension size = c.getSize();
       double RectWidth = (size.width)*(.05); 
       ...
}

check this out for more complete examples: http://www.javadocexamples.com/java/awt/Component/getSize().html

And I apologize I can't be of more help.

tethernova
  • 125
  • 5
  • My problem is that when you drag the corner of the frame to make it bigger, the checkerboard just remains the one size. @tethernova – Frightlin Mar 23 '14 at 15:04