Border problems

From: JS (dsa._at_asdf.com)
Date: 11/29/04


Date: Mon, 29 Nov 2004 13:55:48 +0100

I am trying to make some borders in my graphic window but I get this error:

IllegalArgumentException: adding a window to a container
  at java.awt.Container.addImpl(Unknown Source)
  at java.awt.Container.add(Unknown Source)
  at MyWriter2.<init>(ConvertATemperature.java:22)
  at CelsiusToFahrenheit2.main(ConvertATemperature.java:60)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
>

Here is the code:

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

class MyWriter2 extends JFrame
{ 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;
    JFrame my_frame = new JFrame();
    my_frame.getContentPane().add(this);
    my_frame.setTitle("MyWriter");
    my_frame.setSize(width, height);
    my_frame.setVisible(true);
  }

  public void paintComponent(Graphics g)
  { makeBorder(g);
    g.setColor(Color.red);
    g.drawString(sentence, x_position, y_position);
  }

  private void makeBorder(Graphics pen)
  { 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)
  { 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)
  { MyWriter2 writer = new MyWriter2(300, 200);
    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);

  }
}

Hope someone can help

JS

-- 
Mvh
Johs


Relevant Pages

  • Re: static field initialised twice
    ... private int id; ... private static int sequence = 1; ... public static void main(Stringargs) { ...
    (comp.lang.java.programmer)
  • Re: BigInteger enhancing
    ... Arne Vajhøj wrote: ... public static void Mul ...
    (comp.lang.java.programmer)
  • Re: Border problems
    ... /* A JFrame does not have a paintComponent method, ... The JFrame is created now in the main. ... private int height; ... private void makeBorder ...
    (comp.lang.java.help)
  • 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)
  • Re: Newbie Java Help!!!
    ... private int id; ... public static void main ... Your getID() should look like: ... In general all identifiers use camel case - the first letter is lower case for variables or methods, upper case for class names, each subsequent word part begins with an upper-case letter, the other letters are lower case. ...
    (comp.lang.java.help)