hey everyone, i am creating a project from already created code below. this is just one class of it.
this class projects 4 or less points on the image. the source for those points is received from wii (gaming consoles) remote that tracks infra-red light so it displays infra-red light coordinates on the screen and represents it as dots.
what i need is : in the paint component when it is drawing the points i dont clearly understand how all the points are drawn and what i would need is different variable for each point to store the coordinates so i could be able to work with each point seperatly.
i hope this explanation is clear enough, if not let me know,
thank you
public class IRPanel extends javax.swing.JPanel implements WiimoteListener {
private static int MAX_NB_POINTS = 4;
private Color color = Color.WHITE;
private Color backgroundColor = Color.BLUE;
private Color borderColor = Color.RED;
private Shape shape;
private Image mImage;// image for double buffering
private int[] xCoordinates;
private int[] yCoordinates;
private int nbPoints = -1;
public IRPanel() {
shape = new Ellipse2D.Double(0, 0, 10, 10);
initArrays();
initComponents();
}
public IRPanel(Color bgColor, Color ptColor, Color bdColor, Shape sh) {
backgroundColor = bgColor;
color = ptColor;
borderColor = bdColor;
shape = sh;
initArrays();
initComponents();
}
private void initArrays() {
xCoordinates = new int[MAX_NB_POINTS];
yCoordinates = new int[MAX_NB_POINTS];
for (int i = 0; i < MAX_NB_POINTS; i++) {
xCoordinates[i] = -1;
yCoordinates[i] = -1;
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension d = getSize();
checkOffScreenImage();
Graphics offG = mImage.getGraphics();
offG.setColor(backgroundColor);
offG.fillRect(0, 0, d.width, d.height);
Graphics2D g2 = (Graphics2D) mImage.getGraphics();
// g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
// RenderingHints.VALUE_ANTIALIAS_ON);
//draw line
// draw points
int i = 0;
while (i < nbPoints) {
double x = xCoordinates[i];
double y = yCoordinates[i];
long xx = getWidth() - Math.round((double) getWidth() * x / 1024.0);
long yy = getHeight()- Math.round((double) getHeight() * y / 768.0);
g2.translate(xx, yy);
int xa = xCoordinates[i];
int xb = yCoordinates[i];
g2.drawOval(0,0,30,30);
g2.fillOval(0, 0, 30, 30);
g2.setPaint(borderColor);
//g2.draw(shape);
//g2.setPaint(color);
// g2.fill(shape);
g2.setTransform(new AffineTransform());
i++;
}
g.drawImage(mImage, 0, 0, null);
}
private void checkOffScreenImage() {
Dimension d = getSize();
if (mImage == null || mImage.getWidth(null) != d.width
|| mImage.getHeight(null) != d.height) {
mImage = createImage(d.width, d.height);
}
}
public void onIrEvent(IREvent arg0) {
// transfer points
wiiusej.values.IRSource[] points = arg0.getIRPoints();
nbPoints = points.length;
for (int i = 0; i < points.length; i++) {
xCoordinates[i] = (int) points[i].getRx();
yCoordinates[i] = (int) points[i].getRy();
}
for (int i = points.length; i < MAX_NB_POINTS; i++) {
xCoordinates[i] = -1;
yCoordinates[i] = -1;
}
// redraw panel
repaint();
}
public void onDisconnectionEvent(DisconnectionEvent arg0) {
// clear previous points
for (int i = 0; i < MAX_NB_POINTS; i++) {
xCoordinates[i] = -1;
yCoordinates[i] = -1;
}
// redraw panel
repaint();
}
public Color getBackgroundColor() {
return backgroundColor;
}
public Color getBorderColor() {
return borderColor;
}
public Color getColor() {
return color;
}
public Shape getShape() {
return shape;
}
public void setBackgroundColor(Color backgroundColor) {
this.backgroundColor = backgroundColor;
}
public void setBorderColor(Color borderColor) {
this.borderColor = borderColor;
}
public void setColor(Color color) {
this.color = color;
}
public void setShape(Shape shape) {
this.shape = shape;
}
public void clearView() {
initArrays();
repaint();
}
private void initComponents() {
GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 700,Short.MAX_VALUE));
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 300,Short.MAX_VALUE));
}
}