views:

80

answers:

4

I never get "paint" written to my command line window when I use Eclipse and Run->cmd to run the program. It works fine if I run System.out.print() from paintComponent in another program. Someone who can help?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUI extends JPanel implements KeyListener, ActionListener
 {
  private static final long serialVersionUID = 1L;
  JFrame frmMain = new JFrame("Kodning");
  JTextField text = new JTextField();
  JPanel pan = new JPanel();
  static char bokstav;
  static int x=10, y=80;
  boolean convert = false;
  String s;
  Timer t = new Timer(10, this);
  public static void main(String[] args)
   {

    @SuppressWarnings("unused")
    GUI g = new GUI();

   }

  public GUI()
   {
    frmMain.setSize(600, 120);
    frmMain.setLayout(new GridLayout(2, 1));
    frmMain.addWindowListener(hornStang());
    frmMain.add(text);
    frmMain.add(pan);
    frmMain.setFocusable(true);
    frmMain.setVisible(true);
    frmMain.addKeyListener(this);
    text.addKeyListener(this);
    pan.addKeyListener(this);
    t.start();
   }
  private static WindowAdapter hornStang() 
   {
    return new WindowAdapter() 
     {
      public void windowClosing(WindowEvent e) 
       {
        System.exit(0);
       }
     };
   }
  public void keyPressed(KeyEvent e)
   {
    if(e.getKeyCode()== KeyEvent.VK_ENTER)
     {
      System.out.println("dechifrera");
      repaint();
      deshiffrera(text.getText());
     }
   }
  public void keyReleased(KeyEvent arg0){}
  public void keyTyped(KeyEvent arg0){}
  public void deshiffrera(String s) 
   {
    s = this.s;
    repaint();
   }
  @override
  public void paintComponent(Graphics g)
   {
    System.out.println("paint");
    for(int i=0;i<s.length();i++)
     {
      bokstav = s.charAt(i);
      switch (bokstav)
       {
        case 'a':nere(g); hoger(g); prick(g, 0); break;
        //en massa case
        default:break;
       }
      x=x+12;
     }
   }
  @Override
  public void actionPerformed(ActionEvent e)
   {
    repaint();
   }
 }
A: 

You probably miss the output of "System.out.println("paint");" ?

GUI-Apps under Windows cant write to the console (they dont have a console, because it would suck if every GUI-App would also open a black window).

There are two java-interpreters under windows: "javaw.exe" which is a GUI-App and silently discards any System.out-writes. And "java.exe" which is a console-app and allows writing to the console. Try to start your program with "java.exe"

Markus Kull
I use eclipse and Run->cmd to run the program. it work fine to run System.out.print() from paintComponent in other program.
demon
+1  A: 

There are a number of issues with your code:

  1. Your GUI panel is not in the frame (shouldn't it be added instead of pan?)
  2. String s is uninitialized, which causes a NullPointerException
  3. paint should be overridden instead of paintComponents
  4. paint should not change the state of the component, because it can be called any time.
  5. etc...
Maurice Perry
paintComponent() is the proper method to override for custom painting. See the Swing tutorial on custom painting: http://download.oracle.com/javase/tutorial/uiswing/painting/index.html
camickr
camickr is right: please disregard point 3.
Maurice Perry
A: 

I use this with AWT (not 100% sure whether it's working in Swing too...)

Graphics g = _yourcomponent_.getGraphics();
if (g != null) {
    _yourcomponent_.paint(g);
    // below the estimated code for Swing:
    _yourcomponent_.paintComponent(g);
}
Tim van Elsloo
You should not use the getGraphics() method to do painting. See my comment to Maurice for a link to the tutorial on custom painting.
camickr
Yeah, I know it's not the official way for repainting a component, but (as far as I know) it's the only **working** way and it doesn't crash.
Tim van Elsloo
+1  A: 

The component must be added to a visible window/frame/component for it's paintComponent to be called.
GUI is only added as a KeyListener but is neither added to the JFrame, nor any other visible component in the code above. There is no reason for calling paintComponent since the component is not being displayed at all.

Carlos Heuberger
Thanks for help, i should known that, Ty.
demon