import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JComponent;

paltryboarpigΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 4 χρόνια και 4 μέρες)

88 εμφανίσεις

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JComponent;

import
javax.swing.JFrame;


/**


*


* @author zhao_m


*/

public class RaceTrack extends JComponent implements ActionListener {


private Rectangle raceCar;


private int xFactor = 1, yFactor = 0;


private int move = 10;




public RaceTrack() {



this.setPreferredSize(new Dimension(200, 140));


raceCar = new Rectangle(85, 110, 16, 10);


}



public void paintComponent(Graphics g) {


Graphics2D g2d = (Graphics2D)g;


g2d.setColor(Color.GREEN);


g2d.drawRoundRect(20,

10, 160, 120, 100, 100);


g2d.drawRoundRect(50, 40, 100, 60, 50, 50);


g2d.setColor(Color.RED);


g2d.drawLine(100, 100, 100, 130);




g2d.setColor(Color.BLUE);


g2d.fill(raceCar);


}



public void
actionPerformed(ActionEvent e) {


String sourceButton = e.getActionCommand();


if (sourceButton.equals("Move Forward"))


raceCar.translate(xFactor * move, yFactor * move);


else if (sourceButton.equals("Move Backward"))



raceCar.translate(
-
xFactor * move,
-
yFactor * move);


else if (sourceButton.equals("Turn Left")) {


if (xFactor != 0)


raceCar.grow(
-
3, 3);


else


raceCar.grow(3,
-
3);


int xFa
ctorNew = yFactor;


int yFactorNew =
-
xFactor;


xFactor = xFactorNew;


yFactor = yFactorNew;


}


repaint();


}



public static void main(String[] args) {


JFrame frame = new JFrame("Race Track");


frame.add(new RaceTrack());


frame.pack();


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


frame.setVisible(true);


}

}

import java.awt.Container;

import java.awt.Dimension;

import javax.swing.JButton;

import
javax.swing.JFrame;

import javax.swing.JPanel;


/**


*


* @author zhao_m


*/

public class RaceCarApp {


private RaceTrack track;


private JPanel controlPanel;



public RaceCarApp() {


init();


}



private void init() {


control
Panel = new JPanel();


JButton jbtMoveForward = new JButton("Move Forward");


JButton jbtMoveBackward = new JButton("Move Backward");


JButton jbtTurnLeft = new JButton("Turn Left");


JButton jbtTurnRight = new JButton("Turn
Right");



controlPanel.setPreferredSize(


new Dimension(jbtMoveBackward.getPreferredSize().width + 5,


jbtMoveBackward.getPreferredSize().height * 4 + 10));



controlPanel.add(jbtMoveForward);


controlPan
el.add(jbtMoveBackward);


controlPanel.add(jbtTurnLeft);


controlPanel.add(jbtTurnRight);



track = new RaceTrack();



jbtMoveForward.addActionListener(track);


jbtMoveBackward.addActionListener(track);


jbtTurnLef
t.addActionListener(track);



JFrame frame = new JFrame("Race Car Test");


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


Container c = frame.getContentPane();


c.add(controlPanel, "South");


c.add(track, "Center"
);



frame.pack();


frame.setVisible(true);


}



public static void main(String[] args) {


new RaceCarApp();


}

}