Drawing Problem

Hello Java2D Friends,

I have a 2-dimensional Char Array. Each row contains a number of Chars which I draw as clique. My intention is during the first iteration, I draw the first row as clique of chars. The second iteration will draw both the first and second in different areas of my GUI, the third iteration will draw both the first, second and third row etc.

I had already implemented some part of the code where I draw each row after another as clique. But, the additive part like I stated above proved to be a problem. Please can someone give me an I idea on how to go about it.

I will be quite grateful.

Thanks,

Jona_T

[661 byte] By [Jona_Ta] at [2007-11-15]
# 1

import java.awt.*;

import java.awt.font.*;

import java.awt.geom.*;

import javax.swing.*;

public class CliqueTest extends JPanel {

String[] data = { "abcd", "efghjklm", "nopqr" };

final int PAD = 20;

final float S = 30f;

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

Font font = g2.getFont().deriveFont(24f);

g2.setFont(font);

FontRenderContext frc = g2.getFontRenderContext();

LineMetrics lm = font.getLineMetrics("0", frc);

float height = lm.getAscent() + lm.getDescent();

float y = PAD + lm.getAscent();

for(int j = 0; j < data.length; j++) {

for(int k = 0; k < j+1; k++) {

drawLine(k, y, g2);

y += PAD + height;

}

y += PAD;

}

}

private void drawLine(int index, float y, Graphics2D g2) {

Font font = g2.getFont();

FontRenderContext frc = g2.getFontRenderContext();

LineMetrics lm = font.getLineMetrics("0", frc);

int width = getWidth();

float xInc = (float)width/(data[index].length()+1);

String[] s = data[index].split("(?<=[\\w\\s])");

float x = xInc;

for(int j = 0; j < s.length; j++) {

if(j < s.length-1) {

g2.setPaint(Color.blue);

g2.draw(new Line2D.Float(x, y, x+xInc, y));

}

Ellipse2D.Float e = new Ellipse2D.Float(x-S/2, y-S/2, S, S);

g2.setPaint(Color.orange);

g2.fill(e);

g2.setPaint(Color.blue);

g2.draw(e);

g2.setPaint(Color.red);

float sw = (float)font.getStringBounds(s[j], frc).getWidth();

float sx = x - sw/2;

float sy = y + lm.getAscent()/2 - lm.getDescent();

g2.drawString(s[j], sx, sy);

x += xInc;

}

}

public static void main(String[] args) {

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(new CliqueTest());

f.setSize(500,500);

f.setLocation(200,200);

f.setVisible(true);

}

}

crwooda at 2007-7-12 > top of java,Security,Cryptography...
# 2
Hello Crwood,Many thanks for the idea. I will try and work on it tonight and give you a feedback.Best regards,Jona_T
Jona_Ta at 2007-7-12 > top of java,Security,Cryptography...
# 3

Hello Crwood,

I tried to reconstruct your code so that it will implement runnable but I just hit the rocks. My idea is to have only "abcd" show at the first iteration in the GUI. The second iteration will show only "abcd" and "efghjklm". The third iteration will then show "abcd", "efghjklm" and "nopqr" only.

Please I need your help once more so that I can move on with the rest coding.

Jona_T

Jona_Ta at 2007-7-12 > top of java,Security,Cryptography...
# 4

import java.awt.*;

import java.awt.font.*;

import java.awt.geom.*;

import javax.swing.*;

public class CliqueTest extends JPanel implements Runnable {

String[] data = { "abcd", "efghjklm", "nopqr" };

int lineCount = 0;

Thread thread = null;

boolean animate = false;

final int DELAY = 2000;

final int PAD = 20;

final float S = 30f;

public void run() {

while(animate) {

try {

Thread.sleep(DELAY);

} catch(InterruptedException e) {

stop();

System.out.println("animation interrupted");

}

lineCount = (lineCount+1 < data.length) ? lineCount+1 : 0;

repaint();

}

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

Font font = g2.getFont().deriveFont(24f);

g2.setFont(font);

FontRenderContext frc = g2.getFontRenderContext();

LineMetrics lm = font.getLineMetrics("0", frc);

float height = lm.getAscent() + lm.getDescent();

float y = PAD + lm.getAscent();

for(int j = 0; j <= lineCount; j++) {

drawLine(j, y, g2);

y += PAD + height;

}

}

private void drawLine(int index, float y, Graphics2D g2) {

Font font = g2.getFont();

FontRenderContext frc = g2.getFontRenderContext();

LineMetrics lm = font.getLineMetrics("0", frc);

int width = getWidth();

float xInc = (float)width/(data[index].length()+1);

String[] s = data[index].split("(?<=[\\w\\s])");

float x = xInc;

for(int j = 0; j < s.length; j++) {

if(j < s.length-1) {

g2.setPaint(Color.blue);

g2.draw(new Line2D.Float(x, y, x+xInc, y));

}

Ellipse2D.Float e = new Ellipse2D.Float(x-S/2, y-S/2, S, S);

g2.setPaint(Color.orange);

g2.fill(e);

g2.setPaint(Color.blue);

g2.draw(e);

g2.setPaint(Color.red);

float sw = (float)font.getStringBounds(s[j], frc).getWidth();

float sx = x - sw/2;

float sy = y + lm.getAscent()/2 - lm.getDescent();

g2.drawString(s[j], sx, sy);

x += xInc;

}

}

protected void startWhenReady() {

Thread wait = new Thread(new Runnable() {

public void run() {

while(!isVisible()) {

try {

Thread.sleep(50);

} catch(InterruptedException e) {

System.out.println("waiting interrupted");

System.exit(1);

}

}

start();

}

});

wait.setPriority(Thread.NORM_PRIORITY);

wait.start();

}

private void start() {

if(!animate) {

animate = true;

thread = new Thread(this);

thread.setPriority(Thread.NORM_PRIORITY);

thread.start();

}

}

private void stop() {

animate = false;

if(thread != null)

thread.interrupt(); // wake up

thread = null;

}

public static void main(String[] args) {

CliqueTest test = new CliqueTest();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test);

f.setSize(500,300);

f.setLocation(200,200);

f.setVisible(true);

test.startWhenReady();

}

}

crwooda at 2007-7-12 > top of java,Security,Cryptography...
# 5
Crwood,I will try and digest your code. Thanks for the great effort. Your Dukes are on the way.Jona_T
Jona_Ta at 2007-7-12 > top of java,Security,Cryptography...