0

I am creating a phrasal template word game also known as mad libs. So far I was able to create a console to display the story that was put together based on the input. I was also able to create a background colour however I got stuck when I wanted to add some graphics such as rectangles and squares. How would you suggest I could incorporate that into my program?

Thank you in advance!!

import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.ButtonGroup;

public class MadLibs {

 public static void Action1 () 
  {
    Scanner input = new Scanner(System.in);
    System.out.println("Male Friend:");
    String maleFriend = input.nextLine();
    System.out.println("Adjective:");
    String adjective1 = input.nextLine();
    System.out.println("Past Tense Verb:");
    String pastTenseVerb1 = input.nextLine();
    System.out.println("Past Tense Verb 2:");
    String pastTenseVerb2 = input.nextLine();
    System.out.println("Large Number:");
    String largeNumber = input.nextLine();

    JLabel label = new JLabel("<html>Last summer, my friend "+ maleFriend + " got a job at the " + adjective1 +" Pastry Shop. For the first few<br>"
            + "weeks, he" + pastTenseVerb1 + " the floors, " + pastTenseVerb2 + " on the shelves, and unloaded " + largeNumber + " pound sacks <br>"
            +"of flour from the delivery trucks."
            + "</html>"
            , JLabel.CENTER);
    JFrame window = new JFrame("Please print this");        
    window.setSize(600, 800);
    window.add(label);
    window.getContentPane().setBackground(Color.CYAN);
    window.setVisible(true);

  }

 public static void main(String []args){
     Action1(); 
 }

}

G. Bea
  • 25
  • 1
  • 2
  • 8
  • 1
    I looked at it. It looks nice. http://stackoverflow.com/questions/6118737/how-to-draw-in-jpanel-swing-graphics-java gives an example you might want to look at. – zapl Jun 01 '16 at 02:45
  • 1
    *"How would you suggest I could incorporate that into my program?"* If there is no intention to add 'undo' ability, one good way is to use a `BufferedImage` as a drawing surface as seen in [this answer](http://stackoverflow.com/a/12683632/418556). – Andrew Thompson Jun 01 '16 at 02:54
  • 2
    See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson Jun 01 '16 at 02:55

1 Answers1

3

You'll want to create an extension of the JPanel class.

class drawPanel extends JPanel {
    drawPanel() {
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //Put your graphics code here
    }
}

Then just create the drawPanel in your main class, add your label to it, and add the drawPanel to your JFrame.

JFrame window = new JFrame("Please print this"); 
//Create your custom JPanel class here
drawPanel content = new drawPanel();
//Add the label to the drawPanel instead of the JFrame
content.add(label);       
window.setSize(600, 800);
//Add the drawPanel to the JFrame
window.add(content);
window.getContentPane().setBackground(Color.CYAN);
window.setVisible(true);
  • Thank you for your answer! I tried adding it to my code but it said: No enclosing instance of type MadLibsCulminating is accessible, and the new drawPanel(); was underlined. I created a drawPanel in my main class but that was underlined as well.. What might be the problem? – G. Bea Jun 01 '16 at 18:50
  • If you would like to embed the drawPanel class into your MadLibs class. You'll have to define drawPanel as a static class. This will make drawPanel a nested class. – David Allen Jun 01 '16 at 19:15