0

Seen this posted before but I'm trying to iron out the last part of my code.

Essentially I have:

Public Class GPACalc extends Applet
{

    public String firstname, lastname, course1, course2, course3, course4;
    public int credit1, credit2, credit3, credit4, ct;
    public float qp1, qp2, qp3, qp4, gp1, gp2, gp3, gp4, total, gpa;

    public void paint (Graphics g)
    {
        String string_gpa = Float.toString(gpa);
        g.drawString (string_gpa, 100, 150);
        g.drawString (firstname, 200, 250);
    }

    Public static void main (String [] args)
    {

        //Here's where I need help. 

        GPACalc pj = new GPACalc();
        Frame fr = new Frame ("GPA Calculator");
        fr.add(pj);
        fr.setSize(300,300);
        fr.setVisible(true);
        Graphics g = fr.getGraphics();
        pj.paint(g);

See Here's where I need help. I want to reference the public variables firstname, lastname, credit1, gpa, etc. and have them assigned to information from the user. I've already done the

System.out.println("Please enter your name:")

I even got the applet to run with the following towards the end of my method.

I'm just trying to figure out what I'm missing as far as making sure my variables are set correctly and incorporate into the applet right.

Jud
  • 1,324
  • 3
  • 24
  • 47
  • 1
    Please modify your implementation of the `paint` method to add a call to `super.paint(g);` as the first line. Does that solve your problem? Also, you can't really `System.out.println` in a real applet... perhaps you could expand on your precise question. – Elliott Frisch Feb 26 '15 at 01:54
  • 1
    Your code won't compile; `Applet` shouldn't have a `main` method, it's pointless; Don't call `getGraphics` or `paint`, EVER, it's not how painting is done. AWT is way out of date and you should, at the very least, consider using Swing. Start by taking the time to read through [Creating a GUI With JFC/Swing](http://docs.oracle.com/javase/tutorial/uiswing/) – MadProgrammer Feb 26 '15 at 01:56
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Feb 26 '15 at 01:59
  • .. 3) Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the `{}` button at the top of the message posting/editing form. – Andrew Thompson Feb 26 '15 at 01:59
  • BTW - why did you include the Swing tag when this is ..not Swing? – Andrew Thompson Feb 26 '15 at 02:02
  • It's not really an applet moreover just a display of the users name and GPA so most of my classmates are trying to figure out how to just use paint to display the information. – Noctis L Feb 26 '15 at 02:08
  • 1
    Then start with [Creating a GUI With JFC/Swing](http://docs.oracle.com/javase/tutorial/uiswing/), which will show you how you can use a `JTextField` to get input and a `JLabel` to display output... – MadProgrammer Feb 26 '15 at 02:09

0 Answers0