File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Java
»
Swing / AWT / SWT
Author
Events listener are not working.
Hui Zhao
Ranch Hand
Joined: Jul 09, 2007
Posts: 116
posted
Jul 16, 2007 09:36:00
0
I have three events which are passing parameters: a,id and scale.
The drawing is successful except listener no responses.
I guess I missed something, but where is it?
Please advise.
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
I agree. Here's the link:
http://aspose.com/file-tools
subject: Events listener are not working.
Similar Threads
Signed an applet
put an applet into a web page
JSlider is not working.
JSlider is not working.
AWT Event look for help
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter