Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

    Smile Developing Drag and drop Analog Clock

    I have already code for analog clock.
    Class Clock is extended Jpanel.I have used Jpanel as parent Panel.
    In Which i placed this Analacolck in coding.i get the clock.
    but i am unable to change the background color.
    kindly give solution

    [Code]
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package analogclock;

    import java.awt.Dimension;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import javax.swing.JPanel;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.util.*;


    /**
    *
    * @author britto
    */
    public class Clock extends JPanel {
    //class Clock extends J {

    private int hours = 0;
    private int minutes = 0;
    private int seconds = 0;
    private int millis = 0;

    private static final int spacing = 10;
    private static final float twoPi = (float)(2.0 * Math.PI);
    private static final float threePi = (float)(3.0 * Math.PI);
    // Angles for the trigonometric functions are measured in radians.
    // The following in the number of radians per sec or min.
    private static final float radPerSecMin = (float)(Math.PI / 30.0);

    private int size; // height and width of clock face
    private int centerX; // x coord of middle of clock
    private int centerY; // y coord of middle of clock
    private BufferedImage clockImage;
    private javax.swing.Timer t;

    //================================================== == Clock constructor
    public Clock() {
    this.setPreferredSize(new Dimension(100,100));
    this.setBackground(Color.white);


    t = new javax.swing.Timer(1000,
    new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    update();
    }
    });
    }//end constructor

    //================================================== ============= update
    // Replace the default update so that the plain background
    // doesn't get drawn.
    public void update() {
    // Hmmm, is this the right way to do this? Or do we just
    // call paintComponent? Does this coalesce calls, not that
    // this updates often enuf for that to be important.
    this.repaint();
    }//end update

    //================================================== ============== start
    public void start() {
    t.start(); // start the timer
    }//end start

    //================================================== =============== stop
    public void stop() {
    t.stop(); // start the timer
    }//end stop

    //================================================== ===== paintComponent

    public void paintComponent(Graphics g) {
    super.paintComponent(g); // paint background, borders
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
    RenderingHints.VALUE_ANTIALIAS_ON);

    // The panel may have been resized, get current dimensions
    int w = getWidth();
    int h = getHeight();
    size = ((w<h) ? w : h) - 2*spacing;
    centerX = size/2 + spacing;
    centerY = size/2 + spacing;

    // Create the clock face background image if this is the first time,
    // or if the size of the panel has changed
    if (clockImage == null
    || clockImage.getWidth() != w
    || clockImage.getHeight() != h) {

    clockImage = (BufferedImage)(this.createImage(w, h));
    // now get a graphics context from this image
    Graphics2D gc = clockImage.createGraphics();
    gc.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
    RenderingHints.VALUE_ANTIALIAS_ON);
    drawClockFace(gc);
    }

    // Now get the time and draw the hands.
    Calendar now = Calendar.getInstance();
    hours = now.get(Calendar.HOUR);
    minutes = now.get(Calendar.MINUTE);
    seconds = now.get(Calendar.SECOND);
    millis = now.get(Calendar.MILLISECOND);

    // Draw the clock face from the precomputed image
    g2.drawImage(clockImage, null, 0, 0);

    // Draw the clock hands
    drawClockHands(g);
    }//end paintComponent


    //================================================== ===== drawClockHands
    private void drawClockHands(Graphics g) {
    int secondRadius = size/2;
    int minuteRadius = secondRadius * 3/4;
    int hourRadius = secondRadius/2;

    // second hand
    float fseconds = seconds + (float)millis/1000;
    float secondAngle = threePi - (radPerSecMin * fseconds);
    drawRadius(g, centerX, centerY, secondAngle, 0, secondRadius);

    // minute hand
    float fminutes = (float)(minutes + fseconds/60.0);
    float minuteAngle = threePi - (radPerSecMin * fminutes);
    drawRadius(g, centerX, centerY, minuteAngle, 0, minuteRadius);

    // hour hand
    float fhours = (float)(hours + fminutes/60.0);
    float hourAngle = threePi - (5 * radPerSecMin * fhours);
    drawRadius(g, centerX, centerY, hourAngle, 0, hourRadius);
    }//end drawClockHands

    //================================================== ====== drawClockFace
    private void drawClockFace(Graphics g) {
    // clock face
    g.setColor(Color.GREEN);
    g.fillOval(spacing, spacing, size, size);
    g.setColor(Color.black);
    g.drawOval(spacing, spacing, size, size);

    // tic marks
    for (int sec = 0; sec<60; sec++) {
    int ticStart;
    if (sec%5 == 0) {
    ticStart = size/2-10;
    } else {
    ticStart = size/2-5;
    }
    drawRadius(g, centerX, centerY, radPerSecMin*sec, ticStart , size/2);
    }
    }//endmethod drawClockFace

    //================================================== ========= drawRadius
    private void drawRadius(Graphics g, int x, int y, double angle,
    int minRadius, int maxRadius) {
    float sine = (float)Math.sin(angle);
    float cosine = (float)Math.cos(angle);

    int dxmin = (int)(minRadius * sine);
    int dymin = (int)(minRadius * cosine);

    int dxmax = (int)(maxRadius * sine);
    int dymax = (int)(maxRadius * cosine);
    g.drawLine(x+dxmin, y+dymin, x+dxmax, y+dymax);
    }//end drawRadius

    }//endclass Clock

    //and i call this call this Clock Class in this method

    public void setAnalogClock(){

    jpnlAnalogClock=new Clock() ;
    jpnlAnalogClock.start();
    //67, 114, 160

    // jpnlAnalogClock.setBackground(67, 114, 160); // NOI18N
    jpnlAnalogClock.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 10, true));
    //jpnljpnlAnalogClockClock.setName("jpnlClock"); // NOI18N
    //jpnlAnalogClock.setForeground(Color.RED);
    // jpnlAnalogClock.setBackground(Color.RED);
    jpnlAnalogClock.disable();

    javax.swing.GroupLayout jpnlClockLayout = new javax.swing.GroupLayout(jpnlAnalogClock);
    jpnlAnalogClock.setLayout(jpnlClockLayout);
    jpnlClockLayout.setHorizontalGroup(
    jpnlClockLayout.createParallelGroup(javax.swing.Gr oupLayout.Alignment.LEADING)
    .addGap(0, 224, Short.MAX_VALUE)
    );
    jpnlClockLayout.setVerticalGroup(
    jpnlClockLayout.createParallelGroup(javax.swing.Gr oupLayout.Alignment.LEADING)
    .addGap(0, 216, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout jpnlLeftLayout = new javax.swing.GroupLayout(jpnlLeft);
    jpnlLeft.setLayout(jpnlLeftLayout);
    jpnlLeftLayout.setHorizontalGroup(
    jpnlLeftLayout.createParallelGroup(javax.swing.Gro upLayout.Alignment.LEADING)
    .addGroup(jpnlLeftLayout.createSequentialGroup()
    .addGap(24, 24, 24)
    .addComponent(jlblLogo, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGroup(jpnlLeftLayout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jlblFlashScreen, javax.swing.GroupLayout.PREFERRED_SIZE, 288, Short.MAX_VALUE)
    .addContainerGap())
    .addGroup(jpnlLeftLayout.createSequentialGroup()
    .addGap(27, 27, 27)
    .addGroup(jpnlLeftLayout.createParallelGroup(javax .swing.GroupLayout.Alignment.TRAILING)
    .addComponent(jpnlAnalogClock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jlblLongDate, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)))
    );
    jpnlLeftLayout.setVerticalGroup(
    jpnlLeftLayout.createParallelGroup(javax.swing.Gro upLayout.Alignment.LEADING)
    .addGroup(jpnlLeftLayout.createSequentialGroup()
    .addGap(70, 70, 70)
    .addComponent(jlblLogo)
    .addGap(70, 70, 70)
    .addComponent(jlblFlashScreen)
    .addGap(79, 79, 79)
    .addComponent(jpnlAnalogClock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(54, 54, 54)
    .addComponent(jlblLongDate, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(35, Short.MAX_VALUE))
    );

    }
    Last edited by britto_bicsjohn; 08-07-2009 at 07:16 AM. Reason: code tags added to aid in readability
    :) javadeveloper

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Do you mean as a drag and drop component for NetBeans Matisse GUI builder?

    Also, please use code tags when posting code so that the code will retain its formatting and be more easily readable. You can do this by highlighting your code when editing the post and pressing the "CODE" button. I added the tags to your post above so you can see how they help.

    Thanks and good luck!

  3. #3
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

    Default

    Thank your for ur help
    As i am new to this.
    i do not know how to hidglight the Code.
    Can u say the procedure how to higlit
    :) javadeveloper

Similar Threads

  1. Analog Clock Help
    By mprentice84 in forum New To Java
    Replies: 13
    Last Post: 06-02-2010, 05:29 PM
  2. Drag and Drop Issue
    By kirly in forum Advanced Java
    Replies: 1
    Last Post: 04-16-2009, 05:04 AM
  3. Drag and drop
    By thayalan in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2009, 03:04 PM
  4. Replies: 0
    Last Post: 07-07-2008, 04:36 PM
  5. Drag and drop
    By abhivenugopal in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-30-2008, 02:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •