Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Jslider help

Fri May 18, 2007 2:09 pm

hello
how to write program which could zoom in or zoom out x and y coordinates using two JSliders. One JSlider (horizontal) for x, and other JSlider (vertical) for y.

I want to put them in this program
myFrame.java
java code
/* 
* myFrame.java
*/
import javax.swing.JFrame;

public class myFrame extends JFrame{

public myFrame() {

getContentPane().add(cpanel);
setTitle("XY-Curve");
setSize(600,450);
setLocation(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String[] args) {
new myFrame();
}
myPanel cpanel=new myPanel();
}

myPanel.java

java code
/* 
* myPanel.java
*/

import java.awt.*;
import java.awt.geom.*;
import java.awt.Color;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JPanel;

public class myPanel extends JPanel{

private JButton randButton=new JButton("Draw");
private double[] temparray = new double[]{70.05d, 10.20d, 25.00d, 50.53d, 40.60, 100.00};
private float[] timearray=new float[] {8.15f, 8.45f, 9.15f, 9.45f, 10.15f, 10.45f};
private double oldy;
private float oldx;
Graphics2D g2;

public myPanel() {

randButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

repaint();
}
});

add(randButton);
}

public void paint(Graphics g) {
g2 = (Graphics2D) g;
g2.drawLine(30,400,30,100); // y koordinaciu asis
g2.drawLine(30,400,500,400); // x koordinaciu asis
g2.setColor(Color.BLUE);
oldy=0.0d;
oldx=0.0f;
for(int i=0;i<timearray.length*40;i+=40) {
g2.draw(new Line2D.Double(70+i-40, 400-oldy, 70+i, 400-temparray[i/40]));
oldy=temparray[i/40];
oldx=timearray[i/40];
g.setColor(Color.RED.brighter());
g.drawOval(70+i-2, (int)(400-temparray[i/40]-2),4,4);
for(int j=0; j<300; j+=20){
g.drawString(""+j+"", 4, (int)(400-j));
}
// g.drawString(""+(int)oldy+"", 4, (int)(400-temparray[i/40]-2));
g.drawString(""+oldx+"", 70+i-2, 420);
g.setColor(Color.BLUE);
}
g.setColor(Color.RED);
g.drawString("Temperature", 10, 80);
g.drawString("Time", 550, 400);
}
}



Somebody have ideas?



Re: Jslider help

Fri May 18, 2007 3:08 pm

Hi Makaule,

Zooming mmm ? I have made zooming in java for images.I also added JSlider.But it was not for these purpose. Any way you can check the project. I think it will help you soo much because i contain zooming , jslider , and also scroll bars that interact with the zooming ,
at this link http://www.codemiles.com/viewtopic.php?f=101&t=162
remember it is a netBeans IDE project, if you don't have netBeans you can get it from the following link http://www.netbeans.org/

I add the JSlider and ChangeListener to the JPanel Class
Code:


import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
*
* @author mohamed
*/
public class myPanel extends JPanel implements ChangeListener{
   
    /** Creates a new instance of myPanel */
   
    private JButton randButton=new JButton("Draw");
    private JSlider brislide=new JSlider(0,255,128);
    private JTextField myText=new JTextField(3);
    private JLabel myLabel=new JLabel("Max  Temperature ");
    private int[] temparray=new int[]{7,10,25,50,30};
    private float[] timearray=new float[] {8.15f, 8.45f, 9.15f, 9.45f, 10.15f};
    private int oldy;
    public myPanel() {
       
        randButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

repaint();
            }
        });
       
     brislide.addChangeListener(this);
        add(randButton);     
add(brislide);
    }
   
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
       
        g.drawLine(10,300,10,200);
        g.drawLine(10,300,110,300);
        g.setColor(Color.BLUE);
        oldy=0;
        for(int i=0;i            g.drawLine(30+i-20,300-oldy,30+i,300-temparray[i/20]);
            oldy=temparray[i/20];
            g.setColor(Color.RED.brighter());
            g.drawOval(30+i-2,300-temparray[i/20]-2,4,4);
            g.setColor(Color.BLUE);
           
        }
        g.setColor(Color.RED);
        g.drawString("Temperature",10,100);
        g.drawString("Time  ",120,300);
    }

    public void stateChanged(ChangeEvent e) {
        /// Called when You change the Slider Value
        System.out.println(brislide.getValue());
        // Do Your Zooming Process
    }
   
}



Download the Project from here
XYCurve.rar
NetBeans XY-Curve Project
(11.72 KiB) Downloaded 1258 times



Re: Jslider help

Fri May 18, 2007 3:13 pm

thanks I will check this

Re: Jslider help

Mon Jun 25, 2007 2:00 pm

msi_333, could you add in this program 3 radiobuttons? and how to use layout in jpanel?

Re: Jslider help

Mon Jun 25, 2007 4:03 pm

Hi
Oh :shock: ,I think you have to go to books forum and download a book that help in Java GUI by SWING and then search for radio button and how to make it.I recommended the Thinking in Java book. I will try to make Radio button on JFrame code as soon as i can and post it here. If you have a specific problem in your code you can tell us about.

Post a reply

Topic Tags

Java Swing