Drawing in java

trying to draw a variable inside of something

Page 1 of 1

11 Replies - 1197 Views - Last Post: 15 November 2010 - 06:40 PM Rate Topic: -----

#1 Pandamonium  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 26-October 10

Drawing in java

Posted 11 November 2010 - 09:34 AM

Hi, I am trying to modify a previous program that I had created and use the information the user inputs in that program to modify what I am drawing.
my previous program was designed to have the user input data into an array and then print that information back. The program looks like this:

How many players do the Falcons own: 1
Enter the name of Player #1: blah
How many Players do the Steelers own:1
Enter the name of Player #1: bob
Falcons Win/Loss: 0 / 1 games
Number of Players is: 1
blah
Steelers Win/Loss: 1 / 0 games
Number of Players is: 1
bob

what I am trying to do now is to take the information in the arrays and use it in the drawing I have created. I have attached a picture of what I am trying to do:
Attached Image

So far I have had very little luck in trying to use the information in the arrays in my drawing, any help would be greatly appreciated.

Here is my code:

import jpb.*;
import java.awt.*;

public class NFLGameDay6 {

    public static void main(String[] args) {
        //Construct Team Falcons
        NFLTeam6 falcons = new NFLTeam6("Falcons");

        SimpleIO.prompt("How many players do the Falcons own: ");
        String userInput = SimpleIO.readLine().trim();
        int numberOfPlayers = Integer.parseInt(userInput);
        // Prompt user to enter players into the Team
        for (int i = 0; i < numberOfPlayers; i++) {
            SimpleIO.prompt("Enter the name of Player #" + (i + 1) + ": ");
            userInput = SimpleIO.readLine().trim();
            falcons.addAPlayer(userInput);

        }
        //Construct Team Steelers
        NFLTeam6 steelers = new NFLTeam6("Steelers");

        SimpleIO.prompt("How many Players do the Steelers own:");
        userInput = SimpleIO.readLine().trim();
        numberOfPlayers = Integer.parseInt(userInput);

        // prompt user to enter players into the team
        for (int i = 0; i < numberOfPlayers; i++) {
            SimpleIO.prompt("Enter the name of Player #" + (i + 1) + ": ");
            userInput = SimpleIO.readLine().trim();
            steelers.addAPlayer(userInput);

        }

        DrawableFrame df = new DrawableFrame("Field");
        df.show();
        df.setSize(1000, 600);
        Graphics g = df.getGraphicsContext();

        //defining the inside of the field
        int[] xInnerfield = {20, 720, 720, 20};
        int[] yInnerfield = {100, 100, 400, 400};
        //defining octagon
        int[] xoctOuter = {813, 864, 900, 900, 864, 813, 777, 777};
        int[] yoctOuter = {0, 0, 36, 87, 123, 123, 87, 36};
        int[] xoctInner = {814, 863, 895, 895, 863, 814, 782, 782};
        int[] yoctInner = {5, 5, 37, 86, 118, 118, 86, 37};
        //Draw green field area
        g.setColor(new Color(7, 156, 57));
        g.drawRect(5, 5, 740, 500);
        g.fillRect(5, 5, 740, 500);
        // inside lines
        g.setColor(Color.white);
        g.drawLine(120, 100, 120, 400);//goal
        g.drawLine(170, 100, 170, 400);//10
        g.drawLine(220, 100, 220, 400);//20
        g.drawLine(270, 100, 270, 400);//30
        g.drawLine(320, 100, 320, 400);//40
        g.drawLine(370, 100, 370, 400);//50
        g.drawLine(420, 100, 420, 400);//40
        g.drawLine(470, 100, 470, 400);//30
        g.drawLine(520, 100, 520, 400);//20
        g.drawLine(570, 100, 570, 400);//10
        g.drawLine(620, 100, 620, 400);//goal
        //text on field -numbers
        g.setColor(Color.white);
        g.setFont(new Font("Monospaced", Font.BOLD + Font.ITALIC, 20));
        g.drawString("Goal  10  20  30  40  50  40  30  20  10  Goal", 95, 98);
        g.drawString("Goal  10  20  30  40  50  40  30  20  10  Goal", 95, 415);
        //innerfield -border
        g.setColor(Color.white);
        g.drawPolygon(xInnerfield, yInnerfield, xInnerfield.length);
        //endzones
        g.setColor(Color.white);
        g.setFont(new Font("Monospaced", Font.BOLD, 18));
        g.drawString("Endzone", 30, 115); //top left
        g.drawString("Endzone", 30, 395); //bottom left
        g.drawString("Endzone", 630, 115); // top right
        g.drawString("Endzone", 630, 395); // bottom right

        //Draw Octagon
        //draw edge in black
        g.setColor(Color.black);
        g.drawPolygon(xoctOuter, yoctOuter, xoctOuter.length);
        //fill interior with red
        g.setColor(Color.red);
        g.fillPolygon(xoctInner, yoctInner, xoctInner.length);
        // Draw number of players
        g.setColor(Color.white);
        g.setFont(new Font("Helvetica", Font.BOLD, 36));
        g.drawString("3", 13, 76);

        //repaint frame
        df.repaint();



        //Simulate a game
        falcons.lossAgame(steelers);
        System.out.println(falcons);
        System.out.println(steelers);


    }//end of Main method
}//end of class definition





