Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Basic wxRuby Form Example

From the Ruby on Windows blog...


require 'wx'
include Wx

class MyFrame < Frame
    def initialize()
        super(nil, -1, 'My Frame Title')
        # First create the controls
        @my_panel = Panel.new(self)
        @my_label = StaticText.new(@my_panel, -1, 'My Label Text', 
            DEFAULT_POSITION, DEFAULT_SIZE, ALIGN_CENTER)
        @my_textbox = TextCtrl.new(@my_panel, -1, 'Default Textbox Value')
        @my_combo = ComboBox.new(@my_panel, -1, 'Default Combo Text', 
            DEFAULT_POSITION, DEFAULT_SIZE, ['Item 1', 'Item 2', 'Item 3'])
        @my_button = Button.new(@my_panel, -1, 'My Button Text')
        # Bind controls to functions
        evt_button(@my_button.get_id()) { |event| my_button_click(event)}
        # Now do the layout
        @my_panel_sizer = BoxSizer.new(VERTICAL)
        @my_panel.set_sizer(@my_panel_sizer)
        @my_panel_sizer.add(@my_label, 0, GROW|ALL, 2)
        @my_panel_sizer.add(@my_textbox, 0, GROW|ALL, 2)
        @my_panel_sizer.add(@my_combo, 0, GROW|ALL, 2)
        @my_panel_sizer.add(@my_button, 0, GROW|ALL, 2)        
        show()
    end
    
    def my_button_click(event)
        # Your code here
    end
end

class MyApp < App
    def on_init
        MyFrame.new
    end
end

MyApp.new.main_loop()


Further details and discussion here.

Java - Mini Lettore Wav

// Piccolissimo Lettore Audio per Java

package Multimedia;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.metal.MetalLookAndFeel;

public class AudioSystem
{
	public static void main(String[] args)
	{
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		
		try
		{
			UIManager.setLookAndFeel(new MetalLookAndFeel());
		}
		catch(UnsupportedLookAndFeelException e)
		{
			e.printStackTrace();
		}
		
		new AudioGUI();
	}
}


package Multimedia;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class AudioGUI extends JFrame implements WindowListener
{
	public AudioGUI()
	{
		this.setTitle("AudioSystem");
		this.setSize(240, 100);
		this.setResizable(false);
		
		this.addWindowListener(this);
		
		Container gc = this.getContentPane();
		gc.add(new AudioGUIPan());
		
		this.setVisible(true);
	}

	public void windowOpened(WindowEvent e)
	{
		
	}

	public void windowClosing(WindowEvent e)
	{
		System.exit(0);
	}

	public void windowClosed(WindowEvent e)
	{
		
	}

	public void windowIconified(WindowEvent e)
	{
		
	}

	public void windowDeiconified(WindowEvent e)
	{
		
	}

	public void windowActivated(WindowEvent e)
	{
		
	}

	public void windowDeactivated(WindowEvent e)
	{
		
	}
}

class AudioGUIPan extends JPanel implements ActionListener
{
	private JButton play;
	private JButton stop;
	private JButton loop;
	private JButton open;
	private JButton close;
	
	private JFileChooser jfc;
	
	private String file;
	
	private AudioDevice ad;
	
	public AudioGUIPan()
	{
		play = new JButton("Play");
		play.addActionListener(this);
		stop = new JButton("Stop");
		stop.addActionListener(this);
		open = new JButton("Open");
		open.addActionListener(this);
		close = new JButton("Close");
		close.addActionListener(this);
		
		this.add(stop);
		this.add(play);
		this.add(open);
		this.add(close);
	}

	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == close)
		{
			System.exit(0);
		}
		else if(e.getSource() == open)
		{
			jfc = new JFileChooser();
			jfc.setFileFilter(new AudioFilter());
			
			if(jfc.showOpenDialog(this) != JFileChooser.CANCEL_OPTION)
			{
				file = jfc.getSelectedFile().getAbsolutePath();
				ad = new AudioDevice(new File(file));
			}
		}
		else if(e.getSource() == play)
		{
			if(ad != null)
				ad.play();
		}
		else if(e.getSource() == stop)
		{
			if(ad != null)
				ad.stop();
		}
	}
}


package Multimedia;
import java.io.File;

import javax.swing.filechooser.FileFilter;

public class AudioFilter extends FileFilter
{
	public boolean accept(File f)
	{
		return f.getName().toLowerCase().endsWith(".wav") || f.isDirectory();
	}

