Please look at my layout.

Hi, please look at my layout management code. I guess something wrong but where is it?

I am drawing a 2D on an applet and add some components.

So the top level is frame, then applet, then components. right?

I have three components: dJSlider, comb and alfa.

One 2D drawing. What is the order of layout. Please check my code.

Thanks.

publicclass Comboxextends JApplet

{

...

private Drawing drawing;

private Container Panel;

publicvoid init()

{

Panel = getContentPane();

drawing =new Drawing();

Panel.add(drawing,"Center");

}

publicvoid SliderFrame()

{

int min = 1, max = 16, inc = 5;

dJSlider =new JSlider(min, max, 6);

}

}

class Drawingextends JPanel{

private JSlider dJSlider;

private JComboBox comb;

private JComboBox alfa;

....

publicvoid paintComponent(Graphics g)

{

...

}

publicvoid ComboBoxDemo(){

GridLayout Layout1 =new GridLayout(3,1,1,1);

comb =new JComboBox(energyId);

alfa =new JComboBox(af);

JPanel cPanel =new JPanel();

cPanel.setLayout(Layout1);

cPanel.add(comb);

cPanel.add(alfa);

cPanel.add(dJSlider);

add(cPanel,BorderLayout.EAST);// NO Change if replaced with BorderLayout.WWST)

publicstaticvoid main(String[] args)

{

JFrame appletFrame =new JFrame("My Applet");

Applet theApplet =new Combox();

appletFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

appletFrame.setSize(800,800);

appletFrame.add(theApplet,"Center");

theApplet.init();

appletFrame.setVisible(true);

}

[3169 byte] By [ardmorea] at [2007-11-27 10:38:32]
# 1

It is generally better to keep the gui control components in your top–level container and

keep the graphic component(Drawing2) free of components. Put it all together in the top–

level container (JApplet).

add(cPanel,BorderLayout.EAST);// NO Change if replaced with BorderLayout.WWST)

The enclosing class here is a JPanel whose default layout manager is FlowLayout. So the

BorderLayout constraint/s has/have no affect.

Here's another way to go about putting things together for this:

// <applet code="ComboxRx" width="500" height="500"></applet>

// use at prompt>appletviewer ComboxRx.java

import java.awt.*;

import javax.swing.*;

public class ComboxRx extends JApplet

{

private Drawing2 drawing;

private JSlider dJSlider;

private JComboBox comb;

private JComboBox alfa;

public void init()

{

Container panel = getContentPane();

drawing = new Drawing2();

// Load drawing/graphic component into center section.

panel.add(drawing,"Center");

// Load ui controls into other areas so they will not

// interfere with the graphic component and vice versa.

panel.add(getSlider(), "South");

panel.add(getComboPanel(), BorderLayout.EAST);

}

private JSlider getSlider()

{

int min = 1, max = 16, inc = 5;

dJSlider = new JSlider(min, max, 6);

// configure slider

// addChangeListener

return dJSlider;

}

private JPanel getComboPanel() {

String[] energyId = { "item 1", "item 2", "item 3" };

String[] af = { "item 1", "item 2", "item 3" };

comb = new JComboBox(energyId);

alfa = new JComboBox(af);

JPanel cPanel = new JPanel(//new GridLayout(3,1,1,1));

new GridBagLayout());

// GridBag will show the comboBoxes at their preferred sizes.

GridBagConstraints gbc = new GridBagConstraints();

gbc.insets = new Insets(2,2,2,2);

gbc.weighty = 1.0;

gbc.gridwidth = GridBagConstraints.REMAINDER;

cPanel.add(comb, gbc);

cPanel.add(alfa, gbc);

return cPanel;

}

/** Convenience method. */

public static void main(String[] args)

{

JFrame appletFrame = new JFrame("My Applet");

// All Swing.

JApplet theApplet = new ComboxRx();

appletFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

appletFrame.setSize(800,500);

appletFrame.add(theApplet,"Center");

theApplet.init();

appletFrame.setVisible(true);

}

}

/** Keep your drawing class free of components; drawing only. */

class Drawing2 extends JPanel

{

protected void paintComponent(Graphics g)

{

super.paintComponent(g);

// custom drawing

}

}

crwooda at 2007-7-28 18:55:08 > top of Java-index,Security,Cryptography...
# 2

I modified my code, but why enent listener are not working?

Thanks

import java.io.*;

import java.util.List;

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.ListIterator;

import java.util.Scanner;

import java.awt.Font;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.BasicStroke;

import java.awt.GradientPaint;

import java.awt.geom.GeneralPath;

import java.awt.AlphaComposite;

import java.awt.Composite;

import java.awt.geom.*;

import java.awt.*;

import java.awt.BorderLayout;

import javax.swing.*;

import java.util.Hashtable;

import java.awt.event.*;

import javax.swing.event.*;

import java.applet.Applet;

public class Combox extends JApplet

{

private Drawing drawing;

private JSlider dJSlider;

private JComboBox comb;

private JComboBox alfa;

private Container panel;

private int id; //PlotEnergy

private String currentPattern;

private String af[] = {"Paint","No Paint"};

private double scale=1;

private int a;

private String energyId[];

public void init()

{

panel = getContentPane();

drawing = new Drawing(a,id,scale);

panel.add(drawing,"Center");

panel.add(getSlider(),"North");

panel.add(getComboPanel(),BorderLayout.EAST);

}

public JSlider getSlider()

{

int min = 1, max = 16, inc = 5;

dJSlider = new JSlider(min, max, 6);

dJSlider.setMajorTickSpacing(5);

dJSlider.setMinorTickSpacing(1);

dJSlider.setSnapToTicks(true);

dJSlider.setLabelTable(getLabelTable(min, max, inc));

dJSlider.setPaintTicks(true);

dJSlider.setPaintLabels(true);

dJSlider.addChangeListener(

new ChangeListener() // anonymous inner class

// handle change in slider value

{

public void stateChanged(ChangeEvent e)

{

int value = ((JSlider)e.getSource()).getValue();

double scale = (value+4)/10.0;repaint();

}

}

);

return dJSlider;

}

private Hashtable getLabelTable(int min, int max, int inc) {

Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>();

for(int j = min; j <= max; j+=inc) {

String s = String.format("%.1f", (j+4)/10.0);

table.put(Integer.valueOf(j), new JLabel(s));

}

return table;

}

public JPanel getComboPanel() {

TestCell p = new TestCell();

p.energyId();

energyId = p.getenergyId();

currentPattern = energyId[0];

comb = new JComboBox(energyId);

JPanel cPanel = new JPanel(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.insets = new Insets(2,2,2,2);

gbc.weighty = 1.0;

gbc.gridwidth = GridBagConstraints.REMAINDER;

comb.setEditable(false);

//comb.addActionListener(this);

comb.setForeground (Color.BLUE);

comb.setBackground (Color.GREEN);

comb.setAlignmentX(Component.LEFT_ALIGNMENT);

comb.addActionListener(

new ActionListener() // anonymous inner class

{

public void actionPerformed(ActionEvent e)

{

JComboBox cb = (JComboBox)e.getSource();

id = cb.getSelectedIndex();

}

}

);

alfa = new JComboBox(af);

alfa.setEditable(false);

alfa.setForeground(Color.BLUE);

alfa.setBackground(Color.GREEN);

alfa.setAlignmentX(Component.LEFT_ALIGNMENT);

alfa.addActionListener(

new ActionListener() // anonymous inner class

{

public void actionPerformed(ActionEvent e)

{

JComboBox cc = (JComboBox)e.getSource();

a = cc.getSelectedIndex();

}

}

);

cPanel.add(comb);

cPanel.add(alfa);

return cPanel;

}

}

class Drawing extends JPanel

{

final int TRANSITION = 100; // Transition x,y coordinates of cells.

private double x1, y1, x2, y2; // to plot lines from (x1,y1) to (x2,y2)

private double xCells[][]; // Every cell's points's x coordinates array

private double yCells[][]; // Every cell's points's y coordinates array

private double rgb[][]; // Color red, green, blue for each point in each cell

private String energyId[];

private double red[][];

private double green[][];

private double blue[][];

private double lineWidth;

private int sideId;

private int sidesTotalNumber; // Total sides number for all cells

private int rgbLength; // Total rgb color group number for all cells at each point

private int cellsPointsNumber[]; // point numbers of each cell.(each cell has how many points)

private int numberofCells;

private int numberofsets;

int a,id;

double scale;

Drawing(int a, int id, double scale)

{this.a = a;

this.id = id;

this.scale = scale;

}

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

getDrawingData();

if (a==0)

drawPolygon(g2d);

drawSides(g2d);

}

// end method paintComponent

public void getDrawingData()

{

TestCell p = new TestCell();

p.cellData();

p.rgbs();

xCells = p.getxCells();

yCells = p.getyCells();

sidesTotalNumber = p.getsidesTotalNumber();

red = p.getred();

green = p.getgreen();

blue = p.getblue();

rgbLength = p.getrgbLength();

cellsPointsNumber = p.getcellsPointsNumber();//array: each cell has how many points

numberofCells = p.getnumberofCells();

numberofsets = p.getnumberofsets();

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

{

for(int j = 0; j<cellsPointsNumber[i]; j++)

{

xCells[i][j] = (xCells[i][j])*scale+TRANSITION;

yCells[i][j] = (yCells[i][j])*scale+TRANSITION;

}

}

}

public void drawSides(Graphics2D g2d)

{

g2d.setColor(Color.GRAY);

g2d.setStroke(new BasicStroke((float)lineWidth));

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

{

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

{

x1 = xCells[i][j];

y1 = yCells[i][j];

x2 = xCells[i][j+1];

y2 = yCells[i][j+1];

Line2D line = new Line2D.Double(x1,y1,x2,y2);

g2d.draw(line);// draw Sidess here

}

}

}

public void drawPolygon(Graphics2D g2d)

{

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

{

Polygon poly = new Polygon();

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

// red for ith cell,id is from 0 to numberofsets-1

// if id=0, means "Mixture"

float g = (float)green[i][id]; // green for ith cell

float b = (float)blue[i][id]; // blue for ith cell

List><Integer> xList = new ArrayList<Integer>();

List<Integer> yList = new ArrayList<Integer>();

for (int j = 0; j<cellsPointsNumber[i]; j++)

{

xList.add((int)(xCells[i][j]));

yList.add((int)(yCells[i][j]));

}

Integer[] x = xList.toArray(new Integer[xList.size()]);

Integer[] y = yList.toArray(new Integer[yList.size()]);

for (int s = 0; s><xList.size(); s++)

{

poly.addPoint(x[s],y[s]);

}

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

g2d.fillPolygon(poly);

}

}

public static void main(String[] args)

{

JFrame appletFrame = new JFrame("My Applet");

Applet theApplet = new Combox();

appletFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

appletFrame.setSize(800,800);

appletFrame.add(theApplet,"Center");

theApplet.init();

appletFrame.setVisible(true);

}

} // end class Drawing

>

ardmorea at 2007-7-28 18:55:08 > top of Java-index,Security,Cryptography...
# 3

Looks like you need to send the newly–selected value of id/a to the Drawing class,

maybe with the help of a setXXX method.

public class Combox...

...

public void actionPerformed(ActionEvent e) {

...

id = cb.getSelectedIndex();

// send this to the Drawing class

drawing.setId(id);

}

...

public void actionPerformed(ActionEvent e) {

...

a = cc.getSelectedIndex();

// pass along to drawing

}

...

}

class Drawing...

...

public void setId(int id) {

this.id = id;

repaint();

}

}