and here is the other part:

public class NFLTeam6 {

    private String[] sPlayerArray;
    private int nTotalNumPlayers;
    private int win;
    private int loss;
    private String TeamName;

    // Constructor
    public NFLTeam6(String eName) {
        win = 0;
        loss = 0;
        sPlayerArray = new String[nTotalNumPlayers];
        TeamName = eName;
        nTotalNumPlayers = 0;
    }

    public void winAgame() {
        win++;
    }

    public void winAgame(NFLTeam6 teamB) {
        win++;
        teamB.lossAgame();
    }

    public void lossAgame() {
        loss++;
    }

    public void lossAgame(NFLTeam6 teamB) {
        loss++;
        teamB.winAgame();
    }

    public int getWinNum() {
        return win;
    }

    public int getLossNum() {
        return loss;
    }

    public String getName() {
        return TeamName;
    }

    public String[] getPlayerName() {
        return sPlayerArray;
    }

    public int getTotalPlayersNum() {
        return nTotalNumPlayers;
    }

    public void addAPlayer(String playerA) {
        nTotalNumPlayers++;
        String[] sPlayerArrayTemp = new String[nTotalNumPlayers];
        int i;
        for (i = 0; i < sPlayerArray.length; i++) {
            sPlayerArrayTemp[i] = sPlayerArray[i];
        }
        sPlayerArrayTemp[i] = playerA;
        sPlayerArray = sPlayerArrayTemp;
    }

    public String toString() {
        String sOutput = TeamName + " Win/Loss: " + win + " / " + loss + " games ";
        sOutput = sOutput + "\n" + "Number of Players is: " + nTotalNumPlayers + "\n";
        for (int i = 0; i < nTotalNumPlayers; i++) {
            sOutput = sOutput + sPlayerArray[i] + "\n";
        }
        return sOutput;
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Drawing in java

#2 Pandamonium  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 26-October 10

Re: Drawing in java

Posted 13 November 2010 - 04:40 PM

I have worked on it some more and now kind of understand how to get the Info I need, however I do not know how to call a paint method in a separate class. I created a method called paint in the NFLTeam6 class. how would I call that method in the NFLGameday6 class?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Drawing in java

Posted 13 November 2010 - 10:27 PM

This is a VERY VERY VERY bad habit

Graphics g = df.getGraphicsContext();

write a class that extends JPanel
overload the paint() or paintComponent() method in your class
do your draing there useing the Graphics object you received as parameter

class Nfl extends JPanel {
...
    public void paintComponent(Graphics g) {
       super.paintComponet(g);
       ... do your drawing here
    }
}



then you can call .repaint() on your Nfl object whenever you think it is required
Was This Post Helpful? 0
  • +
  • -

#4 Pandamonium  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 26-October 10

Re: Drawing in java

Posted 14 November 2010 - 07:06 PM

I am sorry I feel really stupid but I still do not understand what to do.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Drawing in java

Posted 14 November 2010 - 07:12 PM

Your class DrawableFrame
how does it look like ?

This post has been edited by pbl: 14 November 2010 - 07:13 PM

Was This Post Helpful? 0
  • +
  • -

#6 Pandamonium  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 26-October 10

Re: Drawing in java

Posted 14 November 2010 - 07:30 PM

the extent of what I really is not anything. all I have so far is this because I am still very confused about what to do.
import jpb.*;
import java.awt.*;

public class DrawableFrame {
  private static Graphics g;  

  public void Paint() {
      
  }
}


Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Drawing in java

Posted 14 November 2010 - 07:34 PM

View PostPandamonium, on 14 November 2010 - 08:30 PM, said:

the extent of what I really is not anything. all I have so far is this because I am still very confused about what to do.
import jpb.*;
import java.awt.*;

public class DrawableFrame {
  private static Graphics g;  

  public void Paint() {
      
  }
}


What an HORROR !!!
OK I see if I can revamp your code in less that 15 minutes but you really missed the basic stuff here
Was This Post Helpful? 0
  • +
  • -

#8 Pandamonium  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 26-October 10

Re: Drawing in java

Posted 14 November 2010 - 07:37 PM

Quote

What an HORROR !!!
OK I see if I can revamp your code in less that 15 minutes but you really missed the basic stuff here



Haha, Thank you. I know I have missed a bunch of stuff, the problem is I don't know what to do as I am pretty new to a lot of this.

This post has been edited by macosxnerd101: 14 November 2010 - 10:36 PM
Reason for edit:: Fixed quote tag

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10492
  • View blog
  • Posts: 38,876
  • Joined: 27-December 08

Re: Drawing in java

Posted 14 November 2010 - 10:40 PM

You should probably check out some of the tutorials listed in the Java Game Programming Thread. I think they will clear up a lot of misconceptions. :)
Was This Post Helpful? 0
  • +
  • -

#10 Pandamonium  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 26-October 10

Re: Drawing in java

Posted 15 November 2010 - 06:13 PM

I have made a good deal of progress and now understand how to call the method in the other class that paints the field.
Here is the code as it is now
import jpb.*;
import java.awt.*;

public class NFLGameDay6 {

