Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Forums
Register
Login
Java
»
Java in General
JSlider is not working.
Hui Zhao
,
Ranch Hand
Jul 17, 2007 09:25:00
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
I am going to plot a 2d, a combox and a JSlider.but JSlider is not working.
Please help me.
thanks
import java.io.*; import java.awt.Font; import java.awt.Color; import java.awt.Graphics; 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 TestLayout extends JApplet { private Drawing drawing; private JComboBox comb; private JComboBox alfa; private JSlider dJSlider; private Container panel; private String af[] = {"Paint","No Paint"}; private String e[] = {"One","Two"}; private GridBagLayout layout; private GridBagConstraints gbc; public void init() { panel = getContentPane(); drawing = new Drawing(); JPanel p2 = new JPanel(); p2.add(drawing,BorderLayout.CENTER); p2.add(new getSlider(),BorderLayout.NORTH); p2.add(new getComboPanel(),BorderLayout.EAST); panel.add(p2,"Center"); } class getComboPanel extends JPanel { public getComboPanel(){ comb = new JComboBox(e); layout = new GridBagLayout(); setLayout(layout); gbc = new GridBagConstraints(); addComponent(this,comb,0,0,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER); comb.setEditable(false); comb.setForeground (Color.BLUE); comb.setBackground (Color.GREEN); comb.setAlignmentX(Component.LEFT_ALIGNMENT); alfa = new JComboBox(af); gbc.weightx = 100; gbc.weighty = 0; addComponent(this,alfa,1,0,20000,1,GridBagConstraints.NONE, GridBagConstraints.CENTER); alfa.setEditable(false); alfa.setForeground(Color.BLUE); alfa.setBackground(Color.GREEN); alfa.setAlignmentX(Component.LEFT_ALIGNMENT); add(comb); add(alfa); } private void addComponent(Container container, Component component, int row, int column, int width, int height, int fill, int anchor) { gbc.gridx = column; gbc.gridy = row; gbc.gridwidth = width; gbc.gridheight = height; gbc.fill = fill; gbc.anchor = anchor; layout.setConstraints(component,gbc); container.add(component); } } class getSlider extends JPanel { public 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); } 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 Drawing extends JPanel { public void paintComponent(Graphics g) { super.paintComponent( g ); // call superclass's paint method this.setBackground( Color.WHITE ); Graphics2D g2d = (Graphics2D)g; g2d.draw3DRect(3,100,90,55,true); } // end method paintComponent } public static void main(String[] args) { JFrame appletFrame = new JFrame("My Applet"); Applet theApplet = new TestLayout(); appletFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); appletFrame.setSize(800,800); appletFrame.add(theApplet,"Center"); theApplet.init(); appletFrame.setVisible(true); } }
marc weber
,
Sheriff
staff
Jul 17, 2007 10:14:00
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
I don't see any code that "listens" for when the slider is changed and does something when that happens.
See
Java
Tutorial - How to Use Sliders and
Java Tutorial - How to Write a Change Listener
.
Hui Zhao
,
Ranch Hand
Jul 17, 2007 10:59:00
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
sorry,please see my new code.
Exception in thread "main" java.lang.NullPointerException at Combox.addComponent(Combox.java:146) at Combox.access$600(Combox.java:27) at Combox$getComboPanel.<init>(Combox.java:97) at Combox.init(Combox.java:52) at Combox.main(Combox.java:290) Java Result: 1
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[]; private GridBagLayout layout; private GridBagConstraints gbc; public void init() { panel = getContentPane(); drawing = new Drawing(a,id,scale); TestCell p = new TestCell(); p.energyId(); energyId = p.getenergyId(); panel.add(drawing); panel.add(new getSlider(),BorderLayout.NORTH); panel.add(new getComboPanel(),BorderLayout.EAST);//[B]wrong here[/B] } class getSlider extends JPanel { public 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); } } ); add(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; } } class getComboPanel extends JPanel { public getComboPanel(){ currentPattern = energyId[0]; comb = new JComboBox(energyId); gbc = new GridBagConstraints(); addComponent(this,comb,0,0,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER); comb.setEditable(false); 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(); drawing.setid(id); } } ); alfa = new JComboBox(af); addComponent(this,alfa,0,1,1,1,GridBagConstraints.NONE, GridBagConstraints.CENTER); 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(); drawing.seta(a); } } ); add(comb); add(alfa); } } private void addComponent(Container container, Component component, int row, int column, int width, int height, int fill, int anchor) { gbc.gridx = column; gbc.gridy = row; gbc.gridwidth = width; gbc.gridheight = height; gbc.fill = fill; gbc.anchor = anchor; layout.setConstraints(component,gbc); container.add(component); } 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 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; repaint(); } public void setid(int id) { this.id = id; repaint(); } public void seta(int a) { this.a = a; 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); } }
Hui Zhao
,
Ranch Hand
Jul 17, 2007 11:15:00
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
ignore it, i have gotten.
poster closed.
Bert Bates
,
author
staff
Jul 17, 2007 12:05:00
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
just for the record, even though this was closed, i'm moving it to the more appropriate intermediate form
Post by:
autobot
So I left, I came home, and I ate some pie. And then I read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
This thread has been viewed 1557 times.
Similar Threads
put an applet into a web page
Events listener are not working.
AWT Event look for help
JSlider is not working.
Layout problem
More...
All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:37:08
.