Re: Border problems

From: Andrew Thompson (SeeMySites_at_www.invalid)
Date: 11/29/04


Date: Mon, 29 Nov 2004 13:41:49 GMT

On Mon, 29 Nov 2004 13:55:48 +0100, JS wrote:

> Here is the ..

Altered

>..code:

See comments in the source..

<sscce>
import java.awt.*;
import javax.swing.*;
import java.text.*;

/* A JFrame does not have a paintComponent method, so your
paintComponent was ever called. I have converted it to a JPanel.
The JFrame is created now in the main. */
class MyWriter2 extends JPanel
{
  /* For the sake of readability, don't put statements directly
  after a '{'. Space your code out a bit! */
  private int width;
  private int height;
  private String sentence = "";
  private int x_position = 50;
  private int y_position = 80;

  public MyWriter2(int w, int h)
  { width = w;
    height = h;
    x_position = width / 5;
    y_position = height / 2;
    
    /* But even when this *was* a JFrame, this second frame is
    unnecessary. */
    //JFrame my_frame = new JFrame();
    //my_frame.getContentPane().add(this);
    setSize(width, height);
    setPreferredSize(new Dimension(width, height));
    setVisible(true);
  }

  public void paintComponent(Graphics g)
  {
    // these debug statements are to check the program flow.
    System.out.println( "MF2.pC:" );
    makeBorder(g);
    g.setColor(Color.red);
    g.drawString(sentence, x_position, y_position);
  }

  private void makeBorder(Graphics pen)
  {
    System.out.println( "MF2.mB:" );
    pen.setColor(Color.blue);
    pen.fillRect(0, 0, width, height);
    int border_size = 20;
    int center_rectangle_width = width - (2 * border_size);
    int center_rectangle_height = height - (2 * border_size);
    pen.setColor(Color.white);
    pen.fillRect(border_size, border_size,
                 center_rectangle_width, center_rectangle_height);
  }

  public void writeSentence(String s)
  {
    System.out.println( "MF2.wS:" );
    sentence = s;
    this.repaint();
  }

  public void repositionSentence(int new_x, int new_y)
  { x_position = new_x;
    y_position = new_y;
    this.writeSentence(sentence);
  }
}

class CelsiusToFahrenheit2
{
  public static void main(String[] args)
    {
    /* We create a simple frame here,
    and add a MyWriter2 panel to it. */
    JFrame frame = new JFrame("MyWriter");
    frame.setLayout( new BorderLayout() );

    MyWriter2 writer = new MyWriter2(300, 200);

    frame.getContentPane().add( writer, BorderLayout.CENTER );
    frame.pack();
    frame.setVisible(true);

    String s =
      JOptionPane.showInputDialog("Type an integer Celsius temperature:");
    int c = new Integer(s).intValue();
    double f = ((9.0 / 5.0) * c) + 32;

    DecimalFormat formatter = new DecimalFormat("0.0");
    String h = formatter.format(f);
    writer.writeSentence("Degrees Fahrenheit = " + h);
  }
}
</sscce>

HTH

-- 
Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane


Relevant Pages

  • Re: JUnit
    ... How i can test a method say X with JUnit which returns void. ... Here's a clock class with Tick method that does not return anything.... ... private int minutes; ... public Clock(int mins, int secs) { ...
    (comp.lang.java.programmer)
  • Border problems
    ... I am trying to make some borders in my graphic window but I get this error: ... {private int width; ... private void makeBorder ... {public static void main ...
    (comp.lang.java.help)
  • Re: Newbie Java Help!!!
    ... ShowStudent.java:10: 'void' type not allowed here ... private int id; ... public void setid ... Look at your method getid(), ...
    (comp.lang.java.help)