	public String getDescription()
	{
		return "Audio File *.wav";
	}
}


package Multimedia;

import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
import java.net.MalformedURLException;

public class AudioDevice implements AudioClip
{
	private AudioClip ac;
	public AudioDevice(File f)
	{		
		try
		{
			ac = Applet.newAudioClip(f.toURL());
		}
		catch(MalformedURLException e)
		{
			e.printStackTrace();
		}
	}
	
	public void play()
	{
		ac.play();
	}

	public void stop()
	{
		ac.stop();
	}

	public void loop()
	{
		
	}
}

Java - UIManager all-windows

// Imposto il tema Metal non solo all'interno delle finestre di Java, ma anche al suo esterno...

    // Con queste definizioni dico che tutti i frame e i dialog prendono il tema Metal
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);
    //

    try
    {
        // Con questa imposto il tema
	UIManager.setLookAndFeel(new MetalLookAndFeel());
    }
    catch(UnsupportedLookAndFeelException e)
    {
	e.printStackTrace();
    }
    // In questo modo tutte le finestre avranno il tema Metal

Drawing an arrow cursor

Calling polygon may be faster than a blit.
x,y = 20,20
arrow = [(0,0), (0,10), (2,8), (4,12), (6,11), (4,7), (7,7)]
canvas.polygon([(x+dx,y+dy) for dx,dy in arrow], 0, 0xffffff)

Here's an example app. It let you move cursor around.
When you press the select key, a red circle is drawn.
The longer you press, the bigger the circle.

Start with the usual stuff
import e32
from appuifw import *
from key_codes import *

class Keyboard(object):
    def __init__(self):
        self.state = {}  # is this key pressing ?
        self.buffer= {}  # is it waiting to be processed ?
    def handle_event(self, event): # for event_callback
        code = event['scancode']
        if event['type'] == EEventKeyDown:
            self.buffer[code]= 1   # put into queue
            self.state[code] = 1
        elif event['type'] == EEventKeyUp:
            self.state[code] = 0
    def pressing(self, code):      # just check
        return self.state.get(code,0)
    def pressed(self, code):       # check and process the event
        if self.buffer.get(code,0):
            self.buffer[code] = 0  # take out of queue
            return 1
        return 0

key = Keyboard()
app.body = canvas = Canvas(event_callback=key.handle_event)

def quit():
    global running
    running = 0

app.exit_key_handler = quit
running = 1

Then the main part of this specific app.
x,y = 20,20
arrow = [(0,0), (0,10), (2,8), (4,12), (6,11), (4,7), (7,7)]

while running:
    if key.pressing(EScancodeUpArrow):
        y -= 1
    if key.pressing(EScancodeDownArrow):
        y += 1
    if key.pressing(EScancodeLeftArrow):
        x -= 1
    if key.pressing(EScancodeRightArrow):
        x += 1
    if key.pressed(EScancodeSelect):
        r = 1
        while key.pressing(EScancodeSelect):
            r += 1       # bigger red circle
            canvas.ellipse([(x-r,y-r),(x+r,y+r)], fill=0xff0000)
            canvas.polygon([(x+dx,y+dy) for dx,dy in arrow], 0, 0xffffff)
            e32.ao_sleep(0.03)

    canvas.clear()
    canvas.polygon([(x+dx,y+dy) for dx,dy in arrow], 0, 0xffffff)
    e32.ao_sleep(0.01)

Using icons in Listbox

pys60 1.1.3 provide some graphics capabilitis.
One of them is using icons in Listbox
(code taken from pys60 API reference)
icon1 = appuifw.Icon(u"z:\\system\\data\\avkon.mbm", 28, 29)
icon2 = appuifw.Icon(u"z:\\system\\data\\avkon.mbm ", 40, 41)
entries = [(u"Signal", icon1),
           (u"Battery", icon2)]
lb = appuifw.Listbox(entries, lbox_observe)

Listbox is one of the 3 types that can be assigned to app.body
(i.e. Text, Listbox, Canvas)
So, to make code above able to run, you must add
appuifw.app.body = lb

and the function lbox_observe that will handle the selection
need to be defined.

Borderless JButton

To create a borderless JButton in swing use the following:

JButton taskButton = new JButton(action);
taskButton.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
taskButton.setHorizontalAlignment(JButton.LEADING); // optional
taskButton.setBorderPainted(false);
taskButton.setContentAreaFilled(false);


This is useful for "hyperlink" style buttons or "task buttons".
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS