Without drawing, why

Hello, I want to paint and draw complicated shapes with graphics 2D.

However I only obtain painting. Where is drawing?

Thanks for any reply.

publicvoid drawSides(Graphics2D g2d){

g2d.setColor(Color.GRAY);

for (int i = 0; i<numberofCells; i++){

GeneralPath p =new GeneralPath();

p.moveTo((float)xCells[i][0],(float)yCells[i][0]);

float r = (float)red[i][id];

float g = (float)green[i][id];

float b = (float)blue[i][id];

for (int j = 1; j><cellsPointsNumber[i]; j++){

p.lineTo((float)xCells[i][j],(float)yCells[i][j]);

}

g2d.setColor(new Color(r, g, b));

g2d.fill(p);

g2d.draw(p);//seems no affection

//p.closePath();

}

}

>

[1571 byte] By [ardmorea] at [2007-11-27 11:43:23]
# 1

You probably just can't see it, since you didn't change the current color between filling the shape and drawing it.

I also notice that you have a commented p.closePath() command after invoking draw(). If you want the path to be closed, then you should close it _before_ you use it. The call to closePath() does not actually draw anything, it merely sets up the path to be closed so that later draw commands that utilize it will draw it as such.

markt1964a at 2007-7-29 17:49:41 > top of Java-index,Security,Cryptography...
# 2

If I want to see it, how?

I have an executable code here.

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.GeneralPath;

import java.awt.geom.*;

import javax.swing.*;

import java.applet.Applet;

import java.util.Random;

public class Draw extends JPanel {

private double xCells[][];

private double yCells[][];

private double red[][];

private double green[][];

private double blue[][];

public void paintComponent(Graphics g){

super.paintComponent( g ); // call superclass's paint method

this.setBackground( Color.WHITE );

Graphics2D g2d = (Graphics2D)g; // cast g to Graphics 2D

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

drawSides(g2d);

g2d.rotate(Math.PI/8.0);

}

public void drawSides(Graphics2D g2d){

g2d.setColor(Color.GRAY);

int id = 0;

Random generator = new Random();

xCells = new double[100][];

yCells = new double[100][];

red= new double[100][];

green = new double[100][];

blue= new double[100][];

for(int i = 0;i<100;i++){

xCells[i] = new double[25];

yCells[i] = new double[25];

red[i]= new double[25];

green[i] = new double[25];

blue[i]= new double[25];

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

xCells[i][j]=generator.nextDouble()*400;

yCells[i][j]=generator.nextDouble()*400;

}

red[i][id] = generator.nextDouble();

green[i][id] = generator.nextDouble();

blue[i][id] = generator.nextDouble();

}

for (int i = 0; i<100; i++){

GeneralPath p = new GeneralPath(GeneralPath.WIND_EVEN_ODD,xCells[i].length);

p.moveTo((float)xCells[i][0],(float)yCells[i][0]);

for (int j = 1; j<25; j++){

p.lineTo((float)xCells[i][j],(float)yCells[i][j]);

}

float r = (float)red[i][id];

float g = (float)green[i][id];

float b = (float)blue[i][id];

p.closePath();

g2d.setPaint(new Color(r, g, b));

g2d.fill(p);

g2d.draw(p);

}

}

public static void main(String argv[]) {

final Draw demo = new Draw();

JFrame f = new JFrame("Java 2D(TM) Demo - Append");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setSize(700,700);

f.add("Center", demo);

f.setVisible(true);

}

}

thanks

ardmorea at 2007-7-29 17:49:41 > top of Java-index,Security,Cryptography...
# 3

import java.awt.*;

import java.awt.geom.GeneralPath;

import javax.swing.*;

import java.util.Random;

public class DrawRx extends JPanel {

private double xCells[][];

private double yCells[][];

private double red[][];

private double green[][];

private double blue[][];

int width = 400;

int height = 400;

public DrawRx() {

this.setBackground( Color.WHITE );

}

// Look up this method signature in JComponent class api */

protected void paintComponent(Graphics g) {

super.paintComponent( g );

//this.setBackground( Color.WHITE ); Don't do this here

Graphics2D g2d = (Graphics2D)g;

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

double theta = Math.PI/8.0;

// Move to upper left corner.

double cos = Math.abs(Math.cos(theta));

double sin = Math.abs(Math.sin(theta));

double rw = width*cos + height*sin;

double rh = height*cos + width*sin;

double x = (rw-width)/2;

double y = (rh-height)/2;

g2d.translate(x,y);

g2d.rotate(theta, width/2, height/2);

drawSides(g2d);

}

public void drawSides(Graphics2D g2d){

g2d.setColor(Color.GRAY);

int id = 0;

Random generator = new Random();

xCells = new double[100][25];

yCells = new double[100][25];

red= new double[100][25];

green = new double[100][25];

blue= new double[100][25];

for(int i = 0;i<100;i++){

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

xCells[i][j]=generator.nextDouble()*width;

yCells[i][j]=generator.nextDouble()*height;

}

red[i][id]= generator.nextDouble();

green[i][id] = generator.nextDouble();

blue[i][id] = generator.nextDouble();

}

for (int i = 0; i<100; i++){

GeneralPath p = new GeneralPath(GeneralPath.WIND_EVEN_ODD,

xCells[i].length);

p.moveTo((float)xCells[i][0],(float)yCells[i][0]);

for (int j = 1; j<25; j++){

p.lineTo((float)xCells[i][j],(float)yCells[i][j]);

}

p.closePath();

float r = (float)red[i][id];

float g = (float)green[i][id];

float b = (float)blue[i][id];

g2d.setPaint(new Color(r, g, b));

g2d.fill(p);

// Adding this next line was suggested by markt1964:

// "change the current color between filling the

// shape and drawing it"

g2d.setPaint(Color.blue);

g2d.draw(p);

}

}

public static void main(String argv[]) {

final DrawRx demo = new DrawRx();

JFrame f = new JFrame("Java 2D(TM) Demo - Append");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setSize(700,700);

f.add("Center", demo);

f.setVisible(true);

}

}

crwooda at 2007-7-29 17:49:41 > top of Java-index,Security,Cryptography...