import java.awt.*; import java.awt.geom.*; import javax.swing.*; class JogDial extends JComponent { public static final int WIDTH = 400; public static final int HEIGHT = 200; public static final int OFFSET = 10; public static final int BAR_THICKNESS = 15; public static final int LINE_DIFFERENCE = 7; public static final int BEAD_WIDTH = 10; public static final int BEAD_HEIGHT = 25; protected int[] values = {-2,-1,0,1,2}; protected int bead_x = WIDTH/2; protected int bead_y = OFFSET + (BAR_THICKNESS/2); public JogDial() { super(); } public Dimension getMinimumSize() { return(new Dimension(WIDTH,HEIGHT)); } public Dimension getPreferredSize() { return(this.getMinimumSize()); } public void drawBead(Graphics2D g2) { Rectangle bead = new Rectangle(bead_x - (this.BEAD_WIDTH/2), bead_y -(this.BEAD_HEIGHT/2), BEAD_WIDTH, BEAD_HEIGHT); g2.setColor(Color.gray); g2.fill(bead); g2.setColor(Color.black); g2.draw(bead); } protected void moveBeadRight() { if (bead_x < 400 - OFFSET) bead_x++; /* */ Graphics g = getGraphics(); drawBead((Graphics2D) g); //// calls the function to draw the bead.. g.dispose(); // not recommended // repaint(); /* //// repaints damaged area Rectangle damaged = new Rectangle(bead_x - 1 - (BEAD_WIDTH/2), bead_y - (BEAD_HEIGHT/2), 1, BEAD_HEIGHT+1); repaint(damaged); */ /* //// repaints new + damaged Rectangle bead = new Rectangle(bead_x - (this.BEAD_WIDTH/2), bead_y -(this.BEAD_HEIGHT/2), BEAD_WIDTH+1, BEAD_HEIGHT+1); Rectangle damaged = new Rectangle(bead_x - 1 - (BEAD_WIDTH/2), bead_y - (BEAD_HEIGHT/2), 1, BEAD_HEIGHT+1); repaint(bead); repaint(damaged); */ } protected void snapBeadCenter() { Rectangle damaged = new Rectangle(bead_x - (BEAD_WIDTH/2), bead_y - (BEAD_HEIGHT/2), BEAD_WIDTH+1, BEAD_HEIGHT+1); bead_x = 200; /* */ Graphics g = getGraphics(); drawBead((Graphics2D) g); //// calls the function to draw the bead.. g.dispose(); // not recommended // repaint(); /* //// repaints damaged area repaint(damaged); */ /* //// repaints new + damaged Rectangle bead = new Rectangle(bead_x - (this.BEAD_WIDTH/2), bead_y -(this.BEAD_HEIGHT/2), BEAD_WIDTH+1, BEAD_HEIGHT+1); repaint(bead); repaint(damaged); */ } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; Dimension size = getSize(); // Polygon p = new Polygon(); // p.addPoint(x,y); Font font = new Font("Helvetica", Font.BOLD, 14); FontMetrics metrics = g2.getFontMetrics(font); Rectangle bar = new Rectangle(OFFSET,OFFSET,size.width-2*OFFSET,BAR_THICKNESS); g2.setColor(Color.black); g2.setFont(font); int segment_size = bar.width / (values.length - 1); for (int i=0; i