public void launchFrame() { add(panCircle, BorderLayout.CENTER); // add Circle panel add(panButtons, BorderLayout.SOUTH); // add Buttons panel panCircle.setLayout(new BorderLayout()); panCircle.addMouseListener( new MouseListener() { @Override public void mouseClicked(MouseEvent e) { // считываем клик мышки и перерисовываем X = e.getX(); Y = e.getY(); // R = Integer.parseInt(textFieldR.getText()); ovalnew[num][0] = e.getX(); ovalnew[num][1] = e.getY(); ovalnew[num][2] = Integer.parseInt(textFieldR.getText()); num++; if (num == 6) { num = 0; } panCircle.repaint(); panCircle.repaint(); } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } }); panButtons.setSize(40, 300); // buttons panel size panButtons.setLayout(new GridLayout(1, 4)); // 1x4 cell panButtons.add(textFieldX); // add textFieldX panButtons.add(textFieldY); // add textFieldY panButtons.add(textFieldR); // add textFieldR panButtons.add(okButton); // add Ok Button okButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { X = Integer.parseInt(textFieldX.getText()); Y = Integer.parseInt(textFieldY.getText()); R = Integer.parseInt(textFieldR.getText()); ovalnew[num][0] = Integer.parseInt(textFieldX.getText()); ovalnew[num][1] = Integer.parseInt(textFieldY.getText()); ovalnew[num][2] = Integer.parseInt(textFieldR.getText()); num++; if (num == 6) { num = 0; } panCircle.repaint(); // тут заполнять массив } }); setVisible(true); setSize(300, 300); // frame size setTitle("Half-Random Circle"); // title addWindowListener(this); }
public void init() { try { robot = new Robot(); } catch (AWTException ex) { ex.printStackTrace(); throw new RuntimeException(ex); } this.setLayout(new BorderLayout()); target.setBackground(Color.green); target.setName("GreenBox"); // for the ease of debug target.setPreferredSize(new Dimension(100, 100)); String toolkit = Toolkit.getDefaultToolkit().getClass().getName(); // on X systems two buttons are reserved for wheel though they are countable by MouseInfo. int buttonsNumber = toolkit.equals("sun.awt.windows.WToolkit") ? MouseInfo.getNumberOfButtons() : MouseInfo.getNumberOfButtons() - 2; for (int i = 0; i < 8; i++) { buttonNumber.add("BUTTON" + (i + 1) + "_MASK"); } pressOn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Now pressing : " + (buttonNumber.getSelectedIndex() + 1)); Timer timer = new Timer(); TimerTask robotInteraction = new TimerTask() { public void run() { robot.mouseMove(updateTargetLocation().x, updateTargetLocation().y); robot.mousePress(getMask(buttonNumber.getSelectedIndex() + 1)); } }; timer.schedule(robotInteraction, SEND_DELAY); } }); releaseOn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Now releasing : " + (buttonNumber.getSelectedIndex() + 1)); Timer timer = new Timer(); TimerTask robotInteraction = new TimerTask() { public void run() { robot.mouseMove(updateTargetLocation().x, updateTargetLocation().y); robot.mouseRelease(getMask(buttonNumber.getSelectedIndex() + 1)); } }; timer.schedule(robotInteraction, SEND_DELAY); } }); clickOn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Now clicking : " + (buttonNumber.getSelectedIndex() + 1)); Timer timer = new Timer(); TimerTask robotInteraction = new TimerTask() { public void run() { robot.mouseMove(updateTargetLocation().x, updateTargetLocation().y); robot.mousePress(getMask(buttonNumber.getSelectedIndex() + 1)); robot.mouseRelease(getMask(buttonNumber.getSelectedIndex() + 1)); } }; timer.schedule(robotInteraction, SEND_DELAY); } }); target.addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent e) { Sysout.println("" + e); } public void mouseReleased(MouseEvent e) { Sysout.println("" + e); } public void mouseClicked(MouseEvent e) { Sysout.println("" + e); } }); String[] instructions = { "Do provide an instruction to the robot by", "choosing the button number to act and ", "pressing appropriate java.awt.Button on the left.", "Inspect an output in the TextArea below.", "Please don't generate non-natural sequences like Release-Release, etc.", "If you use keyboard be sure that you released the keyboard shortly.", "If events are generated well press Pass, otherwise Fail." }; Sysout.createDialogWithInstructions(instructions); } // End init()
public MovingBalls() { f = new Frame(); b = new Button("ok"); b1 = new Button("go"); p = new Panel() { public void paint(Graphics g) { Color c1 = new Color( (int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)); g.setColor(c1); g.fillOval(bx[0], by[0], 2 * br[0], 2 * br[0]); Color c2 = new Color( (int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)); g.setColor(c2); g.fillOval(bx[1], by[1], 2 * br[1], 2 * br[1]); Color c3 = new Color( (int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)); g.setColor(c3); g.fillOval(bx[2], by[2], 2 * br[2], 2 * br[2]); Color c4 = new Color( (int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)); g.setColor(c4); g.fillOval(bx[3], by[3], 2 * br[3], 2 * br[3]); Color c5 = new Color( (int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)); g.setColor(c5); g.fillOval(bx[4], by[4], 2 * br[4], 2 * br[4]); Color c6 = new Color( (int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)); g.setColor(c6); g.fillOval(bx[5], by[5], 2 * br[5], 2 * br[5]); } }; p1 = new Panel(); f.add(p1, BorderLayout.SOUTH); f.add(p, BorderLayout.CENTER); p1.add(b); p1.add(b1); b.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (i < 6) { x = 100; y = 100; r = (int) (Math.random() * 50); bx[i] = x + (int) (Math.random() * 50); by[i] = y + (int) (Math.random() * 50); br[i] = r; i++; p.repaint(); } else if (i == 6) { i = 0; } } }); b1.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { while (true) { for (int l = 0; l < 6; l++) { bx[l] = bx[l] + dx_m[l]; by[l] = by[l] + dy_m[l]; if (by[l] > p.getHeight()) { dy_m[l] = -dy_m[l]; } if (bx[l] > p.getWidth()) { dx_m[l] = -dx_m[l]; } if (by[l] < 0) { dy_m[l] = -dy_m[l]; } if (bx[l] < 0) { dx_m[l] = -dx_m[l]; } } /* bx[0] = bx[0] + dx; by[0] = by[0] + dy; if(by[0]>p.getHeight() ) { dy=-dy; } if(bx[0]>p.getWidth()) { dx=-dx; } if(by[0]<0 ) { dy=-dy; } if(bx[0]<0) { dx=-dx; } */ /* bx[1] = bx[1] + dx1; by[1] = by[1] - dy1; if(by[1]>p.getHeight() ) { dy1=-dy1; } if(bx[1]>p.getWidth()) { dx1=-dx1; } if(by[1]<0 ) { dy1=-dy1; } if(bx[1]<0) { dx1=-dx1; } bx[2] = bx[2] - dx2; by[2] = by[2] + dy2; if(by[2]>p.getHeight() ) { dy2=-dy2; } if(bx[2]>p.getWidth()) { dx2=-dx2; } if(by[2]<0 ) { dy2=-dy2; } if(bx[2]<0) { dx2=-dx2; } bx[3] = bx[3] - dx3; by[3] = by[3] - dy3; if(by[3]>p.getHeight() ) { dy3=-dy3; } if(bx[3]>p.getWidth()) { dx3=-dx3; } if(by[3]<0 ) { dy3=-dy3; } if(bx[3]<0) { dx3=-dx3; } bx[4] = bx[4]; by[4] = by[4] - dy4; if(by[4]>p.getHeight() ) { dy4=-dy4; } if(bx[4]>p.getWidth()) { dx4=-dx4; } if(by[4]<0 ) { dy4=-dy4; } if(bx[4]<0) { dx4=-dx4; } bx[5] = bx[5] - dx5; by[5] = by[5] ; if(by[5]>p.getHeight() ) { dy5=-dy5; } if(bx[5]>p.getWidth()) { dx5=-dx5; } if(by[5]<0 ) { dy5=-dy5; } if(bx[5]<0) { dx5=-dx5; } */ for (int i = 0; i < 10000000; i++) {} p.update(p.getGraphics()); } } }); f.addWindowListener(this); f.addMouseMotionListener(this); p.addMouseListener(this); f.setSize(600, 600); f.setVisible(true); }