I have a class Skeleton that makes a Surface and sets the size to 400x400
public class Skeleton extends JFrame {
public Skeleton()
{
initUI();
}
private void initUI()
{
setTitle("");
int height = 400;
int width = 400;
add(new Surface());
setPreferredSize(new Dimension(width + getInsets().left + getInsets().right,
height + getInsets().top + getInsets().bottom));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
//setResizable(false);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run() {
Skeleton sk = new Skeleton();
sk.setVisible(true);
}
});
}
}
Then in the surface class I draw a line from (0,0) to (400,400) and when I run the code the bottom end of the diagonal ends off the panel.
class Surface extends JPanel
{
private void makediag(Graphics g, int size)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.blue);
g2d.drawLine(0, 0, size, size);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
makediag(g, 400);
}
}
What am I doing wrong? Is the size of the panel wrong or are the drawing coordinates different?