First semester CS student. I'm working on a self-motivated project that was inspired by a previous assignment.
Using swing and awt, I'm trying to create a JFrame and painting inside it, but I can't get the window to scale properly to fit the components I'm adding using pack().
I've commented out the setPreferredSize() line because it does nothing, which is what I'm confused about. The class MyCanvas is the component, right? And inside that component I'm drawing shapes and lines. From what I can see, setPreferredSize() is supposed to be called in the component, not the methods inside the component. I've tried setPreferredSize() in NewGrid(), the component, and inside the paint() method, but no matter what, the window appears one of two ways:
586 x 593 with setPreferredSize() inside NewGrid():
With setPreferredSize() anywhere else in the code:
import java.awt.*;
import javax.swing.*;
public class NewGrid extends JFrame {
private MyCanvas canvas = new MyCanvas();
private int size = 600;
public static void main(String[] args) {
NewGrid newGrid = new NewGrid();
}
public NewGrid() {
add(canvas);
pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
}
private class MyCanvas extends Canvas {
//setPreferredSize(new Dimension(600, 600)); // this line does nothing and I can't figure out why
@Override
public void paint(Graphics g) {
int number = 10;
int spacing = size / number;
g.setColor(Color.darkGray);
g.fillRect(0, 0, size, size); // creates a background square on which I can draw some grid lines
// grid line loop
for (int i = 0; i < number + 1; i++) {
int lineStart = i * spacing;
g.setColor(Color.green);
// Vertical lines
g.drawLine(lineStart, 0, lineStart, size);
// Horizontal lines
g.drawLine(0, lineStart, size, lineStart);
}
}
}
}
//setPreferredSize(new Dimension(600, 600));
// this line does nothing and I can't figure out why
Well for one thing the code won't compile.
That statement would need to be part of your constructor for the class. Sp you need to add the constructor with the above statement.
Also, the pack()
and setVisible(...)
statements should be the last statements executed, AFTER all the components have been added to the frame and the properties of the frame have been set.
Note how the setResizable(...) method affects the frame when placed before/after the pack().
Using swing and awt,
Don't mix Swing and AWT components.
Don't extend Canvas. Instead use JPanel
.
I can't get the window to scale properly to fit the components
Custom painting in Swing is done by extending JPanel
, then you override paintComponent(...)
for the custom painting.
Finally you implement getPreferredSize()
on the panel so the layout managers and the pack() method can do their job.
public class MyPanel extend JPanel
{
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent( g );
// add custom painting code
}
@Override
public Dimension getPreferredSize()
{
return new Dimension(...);
}
}
Read the section from the Swing tutorial on Custom Painting for more information and a working example to get you started.