crwooda at 2007-7-28 18:55:08 > top of Java-index,Security,Cryptography...
# 4

I did pass a new scale to the drawing, but why wrong?

import java.io.*;

import java.util.List;

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.ListIterator;

import java.util.Scanner;

import java.awt.Font;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.BasicStroke;

import java.awt.GradientPaint;

import java.awt.geom.GeneralPath;

import java.awt.AlphaComposite;

import java.awt.Composite;

import java.awt.geom.*;

import java.awt.*;

import java.awt.BorderLayout;

import javax.swing.*;

import java.util.Hashtable;

import java.awt.event.*;

import javax.swing.event.*;

import java.applet.Applet;

public class Combox extends JApplet

{

private Drawing drawing;

private JSlider dJSlider;

private JComboBox comb;

private JComboBox alfa;

private Container panel;

private int id; //PlotEnergy

private String currentPattern;

private String af[] = {"Paint","No Paint"};

private double scale;

private int a;

private String energyId[];

public void init()

{

panel = getContentPane();

drawing = new Drawing(a,id,scale);

panel.add(drawing,"Center");

panel.add(getSlider(),"North");

panel.add(getComboPanel(),BorderLayout.EAST);

}

public JSlider getSlider()

{

int min = 1, max = 16, inc = 5;

dJSlider = new JSlider(min, max, 6);

dJSlider.setMajorTickSpacing(5);

dJSlider.setMinorTickSpacing(1);

dJSlider.setSnapToTicks(true);

dJSlider.setLabelTable(getLabelTable(min, max, inc));

dJSlider.setPaintTicks(true);

dJSlider.setPaintLabels(true);

dJSlider.addChangeListener(

new ChangeListener() // anonymous inner class

// handle change in slider value

{

public void stateChanged(ChangeEvent e)

{

int value = ((JSlider)e.getSource()).getValue();

double scale = (value+4)/10.0;

}

}

);drawing.setscale(scale);

return dJSlider;

}

private Hashtable getLabelTable(int min, int max, int inc) {

Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>();

for(int j = min; j <= max; j+=inc) {

String s = String.format("%.1f", (j+4)/10.0);

table.put(Integer.valueOf(j), new JLabel(s));

}

return table;

}

public JPanel getComboPanel() {

TestCell p = new TestCell();

p.energyId();

energyId = p.getenergyId();

currentPattern = energyId[0];

comb = new JComboBox(energyId);

JPanel cPanel = new JPanel(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.insets = new Insets(2,2,2,2);

gbc.weighty = 1.0;

gbc.gridwidth = GridBagConstraints.REMAINDER;

comb.setEditable(false);

//comb.addActionListener(this);

comb.setForeground (Color.BLUE);

comb.setBackground (Color.GREEN);

comb.setAlignmentX(Component.LEFT_ALIGNMENT);

comb.addActionListener(

new ActionListener() // anonymous inner class

{

public void actionPerformed(ActionEvent e)

{

JComboBox cb = (JComboBox)e.getSource();

id = cb.getSelectedIndex();

}

}

);

alfa = new JComboBox(af);

alfa.setEditable(false);

alfa.setForeground(Color.BLUE);

alfa.setBackground(Color.GREEN);

alfa.setAlignmentX(Component.LEFT_ALIGNMENT);

alfa.addActionListener(

new ActionListener() // anonymous inner class

{

public void actionPerformed(ActionEvent e)

{

JComboBox cc = (JComboBox)e.getSource();

a = cc.getSelectedIndex();

}

}

);

cPanel.add(comb);

cPanel.add(alfa);

return cPanel;

}

}

class Drawing extends JPanel

{

final int TRANSITION = 100; // Transition x,y coordinates of cells.

private double x1, y1, x2, y2; // to plot lines from (x1,y1) to (x2,y2)

private double xCells[][]; // Every cell's points's x coordinates array

private double yCells[][]; // Every cell's points's y coordinates array

private double rgb[][]; // Color red, green, blue for each point in each cell

private String energyId[];

private double red[][];

private double green[][];

private double blue[][];

private double lineWidth;

private int sideId;

private int sidesTotalNumber; // Total sides number for all cells

private int rgbLength; // Total rgb color group number for all cells at each point

private int cellsPointsNumber[]; // point numbers of each cell.(each cell has how many points)

private int numberofCells;

private int numberofsets;

int a,id;

double scale;

Drawing(int a, int id, double scale)

{this.a = a;

this.id = id;

this.scale = scale;

}

public void setscale(double scale)

{

this.scale = scale; System.out.println(scale);

repaint();

}

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

getDrawingData();

if (a==0)

drawPolygon(g2d);

drawSides(g2d);

}

// end method paintComponent

public void getDrawingData()

{

TestCell p = new TestCell();

p.cellData();

p.rgbs();

xCells = p.getxCells();

yCells = p.getyCells();

sidesTotalNumber = p.getsidesTotalNumber();

red = p.getred();

green = p.getgreen();

blue = p.getblue();

rgbLength = p.getrgbLength();

cellsPointsNumber = p.getcellsPointsNumber();//array: each cell has how many points

numberofCells = p.getnumberofCells();

numberofsets = p.getnumberofsets();

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

{

for(int j = 0; j<cellsPointsNumber[i]; j++)

{

xCells[i][j] = (xCells[i][j])*scale+TRANSITION;

yCells[i][j] = (yCells[i][j])*scale+TRANSITION;

}

}

}

public void drawSides(Graphics2D g2d)

{

g2d.setColor(Color.GRAY);

g2d.setStroke(new BasicStroke((float)lineWidth));

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

{

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

{

x1 = xCells[i][j];

y1 = yCells[i][j];

x2 = xCells[i][j+1];

y2 = yCells[i][j+1];

Line2D line = new Line2D.Double(x1,y1,x2,y2);

g2d.draw(line);// draw Sidess here

}

}

}

public void drawPolygon(Graphics2D g2d)

{

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

{

Polygon poly = new Polygon();

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

// red for ith cell,id is from 0 to numberofsets-1

// if id=0, means "Mixture"

float g = (float)green[i][id]; // green for ith cell

float b = (float)blue[i][id]; // blue for ith cell

List><Integer> xList = new ArrayList<Integer>();

List<Integer> yList = new ArrayList<Integer>();

for (int j = 0; j<cellsPointsNumber[i]; j++)

{

xList.add((int)(xCells[i][j]));

yList.add((int)(yCells[i][j]));

}

Integer[] x = xList.toArray(new Integer[xList.size()]);

Integer[] y = yList.toArray(new Integer[yList.size()]);

for (int s = 0; s><xList.size(); s++)

{

poly.addPoint(x[s],y[s]);

}

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

g2d.fillPolygon(poly);

}

}

public static void main(String[] args)

{

JFrame appletFrame = new JFrame("My Applet");

Applet theApplet = new Combox();

appletFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

appletFrame.setSize(800,800);

appletFrame.add(theApplet,"Center");

theApplet.init();

appletFrame.setVisible(true);

}

} // end class Drawing

