Drawing with Swing, using a JSlider : Slider « Swing JFC « Java

Drawing with Swing, using a JSlider

Drawing with Swing, using a JSlider
// : c14:SineWave.java
// Drawing with Swing, using a JSlider.
// <applet code=SineWave width=700 height=400></applet>
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

class SineDraw extends JPanel {
  private static final int SCALEFACTOR = 200;

  private int cycles;

  private int points;

  private double[] sines;

  private int[] pts;

  public SineDraw() {

  public void setCycles(int newCycles) {
    cycles = newCycles;
    points = SCALEFACTOR * cycles * 2;
    sines = new double[points];
    for (int i = 0; i < points; i++) {
      double radians = (Math.PI / SCALEFACTOR) * i;
      sines[i] = Math.sin(radians);

  public void paintComponent(Graphics g) {
    int maxWidth = getWidth();
    double hstep = (double) maxWidth / (double) points;
    int maxHeight = getHeight();
    pts = new int[points];
    for (int i = 0; i < points; i++)
      pts[i] = (int) (sines[i] * maxHeight / 2 * .95 + maxHeight / 2);
    for (int i = 1; i < points; i++) {
      int x1 = (int) ((i - 1) * hstep);
      int x2 = (int) (i * hstep);
      int y1 = pts[i - 1];
      int y2 = pts[i];
      g.drawLine(x1, y1, x2, y2);

public class SineWave extends JApplet {
  private SineDraw sines = new SineDraw();

  private JSlider adjustCycles = new JSlider(1, 30, 5);

  public void init() {
    Container cp = getContentPane();
    adjustCycles.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        sines.setCycles(((JSlider) e.getSource()).getValue());
    cp.add(BorderLayout.SOUTH, adjustCycles);

  public static void main(String[] args) {
    run(new SineWave(), 700, 400);

  public static void run(JApplet applet, int width, int height) {
    JFrame frame = new JFrame();
    frame.setSize(width, height);
} ///:~


Related examples in the same category

1.Creating a JSlider Component
2.Create a horizontal slider with custom min, max, and value
3.Create a vertical slider with min=0, max=100, value=50
4.Create a vertical slider with custom min, max, and value
5.Make the horizontal slider move right-to-left
6.Make it vertical and move bottom-to-top
7.Make it vertical and move top-to-bottom
8.Get the extent
9.Getting and Setting the Values of a JSlider Component
10.Using progress bars and slidersUsing progress bars and sliders
11.A slider with tick marks and labels
12.An example of JSlider with default labelsAn example of JSlider with default labels
13.Set minor tick marks every 5 units
14.Set major tick marks every 25 units
15.Slider SampleSlider Sample
16.JSlider Sample 2JSlider Sample 2
17.Slider ChangeListenerSlider ChangeListener
18.Inverted SlidersInverted Sliders
19.Slider change action listenerSlider change action listener
20.Date sliderDate slider
21.Tick SlidersTick Sliders
22.Scroll SliderScroll Slider
23.Text SliderText Slider
24.Change the minimum value
25.Change the maximum value
26.Set the extent
27.Move the slider by a fixed amount: the extent.
28.Set all the values at once by using the model
29.Set the value; the new value will be forced into the bar's range
30.Listening for Value Changes in a JSlider Component
31.Constraining JSlider Component Values to Tick Marks
32.Determine if currently snapping to tick marks
33.Snap to tick marks
34.Set to a spot between tick marks; the value moves to closest tick mark
35.Determine if currently painting labels
36.Showing Tick Marks on a JSlider Component
37.Show tick marks
38.The slider allows you to use an arbitrary label at any particular major tick mark.
39.Showing Tick-Mark Labels on a JSlider Component
40.Hide the track
41.Sample SlidersSample Sliders
42.A frame with many sliders and a text field to show slider valuesA frame with many sliders and a text field to show slider values