Java Qt Extensions

Examples

Example3 - Custom Component

An example of a custom-drawn component.

QSwing components can draw themselves by overriding the paintComponent method. Component painting in QSwing is less flexible then in Swing. There are no paintBorder and paintChildren methods and the component painting must be explicitly enabled by invoking enableEvents(QSwingEvent.PAINT_EVENT_MASK). This is mainly because of performance reasons.

Code

package yu.ac.bg.etf.javaqx.demo.qsvs.qswing;

import java.util.Calendar;

import yu.ac.bg.etf.javaqx.qswing.JQComponent;
import yu.ac.bg.etf.javaqx.qswing.JQTimer;
import yu.ac.bg.etf.javaqx.qswing.QSwing;
import yu.ac.bg.etf.javaqx.qswing.events.ActionEvent;
import yu.ac.bg.etf.javaqx.qswing.events.ActionListener;
import yu.ac.bg.etf.javaqx.qswing.events.QSwingEvent;
import yu.ac.bg.etf.javaqx.qswing.graphics.Color;
import yu.ac.bg.etf.javaqx.qswing.graphics.Font;
import yu.ac.bg.etf.javaqx.qswing.graphics.Graphics;

/**
 * Custom Component.
 */
public class CustomComponent extends JQComponent {

  private JQTimer timer; 

  public CustomComponent() {
    setName("Custom Component");
    setBounds(100100200200);
    setBackground(new Color(000));
    setForeground(new Color(255255255));
    enableEvents(QSwingEvent.PAINT_EVENT_MASK)
    timer = new JQTimer(500new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        repaint();
      }
    });
    timer.start();
  }

  protected void paintComponent(Graphics g) {
    int w = getWidth();
    int w2 = w >> 1;
    int h = getHeight();
    int h2 = h >> 1;
    int cs = (w < h? w - 20 : h - 50;
    int cs2 = cs >> 1;
    int cs2m5 = cs2 - 5;
    int cs2m12 = cs2 - 12;
    int cs2m30 = cs2 - 30;
    double a;
    g.fillOval(w2 - 2, h2 - 244);
    for (int i = 0; i < 12; i++) {
      a = Math.toRadians(i * 30);
      double cosa = Math.cos(a);
      double sina = Math.sin(a);
      int x1 = w2 + (int)(cs2m5 * cosa);
      int y1 = h2 - (int)(cs2m5 * sina);
      int x2 = w2 + (int)(cs2 * cosa);
      int y2 = h2 - (int)(cs2 * sina);
      g.drawLine(x1, y1, x2, y2);
    }
    Calendar calendar = Calendar.getInstance();
    int hour = calendar.get(Calendar.HOUR);
    int minute = calendar.get(Calendar.MINUTE);
    int second = calendar.get(Calendar.SECOND);
    a = Math.toRadians(-hour * (30 (minute >> 4)) 90);
    int x = w2 + (int)(cs2m30 * Math.cos(a));
    int y = h2 - (int)(cs2m30 * Math.sin(a));
    g.drawLine(w2, h2, x, y);
    a = Math.toRadians(-minute * 90);
    x = w2 + (int)(cs2m12 * Math.cos(a));
    y = h2 - (int)(cs2m12 * Math.sin(a));
    g.drawLine(w2, h2, x, y);
    g.setColor(new Color(25000));
    a = Math.toRadians(-second * 90);
    x = w2 + (int)(cs2 * Math.cos(a));
    y = h2 - (int)(cs2 * Math.sin(a));
    g.drawLine(w2, h2, x, y);
    g.setColor(new Color(150150255));
    g.setFont(g.getFont().deriveFont(Font.BOLD));
    String dt = calendar.getTime().toString();
    g.drawString(dt,
                 w2 - (g.getFontMetrics().stringWidth(dt>> 1),
                 h - 5);
  }

  public static void main(String[] args) {
    CustomComponent customComponent = new CustomComponent();
    customComponent.setVisible(true);
    QSwing.startEventLoop(true);
  }

}

Screenshots

CustomComp. on
Windows Vista
CustomComp. on
Windows XP
CustomComp. on
Windows 98
CustomComp. on
SUSE Linux
CustomComp. on
Ubuntu Linux