>

ardmorea at 2007-7-28 18:55:08 > top of Java-index,Security,Cryptography...
# 5

I have your TestCell class but without data to read I'm unable to run your class.

So here is a small example to give the basic approach to doing what it looks like

you are trying to do.

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.AffineTransform;

import java.io.*;

import java.util.Hashtable;

import javax.swing.*;

import javax.swing.event.*;

public class BasicIdea implements ActionListener, ChangeListener {

BasicGraphicPanel graphicPanel;

JFileChooser fileChooser;

public BasicIdea() {

fileChooser = new JFileChooser(".");

}

public void actionPerformed(ActionEvent e) {

if(fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {

File file = fileChooser.getSelectedFile();

int[][] data = getData(file);

graphicPanel.setData(data);

}

}

public void stateChanged(ChangeEvent e) {

int value = ((JSlider)e.getSource()).getValue();

double scale = (value+4)/10.0;

graphicPanel.setScale(scale);

}

private int[][] getData(File file) {

// Read data from file.

StringBuilder sb = new StringBuilder();

try {

BufferedReader br = new BufferedReader(

new InputStreamReader(

new FileInputStream(file)));

String line;

while((line = br.readLine()) != null) {

sb.append(line + "\n");

}

br.close();

} catch(IOException e) {

System.out.println("Read error: " + e.getMessage());

}

// Process data.

String[] strs = sb.toString().split("\\s");

int sides = strs.length/2;

int[][] data = new int[2][sides];

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

int row = j/sides;

int col = j%sides;

data[row][col] = Integer.parseInt(strs[j]);

}

return data;

}

private JPanel getCenterComponent() {

graphicPanel = new BasicGraphicPanel();

return graphicPanel;

}

private JPanel getControls() {

JButton button = new JButton("open");

button.addActionListener(this);

int min = 1, max = 16, inc = 5;

JSlider slider = new JSlider(min, max, 6);

slider.setMajorTickSpacing(5);

slider.setMinorTickSpacing(1);

slider.setSnapToTicks(true);

slider.setPaintTicks(true);

slider.setLabelTable(getLabelTable(min, max, inc));

slider.setPaintLabels(true);

slider.addChangeListener(this);

// Assemble.

JPanel panel = new JPanel(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.insets = new Insets(2,2,2,2);

gbc.weightx = 1.0;

gbc.gridwidth = GridBagConstraints.REMAINDER;

panel.add(button, gbc);

gbc.fill = GridBagConstraints.HORIZONTAL;

panel.add(slider, gbc);

return panel;

}

private Hashtable getLabelTable(int min, int max, int inc) {

Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>();

for(int j = min; j <= max; j+=inc) {

String s = String.format("%.1f", (j+4)/10.0);

table.put(Integer.valueOf(j), new JLabel(s));

}

return table;

}

public static void main(String[] args) {

BasicIdea test = new BasicIdea();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test.getCenterComponent());

f.getContentPane().add(test.getControls(), "Last");

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

}

}

