diff --git a/WiiUseJ/src/wiiusej/test/NunchukGuiTest.java b/WiiUseJ/src/wiiusej/test/NunchukGuiTest.java new file mode 100644 index 0000000..be6a495 --- /dev/null +++ b/WiiUseJ/src/wiiusej/test/NunchukGuiTest.java @@ -0,0 +1,360 @@ +/** + * This file is part of WiiuseJ. + * + * WiiuseJ is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * WiiuseJ is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with WiiuseJ. If not, see . + */ +package wiiusej.test; + +import wiiusej.Wiimote; +import wiiusej.utils.AccelerationExpansionEventPanel; +import wiiusej.utils.AccelerationPanel; +import wiiusej.utils.GForceExpansionEventPanel; +import wiiusej.utils.GForcePanel; +import wiiusej.utils.NunchukJoystickEventPanel; +import wiiusej.utils.OrientationExpansionEventPanel; +import wiiusej.utils.OrientationPanel; +import wiiusej.wiiusejevents.physicalevents.ExpansionEvent; +import wiiusej.wiiusejevents.physicalevents.IREvent; +import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent; +import wiiusej.wiiusejevents.physicalevents.NunchukButtonsEvent; +import wiiusej.wiiusejevents.physicalevents.NunchukEvent; +import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent; +import wiiusej.wiiusejevents.utils.WiimoteListener; +import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent; +import wiiusej.wiiusejevents.wiiuseapievents.NunchukInsertedEvent; +import wiiusej.wiiusejevents.wiiuseapievents.NunchukRemovedEvent; +import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent; + +/** + * + * @author guiguito + */ +public class NunchukGuiTest extends javax.swing.JFrame implements WiimoteListener { + + private Wiimote wiimote; + private boolean isThresholdsRequested = true; + + /** Creates new form NunchukGuiTest */ + public NunchukGuiTest(Wiimote wiimote) { + initComponents(); + this.wiimote = wiimote; + registerListeners(); + } + + private void registerListeners() { + wiimote.addWiiMoteEventListeners(this); + wiimote.addWiiMoteEventListeners((OrientationPanel) orientationPanel); + wiimote.addWiiMoteEventListeners((GForcePanel) gForcePanel); + wiimote.addWiiMoteEventListeners((AccelerationPanel) rawAccelerationPanel); + wiimote.addWiiMoteEventListeners((NunchukJoystickEventPanel)joystickEventsPanel); + } + + public void unRegisterListeners() { + wiimote.removeWiiMoteEventListeners((OrientationPanel) orientationPanel); + wiimote.removeWiiMoteEventListeners((GForcePanel) gForcePanel); + wiimote.removeWiiMoteEventListeners((AccelerationPanel) rawAccelerationPanel); + wiimote.removeWiiMoteEventListeners((NunchukJoystickEventPanel)joystickEventsPanel); + wiimote.removeWiiMoteEventListeners(this); + } + + public void requestThresholdsUpdate(){ + isThresholdsRequested = true; + } + + /** This method is called from within the constructor to + * initialize the form. + * WARNING: Do NOT modify this code. The content of this method is + * always regenerated by the Form Editor. + */ + // //GEN-BEGIN:initComponents + private void initComponents() { + + topPanels = new javax.swing.JPanel(); + joystickEventsPanel = joystickEventsPanel = new NunchukJoystickEventPanel(); + motionSensingEventsPanel = new javax.swing.JPanel(); + motionSensingEventsTabbedPanels = new javax.swing.JTabbedPane(); + rawAccelerationPanel = new AccelerationExpansionEventPanel(); + orientationPanel = new OrientationExpansionEventPanel(); + gForcePanel = new GForceExpansionEventPanel(); + setNunchukValuesPanel = new javax.swing.JPanel(); + nunchukButtonsEventPanel = new javax.swing.JPanel(); + cButton = new javax.swing.JButton(); + zButton = new javax.swing.JButton(); + nunchukOrientationPanel = new javax.swing.JPanel(); + nunchukOrientationTextField = new javax.swing.JTextField(); + nunchukOrientationButton = new javax.swing.JButton(); + nunchukAccelerationPanel = new javax.swing.JPanel(); + nunchukAccelerationTextField = new javax.swing.JTextField(); + nunchukAccelerationButton = new javax.swing.JButton(); + messagePanel = new javax.swing.JPanel(); + messageText = new javax.swing.JLabel(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + setMinimumSize(new java.awt.Dimension(400, 400)); + getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS)); + + topPanels.setMinimumSize(new java.awt.Dimension(400, 200)); + topPanels.setPreferredSize(new java.awt.Dimension(400, 200)); + topPanels.setLayout(new javax.swing.BoxLayout(topPanels, javax.swing.BoxLayout.LINE_AXIS)); + + joystickEventsPanel.setBackground(new java.awt.Color(0, 0, 0)); + joystickEventsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(51, 153, 0), 2, true), "Joystick View", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(204, 102, 0))); + joystickEventsPanel.setToolTipText("JoystickEvent"); + joystickEventsPanel.setMinimumSize(new java.awt.Dimension(200, 200)); + + javax.swing.GroupLayout joystickEventsPanelLayout = new javax.swing.GroupLayout(joystickEventsPanel); + joystickEventsPanel.setLayout(joystickEventsPanelLayout); + joystickEventsPanelLayout.setHorizontalGroup( + joystickEventsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 601, Short.MAX_VALUE) + ); + joystickEventsPanelLayout.setVerticalGroup( + joystickEventsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 174, Short.MAX_VALUE) + ); + + topPanels.add(joystickEventsPanel); + joystickEventsPanel.getAccessibleContext().setAccessibleName("Joystick"); + + motionSensingEventsPanel.setMinimumSize(new java.awt.Dimension(200, 200)); + + rawAccelerationPanel.setToolTipText("Nunchuk MotionSensingEvent"); + + javax.swing.GroupLayout rawAccelerationPanelLayout = new javax.swing.GroupLayout(rawAccelerationPanel); + rawAccelerationPanel.setLayout(rawAccelerationPanelLayout); + rawAccelerationPanelLayout.setHorizontalGroup( + rawAccelerationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 597, Short.MAX_VALUE) + ); + rawAccelerationPanelLayout.setVerticalGroup( + rawAccelerationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 175, Short.MAX_VALUE) + ); + + motionSensingEventsTabbedPanels.addTab("RawAcceleration", rawAccelerationPanel); + + javax.swing.GroupLayout orientationPanelLayout = new javax.swing.GroupLayout(orientationPanel); + orientationPanel.setLayout(orientationPanelLayout); + orientationPanelLayout.setHorizontalGroup( + orientationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 597, Short.MAX_VALUE) + ); + orientationPanelLayout.setVerticalGroup( + orientationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 175, Short.MAX_VALUE) + ); + + motionSensingEventsTabbedPanels.addTab("Orientation", orientationPanel); + + javax.swing.GroupLayout gForcePanelLayout = new javax.swing.GroupLayout(gForcePanel); + gForcePanel.setLayout(gForcePanelLayout); + gForcePanelLayout.setHorizontalGroup( + gForcePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 597, Short.MAX_VALUE) + ); + gForcePanelLayout.setVerticalGroup( + gForcePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 175, Short.MAX_VALUE) + ); + + motionSensingEventsTabbedPanels.addTab("GForce", gForcePanel); + + javax.swing.GroupLayout motionSensingEventsPanelLayout = new javax.swing.GroupLayout(motionSensingEventsPanel); + motionSensingEventsPanel.setLayout(motionSensingEventsPanelLayout); + motionSensingEventsPanelLayout.setHorizontalGroup( + motionSensingEventsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(motionSensingEventsTabbedPanels, javax.swing.GroupLayout.DEFAULT_SIZE, 602, Short.MAX_VALUE) + ); + motionSensingEventsPanelLayout.setVerticalGroup( + motionSensingEventsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(motionSensingEventsTabbedPanels, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE) + ); + + topPanels.add(motionSensingEventsPanel); + + getContentPane().add(topPanels); + + setNunchukValuesPanel.setMinimumSize(new java.awt.Dimension(400, 200)); + setNunchukValuesPanel.setPreferredSize(new java.awt.Dimension(400, 200)); + setNunchukValuesPanel.setLayout(new javax.swing.BoxLayout(setNunchukValuesPanel, javax.swing.BoxLayout.Y_AXIS)); + + nunchukButtonsEventPanel.setToolTipText("Nunchuk ButtonsEvent"); + nunchukButtonsEventPanel.setMinimumSize(new java.awt.Dimension(100, 100)); + nunchukButtonsEventPanel.setPreferredSize(new java.awt.Dimension(100, 100)); + nunchukButtonsEventPanel.setLayout(new javax.swing.BoxLayout(nunchukButtonsEventPanel, javax.swing.BoxLayout.LINE_AXIS)); + + cButton.setText("C"); + cButton.setMaximumSize(new java.awt.Dimension(50, 50)); + cButton.setMinimumSize(new java.awt.Dimension(50, 50)); + cButton.setPreferredSize(new java.awt.Dimension(50, 50)); + nunchukButtonsEventPanel.add(cButton); + + zButton.setText("Z"); + zButton.setMaximumSize(new java.awt.Dimension(50, 50)); + zButton.setMinimumSize(new java.awt.Dimension(50, 50)); + zButton.setPreferredSize(new java.awt.Dimension(50, 50)); + nunchukButtonsEventPanel.add(zButton); + + setNunchukValuesPanel.add(nunchukButtonsEventPanel); + + nunchukOrientationTextField.setPreferredSize(new java.awt.Dimension(60, 20)); + nunchukOrientationPanel.add(nunchukOrientationTextField); + + nunchukOrientationButton.setText("Set Orientation Threshold"); + nunchukOrientationButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mousePressed(java.awt.event.MouseEvent evt) { + nunchukOrientationButtonMousePressed(evt); + } + }); + nunchukOrientationPanel.add(nunchukOrientationButton); + + setNunchukValuesPanel.add(nunchukOrientationPanel); + + nunchukAccelerationTextField.setPreferredSize(new java.awt.Dimension(60, 20)); + nunchukAccelerationPanel.add(nunchukAccelerationTextField); + + nunchukAccelerationButton.setText("Set Acceleration Threshold"); + nunchukAccelerationButton.addMouseListener(new java.awt.event.MouseAdapter() { + public void mousePressed(java.awt.event.MouseEvent evt) { + nunchukAccelerationButtonMousePressed(evt); + } + }); + nunchukAccelerationPanel.add(nunchukAccelerationButton); + + setNunchukValuesPanel.add(nunchukAccelerationPanel); + + messageText.setText("Message:"); + + javax.swing.GroupLayout messagePanelLayout = new javax.swing.GroupLayout(messagePanel); + messagePanel.setLayout(messagePanelLayout); + messagePanelLayout.setHorizontalGroup( + messagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 1216, Short.MAX_VALUE) + .addGroup(messagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(messagePanelLayout.createSequentialGroup() + .addGap(0, 0, Short.MAX_VALUE) + .addComponent(messageText) + .addGap(0, 0, Short.MAX_VALUE))) + ); + messagePanelLayout.setVerticalGroup( + messagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 34, Short.MAX_VALUE) + .addGroup(messagePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(messagePanelLayout.createSequentialGroup() + .addGap(0, 0, Short.MAX_VALUE) + .addComponent(messageText) + .addGap(0, 0, Short.MAX_VALUE))) + ); + + setNunchukValuesPanel.add(messagePanel); + + getContentPane().add(setNunchukValuesPanel); + + pack(); + }// //GEN-END:initComponents + private void nunchukOrientationButtonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nunchukOrientationButtonMousePressed + try { + float nb = Float.parseFloat(nunchukOrientationTextField.getText()); + wiimote.setNunchukOrientationThreshold(nb); + messageText.setText("Nunchuk orientation threshold set to " + nb); + } catch (NumberFormatException e) { + messageText.setText("Number is not an integer, nunchuk orientation threshold not set !"); + } + }//GEN-LAST:event_nunchukOrientationButtonMousePressed + + private void nunchukAccelerationButtonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nunchukAccelerationButtonMousePressed + try { + int nb = Integer.parseInt(nunchukAccelerationTextField.getText()); + wiimote.setNunchukAccelerationThreshold(nb); + messageText.setText("Nunchuk acceleration threshold set to " + nb); + } catch (NumberFormatException e) { + messageText.setText("Number is not an integer, nunchuk acceleration threshold not set !"); + } + }//GEN-LAST:event_nunchukAccelerationButtonMousePressed + + public void onButtonsEvent(WiimoteButtonsEvent arg0) { + //nothing to do + } + + public void onIrEvent(IREvent arg0) { + //nothing to do + } + + public void onMotionSensingEvent(MotionSensingEvent arg0) { + //nothing to do + } + + public void onExpansionEvent(ExpansionEvent arg0) { + if (arg0 instanceof NunchukEvent) { + NunchukEvent nunchuk = (NunchukEvent) arg0; + NunchukButtonsEvent buttons = nunchuk.getButtonsEvent(); + if (buttons.isButtonCJustPressed()) { + cButton.setEnabled(false); + } + if (buttons.isButtonCJustReleased()) { + cButton.setEnabled(true); + } + if (buttons.isButtonZJustPressed()) { + zButton.setEnabled(false); + } + if (buttons.isButtonZJustReleased()) { + zButton.setEnabled(true); + } + if (isThresholdsRequested){ + MotionSensingEvent evt = nunchuk.getNunchukMotionSensingEvent(); + nunchukAccelerationTextField.setText(evt.getAccelerationThreshold()+""); + nunchukOrientationTextField.setText(evt.getOrientationThreshold()+""); + isThresholdsRequested = false; + } + } + } + + public void onStatusEvent(StatusEvent arg0) { + //nothing to do + } + + public void onDisconnectionEvent(DisconnectionEvent arg0) { + //nothing + } + + public void onNunchukInsertedEvent(NunchukInsertedEvent arg0) { + //nothing + } + + public void onNunchukRemovedEvent(NunchukRemovedEvent arg0) { + //nothing + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton cButton; + private javax.swing.JPanel gForcePanel; + private javax.swing.JPanel joystickEventsPanel; + private javax.swing.JPanel messagePanel; + private javax.swing.JLabel messageText; + private javax.swing.JPanel motionSensingEventsPanel; + private javax.swing.JTabbedPane motionSensingEventsTabbedPanels; + private javax.swing.JButton nunchukAccelerationButton; + private javax.swing.JPanel nunchukAccelerationPanel; + private javax.swing.JTextField nunchukAccelerationTextField; + private javax.swing.JPanel nunchukButtonsEventPanel; + private javax.swing.JButton nunchukOrientationButton; + private javax.swing.JPanel nunchukOrientationPanel; + private javax.swing.JTextField nunchukOrientationTextField; + private javax.swing.JPanel orientationPanel; + private javax.swing.JPanel rawAccelerationPanel; + private javax.swing.JPanel setNunchukValuesPanel; + private javax.swing.JPanel topPanels; + private javax.swing.JButton zButton; + // End of variables declaration//GEN-END:variables +} diff --git a/WiiUseJ/src/wiiusej/utils/AccelerationExpansionEventPanel.java b/WiiUseJ/src/wiiusej/utils/AccelerationExpansionEventPanel.java new file mode 100644 index 0000000..ebc3dbe --- /dev/null +++ b/WiiUseJ/src/wiiusej/utils/AccelerationExpansionEventPanel.java @@ -0,0 +1,37 @@ +/** + * This file is part of WiiuseJ. + * + * WiiuseJ is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * WiiuseJ is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with WiiuseJ. If not, see . + */ +package wiiusej.utils; + +import wiiusej.values.RawAcceleration; +import wiiusej.wiiusejevents.GenericEvent; +import wiiusej.wiiusejevents.physicalevents.NunchukEvent; + +/** + * + * @author guiguito + */ +public class AccelerationExpansionEventPanel extends AccelerationPanel{ + + @Override + public RawAcceleration getRawAccelerationValue(GenericEvent e) { + if (e instanceof NunchukEvent){ + return ((NunchukEvent)e).getNunchukMotionSensingEvent().getRawAcceleration(); + } + return null; + } + +} diff --git a/WiiUseJ/src/wiiusej/utils/AccelerationWiimoteEventPanel.java b/WiiUseJ/src/wiiusej/utils/AccelerationWiimoteEventPanel.java new file mode 100644 index 0000000..25883f1 --- /dev/null +++ b/WiiUseJ/src/wiiusej/utils/AccelerationWiimoteEventPanel.java @@ -0,0 +1,37 @@ +/** + * This file is part of WiiuseJ. + * + * WiiuseJ is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * WiiuseJ is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with WiiuseJ. If not, see . + */ +package wiiusej.utils; + +import wiiusej.values.RawAcceleration; +import wiiusej.wiiusejevents.GenericEvent; +import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent; + +/** + * + * @author guiguito + */ +public class AccelerationWiimoteEventPanel extends AccelerationPanel{ + + @Override + public RawAcceleration getRawAccelerationValue(GenericEvent e) { + if (e instanceof MotionSensingEvent){ + return ((MotionSensingEvent)e).getRawAcceleration(); + } + return null; + } + +} diff --git a/WiiUseJ/src/wiiusej/utils/GForceExpansionEventPanel.java b/WiiUseJ/src/wiiusej/utils/GForceExpansionEventPanel.java new file mode 100644 index 0000000..5493520 --- /dev/null +++ b/WiiUseJ/src/wiiusej/utils/GForceExpansionEventPanel.java @@ -0,0 +1,37 @@ +/** + * This file is part of WiiuseJ. + * + * WiiuseJ is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * WiiuseJ is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with WiiuseJ. If not, see . + */ +package wiiusej.utils; + +import wiiusej.values.GForce; +import wiiusej.wiiusejevents.GenericEvent; +import wiiusej.wiiusejevents.physicalevents.NunchukEvent; + +/** + * + * @author guiguito + */ +public class GForceExpansionEventPanel extends GForcePanel{ + + @Override + public GForce getGForceValue(GenericEvent e) { + if (e instanceof NunchukEvent){ + return ((NunchukEvent)e).getNunchukMotionSensingEvent().getGforce(); + } + return null; + } + +} diff --git a/WiiUseJ/src/wiiusej/utils/JoystickEventPanel.java b/WiiUseJ/src/wiiusej/utils/JoystickEventPanel.java new file mode 100644 index 0000000..62b7a2a --- /dev/null +++ b/WiiUseJ/src/wiiusej/utils/JoystickEventPanel.java @@ -0,0 +1,224 @@ +/** + * This file is part of WiiuseJ. + * + * WiiuseJ is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * WiiuseJ is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with WiiuseJ. If not, see . + */ +package wiiusej.utils; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.RenderingHints; +import java.awt.Shape; +import java.awt.geom.AffineTransform; +import wiiusej.wiiusejevents.physicalevents.ExpansionEvent; +import wiiusej.wiiusejevents.physicalevents.IREvent; +import wiiusej.wiiusejevents.physicalevents.JoystickEvent; +import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent; +import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent; +import wiiusej.wiiusejevents.utils.WiimoteListener; +import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent; +import wiiusej.wiiusejevents.wiiuseapievents.NunchukInsertedEvent; +import wiiusej.wiiusejevents.wiiuseapievents.NunchukRemovedEvent; +import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent; + +/** + * Panel to display joystick events. + * @author guiguito + */ +public abstract class JoystickEventPanel extends javax.swing.JPanel implements WiimoteListener { + + private Image mImage;// image for double buffering + private Color backgroundColor = Color.BLACK; + private Color borderColor = Color.RED; + private Color pointColor = Color.RED; + private Shape shape = new java.awt.geom.Ellipse2D.Double(0, 0, 30, 30); + private JoystickEvent lastJoystickEvent = null; + + /** Creates new form JoystickPanel */ + public JoystickEventPanel() { + initComponents(); + } + + /** + * Constructor used to choose the colors used by the JoystickPanel. + * + * @param bgColor + * background color. + * @param pColor + * point color. + * @param bdColor + * border color for the shape. + * @param sh + * shape of what is drawn. + */ + public JoystickEventPanel(Color bgColor, Color pColor, Color bdColor, Shape sh) { + backgroundColor = bgColor; + pointColor = pColor; + shape = sh; + borderColor = bdColor; + initComponents(); + } + + @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); + g2.setTransform(new AffineTransform()); + + //compute center + int xCenter = (int) Math.round(d.getWidth() / 2.0); + int yCenter = (int) Math.round(d.getHeight() / 2.0); + + //compute coordinates + if (lastJoystickEvent != null) { + double xAng = Math.sin(lastJoystickEvent.getAngle() * Math.PI / 180.0) * lastJoystickEvent.getMagnitude(); + double yAng = Math.cos(lastJoystickEvent.getAngle() * Math.PI / 180.0) * lastJoystickEvent.getMagnitude(); + int dx = (int) Math.round(shape.getBounds().getWidth() / 2); + int dy = (int) Math.round(shape.getBounds().getHeight() / 2); + double xTemp = xAng * (xCenter - dx * 2); + double yTemp = yAng * (yCenter - dy * 2); + int x = xCenter - dx + (int) xTemp; + int y = yCenter - dy - (int) yTemp; +// System.out.println("--------------------------------------------------------------------"); +// System.out.println(lastJoystickEvent); +// System.out.println("xCenter ,yCenter : "+xCenter+" , "+yCenter); +// System.out.println("xAng, yAng : "+xAng+" , "+yAng); +// System.out.println("dx, dy : "+dx+" , "+dy); +// System.out.println("xTemp, yTemp : "+xTemp+" , "+yTemp); +// System.out.println("x, y : "+x+" , "+y); + //shape + g2.translate(x, y); + g2.setPaint(borderColor); + g2.draw(shape); + g2.setPaint(pointColor); + g2.fill(shape); + } + // put offscreen image on the screen + g.drawImage(mImage, 0, 0, null); + } + + /** + * check if the mImage variable has been initialized. If it's not the case + * it initializes it with the dimensions of the panel. mImage is for double + * buffering. + */ + 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 onButtonsEvent(WiimoteButtonsEvent arg0) { + //nothing + } + + public void onIrEvent(IREvent arg0) { + //nothing + } + + public void onMotionSensingEvent(MotionSensingEvent arg0) { + //nothing + } + + public void onExpansionEvent(ExpansionEvent arg0) { + JoystickEvent joy = getJoystikEvent(arg0); + if (joy != null) { + lastJoystickEvent = joy; + } + repaint(); + } + + public void onStatusEvent(StatusEvent arg0) { + //nothing + } + + public void onDisconnectionEvent(DisconnectionEvent arg0) { + //nothing + } + + public void onNunchukInsertedEvent(NunchukInsertedEvent arg0) { + //nothing + } + + public void onNunchukRemovedEvent(NunchukRemovedEvent arg0) { + //nothing + } + + public Color getBackgroundColor() { + return backgroundColor; + } + + public Color getPointColor() { + return pointColor; + } + + public Color getBorderColor() { + return borderColor; + } + + public Shape getShape() { + return shape; + } + + public void setBackgroundColor(Color backgroundColor) { + this.backgroundColor = backgroundColor; + } + + public void setPointColor(Color pointColor) { + this.pointColor = pointColor; + } + + public void setBorderColor(Color borderColor) { + this.borderColor = borderColor; + } + + public void setShape(Shape shape) { + this.shape = shape; + } + + public abstract JoystickEvent getJoystikEvent(ExpansionEvent e); + + /** This method is called from within the constructor to + * initialize the form. + * WARNING: Do NOT modify this code. The content of this method is + * always regenerated by the Form Editor. + */ + // //GEN-BEGIN:initComponents + private void initComponents() { + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); + this.setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 400, Short.MAX_VALUE) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 300, Short.MAX_VALUE) + ); + }// //GEN-END:initComponents + // Variables declaration - do not modify//GEN-BEGIN:variables + // End of variables declaration//GEN-END:variables +} diff --git a/WiiUseJ/src/wiiusej/utils/NunchukJoystickEventPanel.java b/WiiUseJ/src/wiiusej/utils/NunchukJoystickEventPanel.java new file mode 100644 index 0000000..b2fbba4 --- /dev/null +++ b/WiiUseJ/src/wiiusej/utils/NunchukJoystickEventPanel.java @@ -0,0 +1,36 @@ +/** + * This file is part of WiiuseJ. + * + * WiiuseJ is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * WiiuseJ is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with WiiuseJ. If not, see . + */ +package wiiusej.utils; + +import wiiusej.wiiusejevents.physicalevents.ExpansionEvent; +import wiiusej.wiiusejevents.physicalevents.JoystickEvent; +import wiiusej.wiiusejevents.physicalevents.NunchukEvent; + +/** + * Panel to display nunchuk joystick events. + * @author guiguito + */ +public class NunchukJoystickEventPanel extends JoystickEventPanel { + + @Override + public JoystickEvent getJoystikEvent(ExpansionEvent e) { + if (e instanceof NunchukEvent) { + return ((NunchukEvent) e).getNunchukJoystickEvent(); + } + return null; + } +} diff --git a/WiiUseJ/src/wiiusej/utils/OrientationExpansionEventPanel.java b/WiiUseJ/src/wiiusej/utils/OrientationExpansionEventPanel.java new file mode 100644 index 0000000..e1ed7ce --- /dev/null +++ b/WiiUseJ/src/wiiusej/utils/OrientationExpansionEventPanel.java @@ -0,0 +1,36 @@ +/** + * This file is part of WiiuseJ. + * + * WiiuseJ is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * WiiuseJ is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with WiiuseJ. If not, see . + */ +package wiiusej.utils; + +import wiiusej.values.Orientation; +import wiiusej.wiiusejevents.GenericEvent; +import wiiusej.wiiusejevents.physicalevents.NunchukEvent; + +/** + * + * @author guiguito + */ +public class OrientationExpansionEventPanel extends OrientationPanel { + + @Override + public Orientation getOrientationValue(GenericEvent e) { + if (e instanceof NunchukEvent) { + return ((NunchukEvent) e).getNunchukMotionSensingEvent().getOrientation(); + } + return null; + } +} diff --git a/WiiUseJ/src/wiiusej/utils/OrientationWiimoteEventPanel.java b/WiiUseJ/src/wiiusej/utils/OrientationWiimoteEventPanel.java new file mode 100644 index 0000000..5aebd8f --- /dev/null +++ b/WiiUseJ/src/wiiusej/utils/OrientationWiimoteEventPanel.java @@ -0,0 +1,37 @@ +/** + * This file is part of WiiuseJ. + * + * WiiuseJ is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * WiiuseJ is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with WiiuseJ. If not, see . + */ +package wiiusej.utils; + +import wiiusej.values.Orientation; +import wiiusej.wiiusejevents.GenericEvent; +import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent; + +/** + * + * @author guiguito + */ +public class OrientationWiimoteEventPanel extends OrientationPanel{ + + @Override + public Orientation getOrientationValue(GenericEvent e) { + if (e instanceof MotionSensingEvent){ + return ((MotionSensingEvent)e).getOrientation(); + } + return null; + } + +}