> Hi all,
>
[quoted text clipped - 7 lines]
> Thanks in advance,
> Michel
Michel:
It is easy enough to write one yourself but here are two classes that I
wrote to set a heading for a robot. The HeadingSelect class take five
BufferedImages for the background and left and right buttons. To try
this out you should be able to write a simple main() for HeadingSelect
that loads the five images and displays HeadingSelect in a JFrame.
package roamer;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.text.*;
import javax.imageio.*;
import javax.swing.*;
public class HeadingSelect extends JPanel implements ActionListener {
final static RenderingHints ANTI_ALIAS = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
final static Font font = new Font("AuraRegular",Font.PLAIN,18);
boolean enabledState = false;
MouseListener lml,rml;
public ButtonPanel left,right;
javax.swing.Timer timer;
volatile int heading = 0;
int direction;
BufferedImage bgImage;
public HeadingSelect(BufferedImage bgImage, BufferedImage leftImage,
BufferedImage leftImagePressed, BufferedImage rightImage,
BufferedImage rightImagePressed) {
this.bgImage = bgImage;
setLayout(null);
setSize(300,318);
setLocation(0,450);
timer = new javax.swing.Timer(15,this);
timer.setInitialDelay(500);
left = new ButtonPanel(leftImage,leftImagePressed);
left.setLocation(10,200);
left.setSize(135,80);
lml = new MouseAdapter() {
public void mousePressed(MouseEvent me) {
--heading;
direction = -1;
repaint();
timer.start();
}
public void mouseReleased(MouseEvent me) {
repaint();
timer.stop();
}
};
left.addMouseListener(lml);
add(left);
right = new ButtonPanel(rightImage,rightImagePressed);
right.setLocation(155,200);
right.setSize(135,80);
rml = new MouseAdapter() {
public void mousePressed(MouseEvent me) {
++heading;
direction = 1;
repaint();
timer.start();
}
public void mouseReleased(MouseEvent me) {
repaint();
timer.stop();
}
};
right.addMouseListener(rml);
add(right);
enabledState = true;
}
public void setEnabled(boolean enable) {
if (enable) {
if (!enabledState) {
left.addMouseListener(lml);
right.addMouseListener(rml);
enabledState = true;
}
} else {
left.removeMouseListener(lml);
right.removeMouseListener(rml);
enabledState = false;
}
}
public int getHeading() {
heading %= 360;
if (heading < 0)
heading += 360;
return heading;
}
public void setHeading(int heading) {
this.heading = heading;
}
public void actionPerformed(ActionEvent ae) {
heading += direction;
repaint();
}
public void paintComponent(Graphics g) {
// super.paintComponent(g);
int width = getWidth();
int height = getHeight();
Graphics2D g2D = (Graphics2D)g;
g2D.addRenderingHints(ANTI_ALIAS);
g2D.drawImage(bgImage,0,0,this); //draw the background
g2D.setColor(Color.WHITE);
g2D.setFont(font);
g.drawOval(80,30,140,140);
for (int i=0; i<360; i+=5) {
int x1 = (int)(70.0*Math.cos(Math.toRadians(i)));
int y1 = (int)(70.0*Math.sin(Math.toRadians(i)));
int x2 =
(int)((i%10==0?80.0:75.0)*Math.cos(Math.toRadians(i)));
int y2 =
(int)((i%10==0?80.0:75.0)*Math.sin(Math.toRadians(i)));
g2D.drawLine(x1+150,y1+100,x2+150,y2+100);
}
g2D.drawString("000",133,15);
g2D.drawString("180",138,195);
g2D.drawString("090",235,105);
g2D.drawString("270",33,105);
g2D.drawString("045",205,40);
g2D.drawString("135",205,170);
g2D.drawString("225",63,170);
g2D.drawString("315",67,40);
int x = (int)(70.0*Math.cos(Math.toRadians(heading-90)));
int y = (int)(70.0*Math.sin(Math.toRadians(heading-90)));
g2D.drawLine(150,100,x+150,y+100);
int x2 = (int)(10.0*Math.cos(Math.toRadians(heading-60)));
int y2 = (int)(10.0*Math.sin(Math.toRadians(heading-60)));
int x3 = (int)(10.0*Math.cos(Math.toRadians(heading-120)));
int y3 = (int)(10.0*Math.sin(Math.toRadians(heading-120)));
g2D.drawLine(x+150,y+100,x+150-x2,y+100-y2);
g2D.drawLine(x+150,y+100,x+150-x3,y+100-y3);
g2D.setColor(Color.WHITE);
g2D.setFont(font.deriveFont(32.0f));
String str = "SELECT HEADING";
FontMetrics fm = g2D.getFontMetrics();
int stringWidth = fm.stringWidth(str);
int stringHeight = fm.getHeight();
g2D.drawString(str,(width - stringWidth)/2,310);
heading %= 360;
if (heading < 0)
heading += 360;
DecimalFormat df = new DecimalFormat("000");
String headingStr = df.format(heading);
g2D.drawString(headingStr,120,109);
}
}
package roamer;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class ButtonPanel extends JPanel {
final static RenderingHints ANTI_ALIAS = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
volatile boolean mPressed = false;
BufferedImage image1,image2;
public ButtonPanel(BufferedImage image1, BufferedImage image2) {
this.image1 = image1;
this.image2 = image2;
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
mPressed = true;
repaint();
}
public void mouseReleased(MouseEvent me) {
mPressed = false;
repaint();
}
});
}
public void paintComponent(Graphics g) {
if (mPressed)
g.drawImage(image2,0,0,this);
else
g.drawImage(image1,0,0,this);
}
}

Signature
Knute Johnson
email s/nospam/knute/
Molon labe...
Michel Guebel - 06 Apr 2004 22:24 GMT
I haven't tested your example yet. I'm still studying it to understand
how it will work. I'm looking now to get some compass pictures to be
displayed and how to write the main().
I'm newby with Java yet.
Many thanks for your help.
Best regards,
Michel
Knute Johnson a écrit :
>> Hi all,
>>
[quoted text clipped - 16 lines]
> this out you should be able to write a simple main() for HeadingSelect
> that loads the five images and displays HeadingSelect in a JFrame.
Knute Johnson - 07 Apr 2004 05:35 GMT
> I haven't tested your example yet. I'm still studying it to understand
> how it will work. I'm looking now to get some compass pictures to be
[quoted text clipped - 29 lines]
>> HeadingSelect that loads the five images and displays HeadingSelect in
>> a JFrame.
Michel:
Take all the code that refers to the BufferedImages out, take the
ButtonPanel code out and just create a main() in HeadingSelect that
creates a new HeadingSelect.
public static void main(String[] args) {
new HeadingSelect();
}
That will show you what the compass looks like. It is a very simple
representation. The project I worked on had some graphic designers that
did the background images but they are not necessary to see the idea.

Signature
Knute Johnson
email s/nospam/knute/
Molon labe...