class BasicGraphicPanel extends JPanel {

Polygon polygon;

double scale = 1.0;

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

if(polygon != null) {

Rectangle r = polygon.getBounds();

double x = r.getCenterX()*(1.0 - scale);

double y = r.getCenterY()*(1.0 - scale);

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

at.scale(scale, scale);

g2.draw(at.createTransformedShape(polygon));

}

}

public Dimension getPreferredSize() {

return new Dimension(400,400);

}

public void setScale(double scale) {

this.scale = scale;

repaint();

}

public void setData(int[][] data) {

int sides = data[0].length;

polygon = new Polygon(data[0], data[1], sides);

repaint();

}

}

And the two data files:

//triangle.txt

200 257 143

134 232 232

//septagon.txt

200 251 264 228 172 136 149

134 159 214 259 259 214 159

to load with the fileChooser.

crwooda at 2007-7-28 18:55:08 > top of Java-index,Security,Cryptography...
# 6

Thank you much. You are so patient. I am reading you code and try to understand. And I will modigy my code as your model. Now, could you

mind look at my current code? It still can't add on the drawing.Unfortunty I have no data file, my original data file is a binary format.

import java.io.*;

import java.util.List;

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.ListIterator;

import java.util.Scanner;

import java.awt.Font;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.BasicStroke;

