import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import static java.lang.System.*;
import java.awt.Font.*;
class MainPanel extends JPanel {
String firstName = new String();
String lastName = new String();
String department = new String();
String age = new String();
String idNumber = new String();
public MainPanel(){
Scanner kb = new Scanner(in);
System.out.println("Welcome to the ID Card Maker.");
System.out.println("What is your first name?");
String firstName = kb.nextLine();
System.out.println("What is your last name?");
String lastName = kb.nextLine();
System.out.println("What is your department?");
String department = kb.nextLine();
System.out.println("How old are you?");
int age = kb.nextInt();
idNumber = department.substring(0, 3) + lastName.length()*age + firstName.substring(0, 2) + lastName.substring(lastName.length()-2);
}
public void paintComponent(Graphics g) {
g.setColor(Color.orange); // these lines of codes
g.drawRect(0, 0, 800, 500); // sets the background of the
g.fillRect(0, 0, 800, 800); // ID Card to the color orange.
Font name;
name = new Font("TimesNewRoman", Font.BOLD, 20);
g.setColor(Color.red);
g.setFont(name);
g.drawString("Name:" + firstName + " " + lastName, 250, 150);
g.setColor(Color.black);
g.drawString("Department: " + department, 250, 200);
g.setColor(Color.green);
g.drawString("Age: " + age, 250, 250);
/* Now use .length() & .substring() to display the information that is requested
on the badge for the Identification in the correct location. */
Font word;
word = new Font("TimesNewRoman", Font.BOLD, 17);
g.setColor(Color.black);
g.setFont(word);
g.drawString("ID Number:" + idNumber, 250, 50);
/*here is yet another example of how to display the department starting at
index 0 and ending at index 3
g.drawString(dept.substring(0,3)*/
}
}
public class Lab13a extends JFrame{
public static void main(String[] args) {
//This is a necessary in order to create the frame window
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Lab13();
}
});
}
public static void Lab13() {
//Sets up the frame
JFrame frame = new JFrame("Graphics");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.getContentPane().setLayout(new BorderLayout());
//Display the window.
frame.pack();
//Adds the graphics panel and sets the size
frame.setSize(new Dimension(800,600));
frame.getContentPane().add(new MainPanel(), BorderLayout.CENTER);
frame.setVisible(true);
}
}
(This is what I have so far, and I need help on getting the names, age, and department visible after I get the user's input.)
I used JCreator for this.
