Hello there!
I seem to have encountered into a problem which I cannot solve, or understand for that matter.
What I'm trying to do is that I want to paint a rectangle on the screen, and then paint strings as lines onto it.
Something like this:
250yearsbeforevs0.jpg
How it would work:
In the constructor I would have one String parameter, which then would be split into words. These words would then be added to a line if it fits. When the line is full, it would continue to the next line and repeat until all words have been used.
My code:
Basic Window
package Graph; import java.awt.Color; import java.awt.Graphics; public class Porthole { protected static Color frame = new Color(59, 60, 54), back = new Color(27, 77, 62, 150); protected static int SPACE = 4; protected int x, y, width, height; protected int ix, iy, iw, ih; protected int cx, cy, cw, ch; protected boolean visible; public Porthole(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; ix = x + SPACE; iy = y + SPACE; iw = width - 2 * SPACE; ih = height - 2 * SPACE; cx = ix + SPACE; cy = iy + SPACE; cw = iw - 2 * SPACE; ch = ih - 2 * SPACE; visible = false; } public void drawBasic(Graphics g) { g.setColor(back); g.fillRect(x, y, width, height); g.setColor(frame); g.drawRect(x, y, width, height); g.drawRect(ix, iy, iw, ih); } public void drawContentBox(Graphics g){ g.setColor(new Color(120,120,120,100)); g.fillRect(cx, cy, cw, ch); } public void draw(Graphics g) { if (visible) { drawBasic(g); drawContentBox(g); } } public void setVisible(boolean a) { visible = a; } public void move(int x, int y) { this.x = x; this.y = y; ix = x - 2 * SPACE; iy = y - 2 * SPACE; } }
Text Window
package Graph; import java.awt.Graphics; public class Porthole_Text extends Porthole { protected static int tSize = 12; protected String text; protected String[] words; protected String[] lines; public Porthole_Text(int x, int y, int width, int height, String text) { super(x, y, width, height); lines = new String[text.length()]; this.text = text; words = text.split(" "); String t = ""; int id = 0; for (int n = 0; n < lines.length; n++) { while (t.isEmpty() || (t.length() + words[id].length() + 1) * tSize <= cw) { t += words[id]; t += " "; System.out.println(words[id]); id++; } lines[n] += t; System.out.println(t); t = ""; } } @Override public void draw(Graphics g) { if (visible) { drawBasic(g); } } }
Applet
package Graph; import java.applet.Applet; import java.awt.Graphics; import java.awt.Image; public class Appletir extends Applet implements Runnable { Image i; Porthole a; Porthole_Text b; @Override public void init(){ setSize(800,600); a = new Porthole(10, 10, 200, 100); a.setVisible(true); b = new Porthole_Text(0,0,200,200,"Sticks and stones may break my bones, but words can never hurt me."); } @Override public void paint(Graphics g){ a.draw(g); } @Override public void start(){ new Thread(this).start(); } @Override public void run() { while (true){ repaint(); try { Thread.sleep(10); } catch (Exception e) { } } } @Override public void update(Graphics g) { if (i == null || i.getWidth(this) != getWidth() || i.getHeight(this) != getHeight()) { i = createImage(getWidth(), getHeight()); } i.getGraphics().clearRect(0, 0, getWidth(), getHeight()); paint(i.getGraphics()); g.drawImage(i, 0, 0, this); } }
When I run, I get the following output:
Sticks
and
Sticks and
stones
may
stones may
break
my
break my
bones,
but
bones, but
words
can
words can
never
hurt
me.
java.lang.ArrayIndexOutOfBoundsException: 13
at Graph.Porthole_Text.<init>(Porthole_Text.java:22)
at Graph.Appletir.init(Appletir.java:18)
at sun.applet.AppletPanel.run(AppletPanel.java:424)
at java.lang.Thread.run(Thread.java:662)
And the screen does not paint out my test window a.
Could someone please help me with this? And sorry about the unclarities if there are some...Can't seem to really type what I'm thinking...
Thanks in advance!
/Adzox