import java.awt.GradientPaint;

import java.awt.geom.GeneralPath;

import java.awt.AlphaComposite;

import java.awt.Composite;

import java.awt.geom.*;

import java.awt.*;

import java.awt.BorderLayout;

import javax.swing.*;

import java.util.Hashtable;

import java.awt.event.*;

import javax.swing.event.*;

import java.applet.Applet;

public class Combox extends JApplet

{

private BasicGraphicPanel graphicPanel;

private JPanel jPanel1;

private JSlider dJSlider;

private JComboBox jComboBox1;

private JComboBox jComboBox2;

private JComboBox jComboBox3;

private JComboBox jComboBox4;

private int id = 0;

private int TRANSITION;

private String af[] = {"Paint","No Paint"};

private double scale=1;

private int a = 0;

private String tr[] = {"50","100","150"};

private String energyId[];

private int cellsPointsNumber[];

private int numberofCells;

private int numberofsets;

private double xCells[][]; // Every cell's points's x coordinates array

private double yCells[][]; // Every cell's points's y coordinates array

private double rgb[][]; // Color red, green, blue for each point in each cell

private double red[][];

private double green[][];

private double blue[][];

public void init() {

try {

SwingUtilities.invokeAndWait(new Runnable() {

public void run() {

initComponents();

}

});

} catch (Exception ex) {

ex.printStackTrace();

}

}

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents() {

FileSelection fs = new FileSelection();

File fileName = fs.getFile();

TestCell p = new TestCell();

p.cellNumber(fileName);

energyId = p.getenergyId();

graphicPanel = new BasicGraphicPanel();

jPanel1 = new JPanel();

jComboBox1 = new JComboBox();

jComboBox2 = new JComboBox();

jComboBox3 = new JComboBox();

int min = 1, max = 16, inc = 5;

dJSlider = new javax.swing.JSlider(min, max, 6);

dJSlider.setMajorTickSpacing(5);

dJSlider.setMinorTickSpacing(1);

dJSlider.setSnapToTicks(true);

dJSlider.setLabelTable(getLabelTable(min, max, inc));

dJSlider.setPaintTicks(true);

dJSlider.setPaintLabels(true);

dJSlider.addChangeListener(

new ChangeListener() // anonymous inner class

// handle change in slider value

{

public void stateChanged(ChangeEvent e)

{

int value = ((JSlider)e.getSource()).getValue();

double scale = (value+4)/10.0;

graphicPanel.setscale(scale);

}

}

);

// get plotting data

xCells = p.getxCells();

yCells = p.getyCells();

red = p.getred();

green = p.getgreen();

blue = p.getblue();

jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(energyId));

jComboBox1.addActionListener(

new ActionListener() // anonymous inner class

{

public void actionPerformed(ActionEvent e)

{

JComboBox cb = (JComboBox)e.getSource();

id = cb.getSelectedIndex();

graphicPanel.setid(id);

}

}

);

jPanel1.setLayout(new java.awt.GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.insets = new Insets(1,1,1,1);

gbc.weightx = 1.0;

jPanel1.add(jComboBox1, gbc);

jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(af));

jComboBox2.addActionListener(

new ActionListener() // anonymous inner class

{

public void actionPerformed(ActionEvent e)

{

JComboBox cc = (JComboBox)e.getSource();

a = cc.getSelectedIndex();

graphicPanel.seta(a);

}

}

);

jPanel1.add(jComboBox2, gbc);

jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "50", "100", "150" }));

