Urgent Help Needed in drawing square wave!!!

Dear Guys, does anyone know how to draw square wave? Thank you very much.

[80 byte] By [YoZza] at [2007-11-27 10:16:55]
# 1

import java.awt.*;

import javax.swing.*;

public class SquareWave extends JPanel {

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

int w = getWidth();

int h = getHeight();

int pad = 15;

g2.setPaint(Color.blue);

g2.drawLine(0,h/2,w,h/2);

g2.setPaint(Color.red);

g2.drawLine(pad,h/2,pad,h-pad);

g2.drawLine(pad,h-pad,w/2,h-pad);

g2.drawLine(w/2,h-pad,w/2,pad);

g2.drawLine(w/2,pad,w-pad,pad);

g2.drawLine(w-pad,pad,w-pad,h/2);

}

public static void main(String[] args) {

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

crwooda at 2007-7-28 15:48:26 > top of Java-index,Security,Cryptography...
# 2

I would write a method:

public double squareWave(double x) {

return Math.signum(Math.sin(x));

}

then your paint method would look something like:

public void paint(Graphics g) {

double lastX=0.0, lastY=0.0;

for(double x=0.0; x < 10.0; x++) {

g.drawLine((int)lastX, (int)lastY, (int)x,(int)squareWave(x));

lastX = x;

lastY = squareWave(x);

}

}

it's rough but it should give you an idea. You'll need to scale and translate the wave to see it.

P.S. I don't suggest using doubles as iterators, floating point roundings can cause badness...

And I havn't tested that code.

Cogsya at 2007-7-28 15:48:26 > top of Java-index,Security,Cryptography...
# 3

> I would write a method:

> > public double squareWave(double x) {

>return Math.signum(Math.sin(x));

>

>

> then your paint method would look something like:

>

> > public void paint(Graphics g) {

>double lastX=0.0, lastY=0.0;

> for(double x=0.0; x < 10.0; x++) {

> g.drawLine((int)lastX, (int)lastY,

> (int)x,(int)squareWave(x));

>lastX = x;

> lastY = squareWave(x);

> }

>

>

>

> it's rough but it should give you an idea. You'll

> need to scale and translate the wave to see it.

>

> P.S. I don't suggest using doubles as iterators,

> floating point roundings can cause badness...

> And I havn't tested that code.

Hi Cogsy, i tried ur method but cannot work. I cannot see anything being drawn out. Please help me out. Thanks

This is my code:

import java.awt.*;

import javax.swing.*;

public class SquareWave extends JFrame {

public SquareWave(){

super("squarewave");

setSize(400,400);

setVisible(true);

}

public void paint(Graphics g) {

super.paint(g);

g.setColor(Color.RED);

double lastX=0.0, lastY=0.0;

for(double x=0.0; x < 10.0; x++) {

g.drawLine((int)lastX, (int)lastY, (int)x,(int)squareWave(x));

System.out.println("Answer is " + (int)squareWave(x));

lastX = x;

lastY = squareWave(x);

}

}

public static void main(String[] args) {

SquareWave app =new SquareWave();

app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public double squareWave(double x) {

double a = Math.sin(x);

return Math.signum(Math.sin(x));

}

}

YoZza at 2007-7-28 15:48:26 > top of Java-index,Security,Cryptography...
# 4

you'll need to translate the graph into your viewing area

g.translate(0,100);

remember your y axis goes negative up the screen.

that square wave method will only return 1 or -1. the way paint() uses it means that this size is in pixels so you'll need to scale squarewave() output to make it big enough to see. You may also want to change the period of you square wave by scaling x before calculating sin(x).

the last thing you'll need to do is alter your range of the for loop in paint(). again this is in pixels.

Cogsya at 2007-7-28 15:48:26 > top of Java-index,Security,Cryptography...
# 5

Hey crwood...thanks for the help....

By the way how can make as if the square wave is moving/scrolling like in real time and how do I label the pulse of the square wave for example i wanna assign the number "1" of letter "A" within the perimeter of each of the square pulse.

Thanks...anyone knows also can help me out....

Message was edited by:

YoZz

YoZza at 2007-7-28 15:48:26 > top of Java-index,Security,Cryptography...
# 6

> Hey crwood...thanks for the help....

>

> By the way how can make as if the square wave is

> moving/scrolling like in real time and how do I label

> the pulse of the square wave for example i wanna

> assign the number "1" of letter "A" within the

> perimeter of each of the square pulse.

Heavens to creeping requirements!

petes1234a at 2007-7-28 15:48:26 > top of Java-index,Security,Cryptography...
# 7

import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;

public class ScrollingSquareWave extends JPanel implements Runnable {

Shape wave = new GeneralPath();

GeneralPath waveForm;

int scrollX = 0;

Rectangle r;

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

if(waveForm == null) initWaveForm();

int w = getWidth();

int h = getHeight();

g2.setPaint(Color.blue);

g2.draw(new Line2D.Double(0,h/2,w,h/2));

g2.draw(new Line2D.Double(w/2,0,w/2,h));

g2.setPaint(Color.red);

g2.draw(wave);

drawValues(g2, w, h);

}

private void drawValues(Graphics2D g2, int w, int h) {

// Draw strings.

g2.setPaint(Color.green.darker());

// wave length/period = r.width = 904

// Draw strings at one fourth and three fourths of length.

if(r == null) return;

float sx = scrollX + r.width/8, sy;

int count = 0;

while(r.width > 0 && sx < w) {

String s;

if(count++%2 == 0) {

sy = 50;

s = "1";

} else {

sy = h-40;

s = "-1";

}

if(sx > 0)

g2.drawString(s, sx, sy);

sx += r.width/4;

}

}

private void step() {

int w = getWidth();

int h = getHeight();

int pad = 20;

r = waveForm.getBounds();

// For double period => use half of r.width.

double xScale = (w - 2*pad)/(r.getWidth()/2);

double yScale = (h - 2*pad)/r.getHeight();

double x = scrollX;

double y = h/2.0;

AffineTransform at = AffineTransform.getTranslateInstance(x, y);

at.scale(xScale, -yScale);

wave = at.createTransformedShape(waveForm);

// Set member variable for use in drawValues method.

r = wave.getBounds();

// Append another waveForm to fill out the component view.

if(scrollX + r.width < w) {

at.setToTranslation(scrollX + r.width, y);

at.scale(xScale, -yScale);

((GeneralPath)wave).append(at.createTransformedShape(waveForm), false);

}

repaint();

scrollX = (scrollX - 1 > -r.width) ? scrollX -1 : 0;

}

private void initWaveForm() {

waveForm = new GeneralPath();

// Double period for this component view.

for(int j = 0; j <= 2*360; j++) {

double theta = Math.toRadians(j%360);

double y = Math.signum(Math.sin(theta));

if(j > 0)

waveForm.lineTo(j, (float)y);

else

waveForm.moveTo(j, (float)y);

}

}

public void run() {

boolean scrollingWave = true;

while(scrollingWave) {

try {

Thread.sleep(100);

} catch(InterruptedException e) {

scrollingWave = false;

break;

}

step();

}

}

private void start() {

// Just in case...

while(wave == null) {

try {

Thread.sleep(25);

} catch(InterruptedException e) {

System.exit(1);

}

}

Thread thread = new Thread(this);

thread.setPriority(Thread.NORM_PRIORITY);

thread.start();

}

public static void main(String[] args) {

ScrollingSquareWave test = new ScrollingSquareWave();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test);

f.setSize(500,400);

f.setLocation(200,200);

f.setVisible(true);

test.start();

}

}

crwooda at 2007-7-28 15:48:26 > top of Java-index,Security,Cryptography...