EDIT: Never mind I figured out what to do to get the results I wanted. It came to me less than a minute after finishing my post.
Thanks anyway...
Hello all,
I wrote the required program that I must turn in tomorrow. However I have one hangup, I am supposed to simply write a program that displays a 3 x 3 grid. I have that done. But, I was wondering how I can get the 2nd lines, both the horizontal and vertical, to automatically re-orientate themselves when the pane is dragged to a new size.
What I did for the first horizontal and vertical line is this:
g.drawLine(width / 3, 0, width / 3, height); >>> 1st vertical line
g.drawLine(0, height / 3, width, height / 3); >>> 1st horizontal line
I cant think of what to do to the 2nd horizontal and vertical lines to get them to re-orientate on their own, I mean I can set them to a fixed spot but if I drag the pane then they end up as broken lines.
If I could use floating point numbers I would just multiply by 0.33, but to my understanding I can't cast the coordinates, they must be integer.
I have to get past problems like this easier, lol.
My code thus far:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
public class Exercise15_01 extends JFrame {
public Exercise15_01() {
add(new threeByThreePanel());
}
public static void main(String[] args) {
Exercise15_01 frame1 = new Exercise15_01();
frame1.setSize(300, 300);
frame1.setTitle("3 x 3 Grid");
frame1.setLocationRelativeTo(null);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
}
}
class threeByThreePanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
g.setColor(Color.red);
g.drawLine(width / 3, 0, width / 3, height);
g.drawLine(200, 0, 200, 300);
g.setColor(Color.blue);
g.drawLine(0, height / 3, width, height / 3);
g.drawLine(0, 200, 300, 200);
}
}
Improved, and desired, code:
int width = getWidth();
int height = getHeight();
g.setColor(Color.red);
g.drawLine(width/ 3, 0, width/ 3, height);
g.drawLine(width - width/ 3, 0, width - width/ 3, height);
g.setColor(Color.blue);
g.drawLine(0, height / 3, width, height / 3);
g.drawLine(0,height - height /3,width, height - height / 3 );