    public static void main(String[] args) {
        //Construct Team Falcons
        NFLTeam6 falcons = new NFLTeam6("Falcons");

        SimpleIO.prompt("How many players do the Falcons own: ");
        String userInput = SimpleIO.readLine().trim();
        int numberOfPlayers = Integer.parseInt(userInput);
        // Prompt user to enter players into the Team
        for (int i = 0; i < numberOfPlayers; i++) {
            SimpleIO.prompt("Enter the name of Player #" + (i + 1) + ": ");
            userInput = SimpleIO.readLine().trim();
            falcons.addAPlayer(userInput);

        }
        //Construct Team Steelers
        NFLTeam6 steelers = new NFLTeam6("Steelers");

        SimpleIO.prompt("How many Players do the Steelers own:");
        userInput = SimpleIO.readLine().trim();
        numberOfPlayers = Integer.parseInt(userInput);

        // prompt user to enter players into the team
        for (int i = 0; i < numberOfPlayers; i++) {
            SimpleIO.prompt("Enter the name of Player #" + (i + 1) + ": ");
            userInput = SimpleIO.readLine().trim();
            steelers.addAPlayer(userInput);

        }
        //Simulate a game
        falcons.lossAgame(steelers);
        System.out.println(falcons);
        System.out.println(steelers);
        falcons.drawField(); //draws field

    }//end of Main method
}//end of class definition

import jpb.*;
import java.awt.*;

public class NFLTeam6 {

    private String[] sPlayerArray;
    private int nTotalNumPlayers;
    private int win;
    private int loss;
    private String TeamName;

    // Constructor
    public NFLTeam6(String eName) {
        win = 0;
        loss = 0;
        sPlayerArray = new String[nTotalNumPlayers];
        TeamName = eName;
        nTotalNumPlayers = 0;
    }

    public void winAgame() {
        win++;
    }

    public void winAgame(NFLTeam6 teamB) {
        win++;
        teamB.lossAgame();
    }

    public void lossAgame() {
        loss++;
    }

    public void lossAgame(NFLTeam6 teamB) {
        loss++;
        teamB.winAgame();
    }

    public int getWinNum() {
        return win;
    }

    public int getLossNum() {
        return loss;
    }

    public String getName() {
        return TeamName;
    }

    public String[] getPlayerName() {
        return sPlayerArray;
    }

    public int getTotalPlayersNum() {
        return nTotalNumPlayers;
    }

    public void addAPlayer(String playerA) {
        nTotalNumPlayers++;
        String[] sPlayerArrayTemp = new String[nTotalNumPlayers];
        int i;
        for (i = 0; i < sPlayerArray.length; i++) {
            sPlayerArrayTemp[i] = sPlayerArray[i];
        }
        sPlayerArrayTemp[i] = playerA;
        sPlayerArray = sPlayerArrayTemp;
    }

    public String toString() {
        String sOutput = TeamName + " Win/Loss: " + win + " / " + loss + " games ";
        sOutput = sOutput + "\n" + "Number of Players is: " + nTotalNumPlayers + "\n";
        for (int i = 0; i < nTotalNumPlayers; i++) {
            sOutput = sOutput + sPlayerArray[i] + "\n";
        }

        return sOutput;

    }