jComboBox3 = new JComboBox(tr);

jComboBox3.setEditable(false);

jComboBox3.setForeground(Color.BLUE);

jComboBox3.setBackground(Color.GREEN);

jComboBox3.setAlignmentX(Component.LEFT_ALIGNMENT);

jComboBox3.addActionListener(

new ActionListener() // anonymous inner class

{

public void actionPerformed(ActionEvent e)

{

JComboBox cc = (JComboBox)e.getSource();

TRANSITION = cc.getSelectedIndex()*50+50;

graphicPanel.sett(TRANSITION);

}

}

);

jPanel1.add(jComboBox3, gbc);

gbc.gridx = 1;

gbc.gridheight = 4;

//gbc.fill = GridBagConstraints.HORIZONTAL;

Dimension d = dJSlider.getPreferredSize();

d.width = 40;

jPanel1.add(dJSlider, gbc);

Container cp = getContentPane();

graphicPanel.setxyrgbData(xCells,yCells,red,green,blue);

cp.add(graphicPanel,"Center");

cp.add(jPanel1,"North");

}

private Hashtable getLabelTable(int min, int max, int inc) {

Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>();

for(int j = min; j <= max; j+=inc) {

String s = String.format("%.1f", (j+4)/10.0);

table.put(Integer.valueOf(j), new JLabel(s));

}

return table;

}

class BasicGraphicPanel extends JPanel

{

private int TRANSITION=0; // Transition x,y coordinates of cells.

private double x1, y1, x2, y2; // to plot lines from (x1,y1) to (x2,y2)

private double lineWidth;

private int sideId;

private int sidesTotalNumber; // Total sides number for all cells

private int cellsPointsNumber[]; // point numbers of each cell.(each cell has how many points)

private int numberofCells;

private int numberofsets;

File fileName;

int a=0;

int id=0;

double scale=1;

private double xCells[][]; // Every cell's points's x coordinates array

private double yCells[][]; // Every cell's points's y coordinates array

private double x0[][];

private double y0[][];

private double rgb[][]; // Color red, green, blue for each point in each cell

private double red[][];

private double green[][];

private double blue[][];

// Constructor

public void setxyrgbData(double x[][],double y[][],double red[][],double green[][],double blue[][])

{

x0 = x;

y0 = y;

this.red = red;

this.green = green;

this.blue = blue;

}

public Dimension getPreferredSize(){

return new Dimension(600,600);

}

public void setscale(double scale)

{

this.scale = scale;

repaint();

}

public void setid(int id)

{

this.id = id;

repaint();

}

public void seta(int a)

{

this.a = a;

repaint();

}

public void sett(int TRANSITION)

{

this.TRANSITION = TRANSITION;

repaint();

}

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

getDrawingData();

if (a==0)

drawPolygon(g2d);

drawSides(g2d);

}

public void getDrawingData()

{

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

{

for(int j = 0; j<cellsPointsNumber[i]; j++)

{

xCells[i][j] = (x0[i][j])*scale+TRANSITION;

yCells[i][j] = (y0[i][j])*scale+TRANSITION;

}

}

}

public void drawSides(Graphics2D g2d)

{

g2d.setColor(Color.GRAY);

g2d.setStroke(new BasicStroke((float)lineWidth));

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

{

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

{

x1 = xCells[i][j];

y1 = yCells[i][j];

x2 = xCells[i][j+1];

y2 = yCells[i][j+1];

Line2D line = new Line2D.Double(x1,y1,x2,y2);

g2d.draw(line);// draw Sidess here

}

}

}

public void drawPolygon(Graphics2D g2d)

{

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

{

Polygon poly = new Polygon();

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

// red for ith cell,id is from 0 to numberofsets-1

// if id=0, means "Mixture"

float g = (float)green[i][id]; // green for ith cell

float b = (float)blue[i][id]; // blue for ith cell

List><Integer> xList = new ArrayList<Integer>();

List<Integer> yList = new ArrayList<Integer>();

for (int j = 0; j<cellsPointsNumber[i]; j++)

{

xList.add((int)(xCells[i][j]));

yList.add((int)(yCells[i][j]));

}

Integer[] x = xList.toArray(new Integer[xList.size()]);

Integer[] y = yList.toArray(new Integer[yList.size()]);

for (int s = 0; s><xList.size(); s++)

{

poly.addPoint(x[s],y[s]);

}

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

g2d.fillPolygon(poly);

}

}

}

