Well, the problem is the... well, okay I'll do my best to produce
something real quick.
This is rather long. It works on JBuilder X. In this example, the
videoGui works as intended, the componentResize is issued after the
user has finished resizing. If, inside main() you issue a
UIManager.setDefaultLookAndFeel( insert lookandfeel here ) and a
javax.swing.JFrame.setDefaultLookAndFeelDecorated( true ); then you
should see what I am talking about with the resizing issues.
Finally, here's the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class videoGui
extends JFrame
implements ActionListener {
public static videoGui videoControl;
public int fvnumber; /* Ufg ProcId */
public int mixernumber; /* Mixer ProcId */
private int fvtype; /* ??? */
private String fvtypeS; /* String for fvtype */
private String titleString = "FV3 Control Window ";
public String titleLabel = "";
public String window_char;
private int screenNum = 0;
public int OffSetX = 0;
public int OffSetY = 0;
public Rectangle normalbounds = new Rectangle();
public String popups = "centerscreen";
// Window size values
public int source_width;
public int source_height;
private Rectangle positRectangle;
private int diffHeight;
private int diffWidth;
private int oldHeight;
private int oldWidth;
public float ratio;
private boolean first_time = true;
private boolean mouse_down = false;
// Popup Menu Objects
private JPopupMenu FileMenu;
private JPopupMenu ControlMenu;
private JPopupMenu AttributesMenu;
private JPopupMenu InfoMenu;
private JPopupMenu MixingMenu;
private JPopupMenu MixerMenu;
private JPopupMenu HelpMenu;
// Generic Separator
private JSeparator Separator;
// Menu Selections for FileMenu
private JMenuItem FileCapture;
private JMenuItem FilePlayback;
private JMenuItem FileFullScreenCapture;
private JMenuItem FileHistory;
private JMenuItem FileHide;
private JMenuItem FileClose;
// Menu Selections for ControlMenu
private JMenuItem ControlSource;
public JMenuItem ControlOff;
public JMenuItem ControlFreezeIn;
public JMenuItem ControlFreezeOut;
public JMenuItem ControlAspect;
private JMenuItem ControlReset;
// Menu Selections for AttributesMenu
private JMenuItem AttributesColor;
private JMenuItem AttributesZoom;
private JMenuItem AttributesZoomValue;
private JMenuItem AttributesSharpness;
private JMenuItem AttributesAlign;
private JMenuItem AttributesSignalListing;
// Menu Selections for MixingMenu
private JMenuItem MixingKeyingMode;
private JMenuItem MixingChroma;
private JMenuItem MixingTransparency;
private JMenuItem MixingAlpha;
// Menu Selections for MixerMenu
private JMenuItem MixerColor;
private JMenuItem MixerBypass;
private JMenuItem MixerGenLock;
private JMenuItem MixerReset;
private JMenuItem MixerAlign;
private JMenuItem MixerSignalListing;
// Menu Selections for InfoMenu
private JMenuItem InfoHardware;
private JMenuItem InfoLabel;
// Menu Selections for Help Menu
private JMenuItem HelpSettings;
private JMenuItem HelpAbout;
// Create objects inside the video gui
public GUICanvas videoCanvas;
private JPanel forButtons;
private JButton FileButton;
private JButton ControlButton;
private JButton AttributesButton;
private JButton InfoButton;
private JButton MixingButton;
private JButton MixerButton;
private JButton HelpButton;
private JMenuBar topPanel;
public videoGui(int ufg, int mix) {
fvnumber = ufg;
mixernumber = mix;
initGUI();
}
static public void main(String[] args) {
videoControl = new videoGui(0, 0);
}
private void initGUI() {
GetInfo();
makeTheObjects();
createMenuBar();
doTheLayout();
// Set up ActionListeners
this.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent ke) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_ESCAPE:
// Do Unzoom.
break;
case KeyEvent.VK_B:
case KeyEvent.VK_H:
if (ke.isControlDown()) {
}
break;
case KeyEvent.VK_Z:
if (ke.isControlDown()) {
}
break;
case KeyEvent.VK_S:
if (ke.isControlDown()) {
}
break;
case KeyEvent.VK_C:
if (ke.isControlDown()) {
}
break;
case KeyEvent.VK_A:
if (ke.isControlDown()) {
if (ControlAspect.getText().equals("Aspect Off")) {
ControlAspect.setText("Aspect On");
}
else {
ControlAspect.setText("Aspect Off");
int inWidth = oldWidth - diffWidth;
int inHeight = (int) ( ( (float) inWidth) / ratio);
oldHeight = inHeight + diffHeight;
setSize(oldWidth, oldHeight);
setPortPosition();
}
}
}
}
public void keyReleased(KeyEvent ke) {
}
public void keyTyped(KeyEvent ke) {
}
});
this.setFocusable(true);
this.addComponentListener(new ComponentAdapter() {
public void componentHidden(ComponentEvent evt) {
}
public void componentMoved(ComponentEvent evt) {
setPortPosition();
}
public void componentResized(ComponentEvent evt) {
resizeVideo();
setPortPosition();
}
public void componentShown(ComponentEvent evt) {
checkIfOn();
resizeVideo();
setPortPosition();
}
});
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
saveHistory();
hideVideoJPanel();
closeout();
}
public void windowClosed(WindowEvent evt) {
hideVideoJPanel();
closeout();
}
public void windowDeiconified(WindowEvent evt) {
checkIfOn();
setPortPosition();
}
public void windowIconified(WindowEvent evt) {
}
public void windowActivated(WindowEvent evt) {
setPortPosition();
}
public void windowDeactivated(WindowEvent evt) {
}
public void windowOpened(WindowEvent evt) {
checkIfOn();
setPortPosition();
}
});
MouseListener FileListener = new FilePopupListener();
FileButton.addMouseListener(FileListener);
MouseListener ControlListener = new ControlPopupListener();
ControlButton.addMouseListener(ControlListener);
MouseListener AttributesListener = new AttributesPopupListener();
AttributesButton.addMouseListener(AttributesListener);
MouseListener InfoListener = new InfoPopupListener();
InfoButton.addMouseListener(InfoListener);
MouseListener MixingListener = new MixingPopupListener();
MixingButton.addMouseListener(MixingListener);
MouseListener MixerListener = new MixerPopupListener();
MixerButton.addMouseListener(MixerListener);
MouseListener HelpListener = new HelpPopupListener();
HelpButton.addMouseListener(HelpListener);
}
class FilePopupListener
extends MouseAdapter {
public void mouseReleased(MouseEvent evt) {
if ( (evt.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
showPopup(evt);
}
}
private void showPopup(MouseEvent evt) {
FileMenu.show(evt.getComponent(), evt.getX(), evt.getY());
}
}
class ControlPopupListener
extends MouseAdapter {
public void mouseReleased(MouseEvent evt) {
if ( (evt.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
showPopup(evt);
}
}
private void showPopup(MouseEvent evt) {
//ControlMenu.show(evt.getComponent(), evt.getX(), evt.getY());
ControlMenu.show(evt.getComponent(), evt.getX(), evt.getY());
}
}
class AttributesPopupListener
extends MouseAdapter {
public void mouseReleased(MouseEvent evt) {
if ( (evt.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
showPopup(evt);
}
}
private void showPopup(MouseEvent evt) {
//AttributesMenu.show(evt.getComponent(), evt.getX(),
evt.getY());
AttributesMenu.show(evt.getComponent(), evt.getX(), evt.getY());
}
}
class InfoPopupListener
extends MouseAdapter {
public void mouseReleased(MouseEvent evt) {
if ( (evt.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
showPopup(evt);
}
}
private void showPopup(MouseEvent evt) {
//InfoMenu.show(evt.getComponent(), evt.getX(), evt.getY());
InfoMenu.show(evt.getComponent(), evt.getX(), evt.getY());
}
}
class MixingPopupListener
extends MouseAdapter {
public void mouseReleased(MouseEvent evt) {
if ( (evt.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
showPopup(evt);
}
}
private void showPopup(MouseEvent evt) {
//MixingMenu.show(evt.getComponent(), evt.getX(), evt.getY());
MixingMenu.show(evt.getComponent(), evt.getX(), evt.getY());
}
}
class MixerPopupListener
extends MouseAdapter {
public void mouseReleased(MouseEvent evt) {
if ( (evt.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
showPopup(evt);
}
}
private void showPopup(MouseEvent evt) {
//MixingMenu.show(evt.getComponent(), evt.getX(), evt.getY());
MixerMenu.show(evt.getComponent(), evt.getX(), evt.getY());
}
}
class HelpPopupListener
extends MouseAdapter {
public void mouseReleased(MouseEvent evt) {
if ( (evt.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
showPopup(evt);
}
}
private void showPopup(MouseEvent evt) {
//HelpMenu.show(evt.getComponent(), evt.getX(), evt.getY());
HelpMenu.show(evt.getComponent(), evt.getX(), evt.getY());
}
}
class FileButtonListener
implements ActionListener {
public void actionPerformed(ActionEvent evt) {
FileMenu.show(FileButton, FileButton.getX(), FileButton.getY());
}
}
class ControlButtonListener
implements ActionListener {
public void actionPerformed(ActionEvent evt) {
ControlMenu.show(ControlButton, ControlButton.getX(),
ControlButton.getY());
}
}
class AttributesButtonListener
implements ActionListener {
public void actionPerformed(ActionEvent evt) {
AttributesMenu.show(AttributesButton, AttributesButton.getX(),
AttributesButton.getY());
}
}
class MixingButtonListener
implements ActionListener {
public void actionPerformed(ActionEvent evt) {
MixingMenu.show(MixingButton, MixingButton.getX(),
MixingButton.getY());
}
}
class MixerButtonListener
implements ActionListener {
public void actionPerformed(ActionEvent evt) {
MixerMenu.show(MixerButton, MixerButton.getX(),
MixerButton.getY());
}
}
class InfoButtonListener
implements ActionListener {
public void actionPerformed(ActionEvent evt) {
InfoMenu.show(InfoButton, InfoButton.getX(), InfoButton.getY());
}
}
class HelpButtonListener
implements ActionListener {
public void actionPerformed(ActionEvent evt) {
HelpMenu.show(HelpButton, HelpButton.getX(), HelpButton.getY());
}
}
public void lowerFrame() {
this.toBack();
}
public void makeTheObjects() {
/* Create the GUI's here */
}
public JMenuBar createMenuBar() {
/* START SWING POPUP MENU */
// Initialize top panel
topPanel = new JMenuBar();
FileButton = new JButton("File");
FileButton.setActionCommand("FileButton");
FileButton.addActionListener(new FileButtonListener());
ControlButton = new JButton("File");
ControlButton.setActionCommand("ControlButton");
ControlButton.addActionListener(new ControlButtonListener());
AttributesButton = new JButton("File");
AttributesButton.setActionCommand("AttributesButton");
AttributesButton.addActionListener(new AttributesButtonListener());
InfoButton = new JButton("File");
InfoButton.setActionCommand("InfoButton");
InfoButton.addActionListener(new InfoButtonListener());
MixingButton = new JButton("File");
MixingButton.setActionCommand("MixingButton");
MixingButton.addActionListener(new MixingButtonListener());
MixerButton = new JButton("File");
MixerButton.setActionCommand("MixerButton");
MixerButton.addActionListener(new MixerButtonListener());
HelpButton = new JButton("File");
HelpButton.setActionCommand("HelpButton");
HelpButton.addActionListener(new HelpButtonListener());
forButtons = new JPanel();
forButtons.setLayout(new GridLayout(1, 7, 0, 0));
forButtons.add(FileButton);
forButtons.add(ControlButton);
forButtons.add(AttributesButton);
forButtons.add(MixingButton);
forButtons.add(MixerButton);
forButtons.add(InfoButton);
forButtons.add(HelpButton);
// Initialize Popup Menus
FileMenu = new JPopupMenu("File");
ControlMenu = new JPopupMenu("Control");
AttributesMenu = new JPopupMenu("Attributes");
InfoMenu = new JPopupMenu("Information");
MixingMenu = new JPopupMenu("Mixing");
MixerMenu = new JPopupMenu("Mixer");
HelpMenu = new JPopupMenu("Help");
// Create Menu Items
// File Menu
FileCapture = new JMenuItem("File");
FileCapture.addActionListener(this);
FileCapture.setActionCommand("Capture");
FileMenu.add(FileCapture);
FilePlayback = new JMenuItem("File");
FilePlayback.addActionListener(this);
FilePlayback.setActionCommand("Playback");
FileMenu.add(FilePlayback);
FileFullScreenCapture = new JMenuItem("File");
FileFullScreenCapture.addActionListener(this);
FileFullScreenCapture.setActionCommand("FullScreenCapture");
FileMenu.add(FileFullScreenCapture);
FileHistory = new JMenuItem("File");
FileHistory.addActionListener(this);
FileHistory.setActionCommand("History");
FileMenu.add(FileHistory);
Separator = new JSeparator();
FileMenu.add(Separator);
FileHide = new JMenuItem("File");
FileHide.addActionListener(this);
FileHide.setActionCommand("Hide");
FileMenu.add(FileHide);
FileClose = new JMenuItem("File");
FileClose.addActionListener(this);
FileClose.setActionCommand("Close");
FileMenu.add(FileClose);
forButtons.add(FileMenu);
// Control Menu
ControlSource = new JMenuItem("File");
ControlSource.addActionListener(this);
ControlSource.setActionCommand("Source");
ControlMenu.add(ControlSource);
ControlOff = new JMenuItem("File");
ControlOff.addActionListener(this);
ControlOff.setActionCommand("Off");
ControlMenu.add(ControlOff);
ControlFreezeIn = new JMenuItem("File");
ControlFreezeIn.addActionListener(this);
ControlFreezeIn.setActionCommand("FreezeIn");
ControlMenu.add(ControlFreezeIn);
ControlFreezeOut = new JMenuItem("File");
ControlFreezeOut.addActionListener(this);
ControlFreezeOut.setActionCommand("FreezeOut");
ControlMenu.add(ControlFreezeOut);
ControlAspect = new JMenuItem("File");
ControlAspect.addActionListener(this);
ControlAspect.setActionCommand("Aspect");
ControlMenu.add(ControlAspect);
ControlReset = new JMenuItem("File");
ControlReset.addActionListener(this);
ControlReset.setActionCommand("Reset");
ControlMenu.add(ControlReset);
forButtons.add(ControlMenu);
// Attributes Menu
AttributesColor = new JMenuItem("File");
AttributesColor.addActionListener(this);
AttributesColor.setActionCommand("Color");
AttributesMenu.add(AttributesColor);
AttributesZoom = new JMenuItem("File");
AttributesZoom.addActionListener(this);
AttributesZoom.setActionCommand("Zoom");
AttributesMenu.add(AttributesZoom);
AttributesZoomValue = new JMenuItem("File");
AttributesZoomValue.addActionListener(this);
AttributesZoomValue.setActionCommand("ZoomValue");
AttributesMenu.add(AttributesZoomValue);
AttributesSharpness = new JMenuItem("File");
AttributesSharpness.addActionListener(this);
AttributesSharpness.setActionCommand("Sharpness");
AttributesMenu.add(AttributesSharpness);
AttributesAlign = new JMenuItem("File");
AttributesAlign.addActionListener(this);
AttributesAlign.setActionCommand("Align");
AttributesMenu.add(AttributesAlign);
AttributesSignalListing = new JMenuItem("File");
AttributesSignalListing.addActionListener(this);
AttributesSignalListing.setActionCommand("SignalListing");
AttributesMenu.add(AttributesSignalListing);
forButtons.add(AttributesMenu);
//Information Menu
InfoHardware = new JMenuItem("File");
InfoHardware.addActionListener(this);
InfoHardware.setActionCommand("Hardware");
InfoMenu.add(InfoHardware);
InfoLabel = new JMenuItem("File");
InfoLabel.addActionListener(this);
InfoLabel.setActionCommand("Label");
InfoMenu.add(InfoLabel);
forButtons.add(InfoMenu);
// Mixing Menu
MixingKeyingMode = new JMenuItem("File");
MixingKeyingMode.addActionListener(this);
MixingKeyingMode.setActionCommand("KeyingMode");
MixingMenu.add(MixingKeyingMode);
MixingChroma = new JMenuItem("File");
MixingChroma.addActionListener(this);
MixingChroma.setActionCommand("Chroma");
MixingMenu.add(MixingChroma);
MixingTransparency = new JMenuItem("File");
MixingTransparency.addActionListener(this);
MixingTransparency.setActionCommand("Transparency");
MixingMenu.add(MixingTransparency);
MixingAlpha = new JMenuItem("File");
MixingAlpha.addActionListener(this);
MixingAlpha.setActionCommand("Alpha");
MixingMenu.add(MixingAlpha);
forButtons.add(MixingMenu);
// Mixer Menu
MixerColor = new JMenuItem("File");
MixerColor.addActionListener(this);
MixerColor.setActionCommand("MixerColor");
MixerMenu.add(MixerColor);
MixerBypass = new JMenuItem("File");
MixerBypass.addActionListener(this);
MixerBypass.setActionCommand("MixerBypass");
MixerMenu.add(MixerBypass);
MixerGenLock = new JMenuItem("File");
MixerGenLock.addActionListener(this);
MixerGenLock.setActionCommand("GenLock");
MixerMenu.add(MixerGenLock);
MixerReset = new JMenuItem("File");
MixerReset.addActionListener(this);
MixerReset.setActionCommand("MixerReset");
MixerMenu.add(MixerReset);
MixerAlign = new JMenuItem("File");
MixerAlign.addActionListener(this);
MixerAlign.setActionCommand("MixerAlign");
MixerMenu.add(MixerAlign);
MixerSignalListing = new JMenuItem("File");
MixerSignalListing.addActionListener(this);
MixerSignalListing.setActionCommand("MixerSignalListing");
MixerMenu.add(MixerSignalListing);
// Help Menu
HelpSettings = new JMenuItem("File");
HelpSettings.addActionListener(this);
HelpSettings.setActionCommand("Settings");
HelpMenu.add(HelpSettings);
HelpAbout = new JMenuItem("File");
HelpAbout.addActionListener(this);
HelpAbout.setActionCommand("About");
HelpMenu.add(HelpAbout);
forButtons.add(HelpMenu);
// Add in Menus to the top panel.
topPanel.add(FileMenu);
topPanel.add(ControlMenu);
topPanel.add(AttributesMenu);
topPanel.add(InfoMenu);
topPanel.add(MixingMenu);
topPanel.add(MixerMenu);
topPanel.add(HelpMenu);
return topPanel;
/* END SWING POPUP MENU */
}
private void doTheLayout() {
videoCanvas = new GUICanvas(this);
videoCanvas.setSize(source_width, source_height);
// videoCanvas.setOpaque( false );
videoCanvas.setOpaque(true);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(videoCanvas, "Center");
this.getContentPane().add(forButtons, "South");
this.setSize(source_width, source_height);
// this.getContentPane().setBackground( new java.awt.Color( 0, 0, 0
) );
String popupBehavior = "centerscreen";
popups = popupBehavior;
}
private void SetTopWindow(int val) {
if (ControlOff.getText().equals("Off")) {
// Tell it to set me to the top window.
}
}
public void setPortPosition() {
Point newLoc;
positRectangle = getBounds();
if (ControlOff.getText().equals("Off")) {
if (this.isVisible()) {
newLoc = videoCanvas.getLocationOnScreen();
}
else {
newLoc = new Point(0, 0);
}
int AbsoluteX = newLoc.x - OffSetX;
int AbsoluteY = newLoc.y - OffSetY;
//fvmain.SetLocation( fvnumber, AbsoluteX, AbsoluteY );
}
}
private void resizeVideo() {
int newWidth = this.getWidth();
int newHeight = this.getHeight();
int vidWidth = videoCanvas.getWidth();
int vidHeight = videoCanvas.getHeight();
int inHeight;
int inWidth;
if (first_time) {
oldHeight = newHeight;
oldWidth = newWidth;
diffHeight = oldHeight - vidHeight;
diffWidth = oldWidth - vidWidth;
oldHeight = source_height + diffHeight;
oldWidth = source_width + diffWidth;
newHeight = oldHeight;
newWidth = oldWidth;
first_time = false;
}
if (ControlAspect.getText().equals("Aspect Off")) {
if (newHeight != oldHeight || newWidth != oldWidth) {
// Keep aspect ratio
if (java.lang.Math.abs(oldHeight - newHeight) >=
java.lang.Math.abs(oldWidth - newWidth)) {
oldHeight = newHeight;
inHeight = oldHeight - diffHeight;
inWidth = (int) (ratio * ( (float) inHeight));
oldWidth = inWidth + diffWidth;
}
else {
oldWidth = newWidth;
inWidth = oldWidth - diffWidth;
inHeight = (int) ( ( (float) inWidth) / ratio);
oldHeight = inHeight + diffHeight;
}
}
this.setSize(oldWidth, oldHeight);
}
else {
oldHeight = newHeight;
oldWidth = newWidth;
}
//fvmain.SetSize( fvnumber, this.videoCanvas.getWidth(),
this.videoCanvas.getHeight() );
}
private void closeout() {
System.exit(0);
}
public void SetString() {
switch (fvtype) {
case 0:
fvtypeS = "RGB";
break;
case 1:
fvtypeS = "CVBS";
break;
case 2:
fvtypeS = "Monochrome";
break;
default:
fvtypeS = "Unknown Type";
break;
}
}
public void GetInfo() {
SetString();
source_width = 640;
source_height = 480;
this.setTitle("FlexiVision 3 - Board " + fvnumber + " " + fvtypeS);
ratio = ( (float) source_width) / ( (float) source_height);
}
public void setTitles(String label) {
}
private void hideVideoJPanel() {
this.setVisible(false);
}
private void hideWindows(int test) {
}
private void checkIfOn() {
}
public void loadHistory() {
}
private void saveHistory() {
// Gather various information and save it for later retrieval.
// Get size and position (relative to offset)
}
public void window_popup_location(JDialog window) {
if (popups.equals("centerscreen")) {
window.setLocation( (int) (normalbounds.getWidth() / 2 -
window.getWidth() / 2) + OffSetX,
(int) (normalbounds.getHeight() / 2 -
window.getHeight() / 2) + OffSetY);
}
else if (popups.equals("upperleft")) {
window.setLocation(OffSetX, OffSetY);
}
else if (popups.equals("centerowner")) {
window.setLocation(this.getWidth() / 2 - window.getWidth() / 2 +
this.getX(),
this.getHeight() / 2 - window.getHeight() / 2
+
this.getY());
}
else {
window.setLocation(OffSetX, OffSetY);
}
}
public void window_popup_location(JFrame window) {
if (popups.equals("centerscreen")) {
window.setLocation( (int) (normalbounds.getWidth() / 2 -
window.getWidth() / 2) + OffSetX,
(int) (normalbounds.getHeight() / 2 -
window.getHeight() / 2) + OffSetY);
}
else if (popups.equals("upperleft")) {
window.setLocation(OffSetX, OffSetY);
}
else if (popups.equals("centerowner")) {
window.setLocation(this.getWidth() / 2 - window.getWidth() / 2 +
this.getX(),
this.getHeight() / 2 - window.getHeight() / 2
+
this.getY());
}
else {
window.setLocation(OffSetX, OffSetY);
}
}
public void window_popup_location(Frame window) {
if (popups.equals("centerscreen")) {
window.setLocation( (int) (normalbounds.getWidth() / 2 -
window.getWidth() / 2) + OffSetX,
(int) (normalbounds.getHeight() / 2 -
window.getHeight() / 2) + OffSetY);
}
else if (popups.equals("upperleft")) {
window.setLocation(OffSetX, OffSetY);
}
else if (popups.equals("centerowner")) {
window.setLocation(this.getWidth() / 2 - window.getWidth() / 2 +
this.getX(),
this.getHeight() / 2 - window.getHeight() / 2
+
this.getY());
}
else {
window.setLocation(OffSetX, OffSetY);
}
}
public void actionPerformed(ActionEvent evt) {
}
// First panel which opens Video Panel
public class GUICanvas
extends JComponent
implements MouseListener, MouseMotionListener {
private String msg = "";
public int mouseX = 0;
public int mouseY = 0;
public int prevX = 0;
public int prevY = 0;
private boolean drawRect = false;
private boolean doPan = false;
private boolean unzoom = false;
public boolean rubberBand = true;
public Color canvasColor = new java.awt.Color(0, 0, 0);
private String theColor = "";
private videoGui videowindow;
private int drawX, drawY, drawWidth, drawHeight;
public void GUICanvas(Graphics g) {
this.paintComponent(g);
repaint();
addMouseListener(this);
addMouseMotionListener(this);
}
public GUICanvas(videoGui v) {
videowindow = v;
repaint();
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me) {
// Save Coords
mouseX = 0;
mouseY = 10;
msg = "Mouse clicked.";
drawRect = false;
doPan = false;
if (me.getModifiers() == InputEvent.BUTTON3_MASK) {
unzoom = true;
}
videowindow.requestFocus();
repaint();
}
public void mouseEntered(MouseEvent me) { // Save Coordinates
mouseX = 0;
mouseY = 10;
msg = "Mouse entered.";
if (rubberBand) {
this.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.
CROSSHAIR_CURSOR));
}
repaint();
}
public void mouseExited(MouseEvent me) {
mouseX = 0;
mouseY = 10;
msg = "Mouse Exited.";
this.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.
DEFAULT_CURSOR));
repaint();
}
public void mousePressed(MouseEvent me) {
prevX = me.getX();
prevY = me.getY();
msg = "Down.";
repaint();
}
public void mouseReleased(MouseEvent me) {
mouseX = me.getX();
mouseY = me.getY();
msg = "Up.";
if (drawRect == true) {
System.out.println("Zooming...");
drawRect = false;
}
doPan = false;
unzoom = false;
this.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.
CROSSHAIR_CURSOR));
repaint();
prevX = 0;
prevY = 0;
}
public void mouseDragged(MouseEvent me) {
mouseX = me.getX();
mouseY = me.getY();
msg = "* Dragging mouse at " + mouseX + ", " + mouseY;
if (me.getModifiers() == InputEvent.BUTTON1_MASK) {
drawRect = true;
}
else if (me.getModifiers() == InputEvent.BUTTON2_MASK) {
doPan = true;
this.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.
MOVE_CURSOR));
}
else if (me.getModifiers() == InputEvent.BUTTON3_MASK) {
unzoom = true;
}
repaint();
}
public void mouseMoved(MouseEvent me) {
msg = "Moving mouse at " + me.getX() + ", " + me.getY();
}
public Color randomColor() {
int randomR;
int randomG;
int randomB;
Random generator = new Random();
Color tempColor;
randomR = generator.nextInt(256);
randomG = generator.nextInt(256);
randomB = generator.nextInt(256);
tempColor = new java.awt.Color(randomR, randomG, randomB);
return tempColor;
}
public void setParams(String newColor) {
theColor = newColor;
repaint();
}
public void paintComponent(Graphics g) {
drawX = 0;
drawY = 0;
drawHeight = 0;
drawWidth = 0;
g.setColor(canvasColor);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(randomColor());
if (rubberBand) {
if (drawRect) {
if (mouseY < this.getY()) {
mouseY = 0;
}
if (mouseX < this.getX()) {
mouseX = 0;
}
if (mouseY > this.getHeight()) {
mouseY = this.getHeight() - 1;
}
if (mouseX > this.getWidth()) {
mouseX = this.getWidth() - 1;
}
if (prevX < mouseX) {
drawWidth = mouseX - prevX;
drawX = prevX;
}
else {
drawWidth = prevX - mouseX;
drawX = mouseX;
}
if (prevY < mouseY) {
drawHeight = mouseY - prevY;
drawY = prevY;
}
else {
drawHeight = prevY - mouseY;
drawY = mouseY;
}
if ( (drawX + drawWidth) > this.getWidth()) {
drawWidth = this.getWidth() - drawX - 1;
}
if ( (drawY + drawHeight) > this.getWidth()) {
drawHeight = this.getHeight() - drawY - 1;
}
g.drawRect(drawX, drawY, drawWidth, drawHeight);
}
else if (doPan) {
// Do pan code.
System.out.println("Panning...");
}
else if (unzoom) {
// Zoom to 0.
unzoom = false;
}
}
}
}
}