1

I want my java program to draw string "hello" as the input method parameter changes, without losing the previous drawings. In other words the frame has to draw many strings of "Hello" one after the other until the program is forced to stop. Currently it is showing only one word of "hello" with it's new y position changed.

How do I change the program below to draw many words of "hello" s with new y positions? Your help is much appreciated.

thanks

codes

import java.awt.*;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.logging.Level;
import java.util.logging.Logger;
import  javax.swing.*;


public  class test6 extends JPanel  {
      int x=100;
    int y=30;



 String text = null; 



    public static void main (String args[]){

          JFrame frame = new JFrame("Test Game");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      test6 gamePanel = new test6();
      frame.add(gamePanel);
      frame.setSize(400,400);
      frame.setVisible(true);

        }


      @Override
      public  void paintComponent(Graphics g){


        super.paintComponent(g);   


input();


    g.drawString("hello", x, y);

      } 



  void input(){


   try {
              System.out.println("input your command?");
              BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

              text = in.readLine();

        y=y+50;


          } catch (IOException ex) {
              Logger.getLogger(test6.class.getName()).log(Level.SEVERE, null, ex);
          }


 repaint();


}

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Do you want them printed all at once or just printed over time? – MadProgrammer Oct 29 '15 at 23:38
  • `the frame has to draw many strings of "Hello" one after the other until the program is forced to stop.` - Don't do custom painting. Instead just add the text to a JTextArea. Read the Swing tutorial on [Using Text Components](http://docs.oracle.com/javase/tutorial/uiswing/components/text.html) for more information. – camickr Oct 29 '15 at 23:40
  • @camickr You should make that an answer, it would be practical then what the OP seems to trying to do :P – MadProgrammer Oct 29 '15 at 23:48

2 Answers2

2

Iterate through a List<Point> in your implementation of paintComponent(), where each point is the leading baseline of a string. Starting from this example, the following iteration scheme produces an image similar to the one shown below.

    for (Bauble b : queue) {
        g2d.setColor(b.c);
        //g2d.fillOval(b.x, b.y, b.d, b.d);
        g2d.drawString("Hello", b.x, b.y);
    }

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

If you want to preserve what you've drawn, you can use a new buffered image. Create a field:

Image drawing = new BufferedImage(600, 600, BufferedImage.TYPE_INT_ARGB);

Then in your paint components:

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawImage(drawing, 0, 0, this);
}

Then when you want to update the image just draw to it.

public void addText(String s){
    Graphics2D g = drawing.createGraphics();
    g.setColor(Color.WHITE);
    g.drawString(s, x, y);
    g.dispose();
    repaint();
}

Then the BufferedImage will accumulate all of the drawn strings.

matt
  • 10,892
  • 3
  • 22
  • 34
  • Thank you very much . I think I manage to use a buffered Image to do this program. – user5505248 Oct 30 '15 at 21:12
  • My real problem is somewhat complex . A drawing based on user inputs. I want to send input-steams repeatedly to the paintComponent(Graphics g) method. Is this possible? So based on the input the drawing changes. My program has a state object that changes x,y positions of the drawing every time the input is drawn. I think the problem is repaint(). It resets my stateobject.(which shouldn't be the case) System.out.println("input your command?"); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); text = in.readLine(); – user5505248 Oct 30 '15 at 21:15
  • In the way I set it up, you never modify your paintComponents method. You just have methods that modify the image, then you repaint. – matt Oct 31 '15 at 12:13