    public void drawField() {
        DrawableFrame df = new DrawableFrame("Field");
        df.show();
        df.setSize(1000, 600);
        Graphics g = df.getGraphicsContext();

        //defining the inside of the field
        int[] xInnerfield = {20, 720, 720, 20};
        int[] yInnerfield = {100, 100, 400, 400};
        //defining octagon
        int[] xoctOuter = {813, 864, 900, 900, 864, 813, 777, 777};
        int[] yoctOuter = {0, 0, 36, 87, 123, 123, 87, 36};
        int[] xoctInner = {814, 863, 895, 895, 863, 814, 782, 782};
        int[] yoctInner = {5, 5, 37, 86, 118, 118, 86, 37};
        //Draw green field area
        g.setColor(new Color(7, 156, 57));
        g.drawRect(5, 5, 740, 500);
        g.fillRect(5, 5, 740, 500);
        // inside lines
        g.setColor(Color.white);
        g.drawLine(120, 100, 120, 400);//goal
        g.drawLine(170, 100, 170, 400);//10
        g.drawLine(220, 100, 220, 400);//20
        g.drawLine(270, 100, 270, 400);//30
        g.drawLine(320, 100, 320, 400);//40
        g.drawLine(370, 100, 370, 400);//50
        g.drawLine(420, 100, 420, 400);//40
        g.drawLine(470, 100, 470, 400);//30
        g.drawLine(520, 100, 520, 400);//20
        g.drawLine(570, 100, 570, 400);//10
        g.drawLine(620, 100, 620, 400);//goal
        //text on field -numbers
        g.setColor(Color.white);
        g.setFont(new Font("Monospaced", Font.BOLD + Font.ITALIC, 20));
        g.drawString("Goal  10  20  30  40  50  40  30  20  10  Goal", 95, 98);
        g.drawString("Goal  10  20  30  40  50  40  30  20  10  Goal", 95, 415);
        //innerfield -border
        g.setColor(Color.white);
        g.drawPolygon(xInnerfield, yInnerfield, xInnerfield.length);
        //endzones
        g.setColor(Color.white);
        g.setFont(new Font("Monospaced", Font.BOLD, 18));
        g.drawString("Endzone", 30, 115); //top left
        g.drawString("Endzone", 30, 395); //bottom left
        g.drawString("Endzone", 630, 115); // top right
        g.drawString("Endzone", 630, 395); // bottom right

        //Draw Octagon
        //draw edge in black
        g.setColor(Color.black);
        g.drawPolygon(xoctOuter, yoctOuter, xoctOuter.length);
        //fill interior with red
        g.setColor(Color.red);
        g.fillPolygon(xoctInner, yoctInner, xoctInner.length);
        // Draw number of players
        g.setColor(Color.blue);
        g.setFont(new Font("Helvetica", Font.BOLD, 36));
        g.drawString(TeamName, 750, 150);;
        String Names = "";
        for (int i = 0; i < nTotalNumPlayers; i++) {
            Names = Names + sPlayerArray[i] + "\n";
        }
        g.drawString(Names, 750, 175);
        //repaint frame
        df.repaint();
    }
}



However I am still having issues with one part, I am trying to have the program paint the user names that have been stored in the array. The problem is it keeps printing them on the same line.
Here is the code for that specific part
g.setColor(Color.blue);
        g.setFont(new Font("Helvetica", Font.BOLD, 36));
        g.drawString(TeamName, 750, 150);
        String Names = "";
        for (int i = 0; i < nTotalNumPlayers; i++) {
            Names = Names + sPlayerArray[i] + "\n";
        }
        g.drawString(Names, 750, 175);

How do I get the program to paint the next name on a different line?
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Drawing in java

Posted 15 November 2010 - 06:26 PM

g.setColor(Color.blue);
        g.setFont(new Font("Helvetica", Font.BOLD, 36));
        int y = 150;
        g.drawString(TeamName, 750, y);
        String Names = "";
        for (int i = 0; i < nTotalNumPlayers; i++) {
            y += 20;
            g.drawString(sPlayerArray[i], 750, y);
        }


Was This Post Helpful? 1
  • +
  • -

#12 Pandamonium  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 26-October 10

Re: Drawing in java

Posted 15 November 2010 - 06:40 PM

Thanks that works perfectly. Thank you for all the help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1