Hey;
I'm trying to do a hang man game in Java. I newly learned some stuff about programming some GUI using Swing and AWT, but I'm not still clear with it.
The problem is that all the drawing is done by a single function paintComponent() from the JPanel that I should add to the JFrame. The function does not give me flexibility to take some parameters, there is only one parameter, Graphics object.
I want to be able to respond to the users answer/input to the program and draw some primitive shapes such as lines and circles. But I have only one function, and that does not let me pass parameters to it.
I know I got something wrong there but I cant make my way out. So can you tell me what I got wrong and a code snippet to show me that draws a line if user inputs a "line" in a text box and a circle if the user inputs a "circle".
class MyPanel extends JPanel
{
@Override
public void paintComponent(Graphics g)//This function always does the same, no interaction with user inputs
{
Graphics2D graph = (Graphics2D)g;
graph.draw(new Ellipse2D.Double(100, 100, 200, 300));
graph.drawString("Heeeyy!!!", 100, 400);
Rectangle2D rec = new Rectangle2D.Double(100, 100, 50, 50);
graph.draw(rec);
Line2D line = new Line2D.Double(new Point2D.Double(0,0), new Point2D.Double(300, 400));
graph.draw(line);
graph.setPaint(Color.MAGENTA);
graph.fill(rec);
}
}