public static void main(String[] args)

{

JFrame appletFrame = new JFrame("My Applet");

Applet theApplet = new Combox();

appletFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

appletFrame.setSize(800,800);

appletFrame.pack();

appletFrame.add(theApplet,"Center");

theApplet.init();

appletFrame.setVisible(true);

}

}

>

ardmorea at 2007-7-28 18:55:08 > top of Java-index,Security,Cryptography...
# 7

It still can't add on the drawing

Do you mean in the layout? I tried your initComponents code and it lays out okay.

// <applet code="ComboxRx3" width="600" height="500"></applet>

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import java.io.*;

import java.util.Hashtable;

import javax.swing.*;

import javax.swing.event.*;

public class ComboxRx3 extends JApplet {

//private Drawing drawing;

private JPanel jPanel1;

private JSlider dJSlider;

private JComboBox jComboBox1;

private JComboBox jComboBox2;

private JComboBox jComboBox3;

//private Container panel;

private int id;

private String af[] = {"Paint","No Paint"};

private double scale=1;

private int a;

private String energyId[] ={"one","two"};

public void init() {

try {

EventQueue.invokeAndWait(new Runnable() {

public void run() {

initComponents();

}

});

} catch (Exception ex) {

ex.printStackTrace();

}

}

private void initComponents() {

//FileSelection fs = new FileSelection();

//File fileName = fs.getFile();

//TestCell p = new TestCell();

//p.cellNumber(fileName);

//energyId = p.getenergyId();

//graphicPanel = new BasicGraphicPanel();

jPanel1 = new JPanel();

jComboBox1 = new JComboBox();

jComboBox2 = new JComboBox();

jComboBox3 = new JComboBox();

int min = 1, max = 16, inc = 5;

dJSlider = new javax.swing.JSlider(min, max, 6);

dJSlider.setMajorTickSpacing(5);

dJSlider.setMinorTickSpacing(1);

dJSlider.setSnapToTicks(true);

dJSlider.setLabelTable(getLabelTable(min, max, inc));

dJSlider.setPaintTicks(true);

dJSlider.setPaintLabels(true);

dJSlider.addChangeListener(new ChangeListener() {

public void stateChanged(ChangeEvent e) {

int value = ((JSlider)e.getSource()).getValue();

double scale = (value+4)/10.0;

//graphicPanel.setscale(scale);

}

});

// get plotting data

//xCells = p.getxCells();

//yCells = p.getyCells();

//red = p.getred();

//green = p.getgreen();

//blue = p.getblue();

jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(energyId));

jComboBox1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JComboBox cb = (JComboBox)e.getSource();

id = cb.getSelectedIndex();

//graphicPanel.setid(id);

}

});

jPanel1.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.insets = new Insets(1,1,1,1);

gbc.weightx = 1.0;

jPanel1.add(jComboBox1, gbc);

jComboBox2.setModel(new DefaultComboBoxModel(af));

jComboBox2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JComboBox cc = (JComboBox)e.getSource();

a = cc.getSelectedIndex();

//graphicPanel.seta(a);

}

});

jPanel1.add(jComboBox2, gbc);

jComboBox3.setModel(new DefaultComboBoxModel(

new String[] { "50", "100", "150" }));

//jComboBox3 = new JComboBox(tr);

jComboBox3.setEditable(false);

jComboBox3.setForeground(Color.BLUE);

jComboBox3.setBackground(Color.GREEN);

jComboBox3.setAlignmentX(Component.LEFT_ALIGNMENT);

jComboBox3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JComboBox cc = (JComboBox)e.getSource();

//TRANSITION = cc.getSelectedIndex()*50+50;

//graphicPanel.sett(TRANSITION);

}

});

jPanel1.add(jComboBox3, gbc);

gbc.gridx = 1;

// This is not correct. So far you have only two rows so the

// maximum gridheight that makes sense is 2. You cannot control

// size with this constraint. You can use an insets value(s) to

// change the spacing, eg, "gbc.insets.top = 5" or

// "gbc.insets = new Insets(5,0,2,0)".

//gbc.gridheight = 4;

//gbc.fill = GridBagConstraints.HORIZONTAL;

//Dimension d = dJSlider.getPreferredSize();

//d.width = 40;

jPanel1.add(dJSlider, gbc);

Container cp = getContentPane();

//graphicPanel.setxyrgbData(xCells,yCells,red,green,blue);

// I'm unable to use your graphics class but for layout purposes

// we can substitute a colored panel.

JPanel graphicPanel = new JPanel();

graphicPanel.setBackground(Color.red);

cp.add(graphicPanel,"Center");

cp.add(jPanel1,"North");

}

