Hi, I am new to Java.have looked in many topics of this forum, references and could not find the answer. thanks for helping.
I am trying to test a simple procedure to draw a line from (0,0) to (x,x), where x is an integer entered by the user using a showInputDialog.
I am using a JPanel and the paintComponent as suggested by some tutorial (so that, the frame can be moved, resized etc..and the line still remains there).
I have tried for hours how to get the x variable into the g.drawline method. Can anyone help.
Additionally, my frame has its top-left corner centered on the screen, resulting in the frame being shown in the bottom-right quadrant of the PC screen. How do I center it?
thanks for the help
package graphictest;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Draw extends JFrame {
public Draw(){
String inputString = JOptionPane.showInputDialogu(null,"enter input")l
int input;
input = Integer.parseInt(inputString);
add(new NewPanel());
}
public class NewPanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(0, 0,input, input);
}
}
public static void main(String[] args) {
Draw frame = new Draw();
frame.setTitle("Title");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,800);
frame.setVisible(true);
}
}