java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4090
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to implement your own sketching application E-mail
User Rating: / 4
PoorBest 

This Java Swing tip demonstrates a sketching application. The application includes two dials: one for horizontal movement, one for vertical movement. The dials are instances of the JogShuttle class.

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.*;

import javax.swing.*;
import javax.swing.border.*;

public class Sketch extends JPanel
        implements PropertyChangeListener, ActionListener {
    
    JogShuttle shuttle1;
    JogShuttle shuttle2;
    JPanel board;
    JButton clear;
    
    int lastX, lastY;  // Keep track of the last point we drew.
    
    public Sketch() {
        
        super(true);
        
        setLayout(new BorderLayout());
        board = new JPanel(true);
        board.setPreferredSize(new Dimension(300300));
        board.setBorder(new LineBorder(Color.black, 5));
        
        clear = new JButton("Clear Drawing Area");
        clear.addActionListener(this);
        
        shuttle1 = new JogShuttle(0300150);
        lastX = shuttle1.getValue();
        shuttle2 = new JogShuttle(0300150);
        lastY = shuttle2.getValue();
        
        shuttle1.setValuePerRevolution(100);
        shuttle2.setValuePerRevolution(100);
        
        shuttle1.addPropertyChangeListener(this);
        shuttle2.addPropertyChangeListener(this);
        
        shuttle1.setBorder(new BevelBorder(BevelBorder.RAISED));
        shuttle2.setBorder(new BevelBorder(BevelBorder.RAISED));
        
        add(board, BorderLayout.NORTH);
        add(shuttle1, BorderLayout.WEST);
        add(clear, BorderLayout.CENTER);
        add(shuttle2, BorderLayout.EAST);
    }
    
    public void propertyChange(PropertyChangeEvent e) {
        
        if (e.getPropertyName() == "value") {
            Graphics g = board.getGraphics();
            g.setColor(getForeground());
            g.drawLine(lastX, lastY,
                    shuttle1.getValue(), shuttle2.getValue());
            lastX = shuttle1.getValue();
            lastY = shuttle2.getValue();
        }
    }
    
    public void actionPerformed(ActionEvent e) {
        
        //  The button must have been pressed.
        Insets insets = board.getInsets();
        Graphics g = board.getGraphics();
        g.setColor(board.getBackground());
        g.fillRect(insets.left, insets.top,
                board.getWidth()-insets.left-insets.right,
                board.getHeight()-insets.top-insets.bottom);
    }
    
    public static void main(String[] args) {
        
        UIManager.put(JogShuttleUI.UI_CLASS_ID, "BasicJogShuttleUI");
        Sketch s = new Sketch();
        
        JFrame frame = new JFrame("Sample Sketch Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(s);
        frame.pack();
        frame.setVisible(true);
        
    }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.