private Hashtable getLabelTable(int min, int max, int inc) {

Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>();

for(int j = min; j <= max; j+=inc) {

String s = String.format("%.1f", (j+4)/10.0);

table.put(Integer.valueOf(j), new JLabel(s));

}

return table;

}

public static void main(String[] args) {

JFrame appletFrame = new JFrame("My Applet");

JApplet theApplet = new ComboxRx3();

appletFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

appletFrame.setSize(800,600);

appletFrame.add(theApplet,"Center");

theApplet.init();

appletFrame.setVisible(true);

}

}

crwooda at 2007-7-28 18:55:08 > top of Java-index,Security,Cryptography...
# 8

Okay, now the program works well.

I want to embed the painting into a web page. why it failed?

My html code is

<html>

<applet code = "Combox.class" codebase="."

archive="TestCell4.2.jar"

width ="600" height = "450">

</applet>

</html>

The erros from Java console is

Java Plug-in 1.6.0_01

Using JRE version 1.6.0_01 Java HotSpot(TM) Client VM

User home directory = C:\Documents and Settings\123

-

c:clear console window

f:finalize objects on finalization queue

g:garbage collect

h:display this help message

l:dump classloader list

m:print memory usage

o:trigger logging

p:reload proxy configuration

q:hide console

r:reload policy configuration

s:dump system and deployment properties

t:dump thread list

v:dump thread stack

x:clear classloader cache

0-5: set trace level to <n>

-

java.lang.reflect.InvocationTargetException

at java.awt.EventQueue.invokeAndWait(Unknown Source)

at javax.swing.SwingUtilities.invokeAndWait(Unknown Source)

at Combox.init(Combox.java:57)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Caused by: java.security.AccessControlException: access denied (java.util.PropertyPermission user.dir read)

at java.security.AccessControlContext.checkPermission(Unknown Source)

at java.security.AccessController.checkPermission(Unknown Source)

at java.lang.SecurityManager.checkPermission(Unknown Source)

at java.lang.SecurityManager.checkPropertyAccess(Unknown Source)

at java.lang.System.getProperty(Unknown Source)

at java.io.Win32FileSystem.getUserPath(Unknown Source)

at java.io.Win32FileSystem.resolve(Unknown Source)

at java.io.File.getAbsolutePath(Unknown Source)

at sun.awt.shell.Win32ShellFolder2.<init>(Unknown Source)

at sun.awt.shell.Win32ShellFolder2.listFiles(Unknown Source)

at sun.awt.shell.ShellFolder.listFiles(Unknown Source)

at sun.awt.shell.Win32ShellFolderManager2.get(Unknown Source)

at sun.awt.shell.ShellFolder.get(Unknown Source)

at javax.swing.plaf.metal.MetalFileChooserUI.updateUseShellFolder(Unknown Source)

at javax.swing.plaf.metal.MetalFileChooserUI.installComponents(Unknown Source)

at javax.swing.plaf.basic.BasicFileChooserUI.installUI(Unknown Source)

at javax.swing.plaf.metal.MetalFileChooserUI.installUI(Unknown Source)

at javax.swing.JComponent.setUI(Unknown Source)

at javax.swing.JFileChooser.updateUI(Unknown Source)

at javax.swing.JFileChooser.setup(Unknown Source)

at javax.swing.JFileChooser.<init>(Unknown Source)

at javax.swing.JFileChooser.<init>(Unknown Source)

at FileSelection.getFile(FileSelection.java:10)

at Combox.initComponents(Combox.java:70)

at Combox.access$000(Combox.java:28)

at Combox$1.run(Combox.java:59)

at java.awt.event.InvocationEvent.dispatch(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

ardmorea at 2007-7-28 18:55:08 > top of Java-index,Security,Cryptography...
# 9

AccessControlException: access denied (java.util.PropertyPermission user.dir read)

Applets have limitations about accessing files. See

http://faq.javaranch.com/view?HowCanAnAppletReadFilesOnTheLocalFileSystem

for discussion.

crwooda at 2007-7-28 18:55:08 > top of Java-index,Security,Cryptography...
# 10

okay, I still have some questions.

1) I try to rotate my drawing. It doesn't work after I add one statement though.

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);

getDrawingData();

if (a==0)

drawPolygon(g2d);

drawSides(g2d);

g2d.rotate(Math.PI/8.0);// here not working

2) I found in my applet a small part is cut off at the bottom in my painting. why?

3). I see in your example applying with AffineTransform. In my case I will draw many lines without defining an object. So how can I implement it?

public void drawSides(Graphics2D g2d)

{

g2d.setColor(Color.GRAY);

g2d.setStroke(new BasicStroke((float)lineWidth));

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

{

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

{

x1 = xCells[i][j];

y1 = yCells[i][j];

x2 = xCells[i][j+1];

y2 = yCells[i][j+1];

Line2D line = new Line2D.Double(x1,y1,x2,y2);

g2d.draw(line);// draw Sides here

}

}

}

Thanks again,>

ardmorea at 2007-7-28 18:55:08 > top of Java-index,Security,Cryptography...
# 11

ignore the second question.

ardmorea at 2007-7-28 18:55:08 > top of Java-index,Security,Cryptography...