import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.border.MatteBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Week06 {
private JFrame myFrame;
private final GPanel gP = new GPanel();
private final JSlider vSlider =
new JSlider(SwingConstants.VERTICAL, 0, 100, 0);
private final JSlider hSlider =
new JSlider(SwingConstants.HORIZONTAL, 0, 100, 0);
private final JButton btnExit =
new JButton("Exit");
private int x_pos = 10;
private int y_pos = 100;
private int radius = 10;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Week06 window = new Week06();
window.myFrame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Week06() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
myFrame = new JFrame();
myFrame.setTitle("Week06");
myFrame.setBounds(100, 100, 780, 495);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.getContentPane().setLayout(null);
//JPanel pnlBallMover = new JPanel();
gP.setBackground(new Color(0, 206, 209));
gP.setBorder(new MatteBorder(5, 5, 5, 5,
(Color) new Color(222, 184, 135)));
gP.setBounds(10, 11, 714, 374);
myFrame.getContentPane().add(gP);
//JButton btnExit = new JButton("Exit");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
btnExit.setFont(new Font("Lucida Calligraphy", Font.BOLD, 16));
btnExit.setBounds(644, 413, 110, 33);
myFrame.getContentPane().add(btnExit);
hSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
gP.repaint();
}
});
hSlider.setBounds(0, 0, 714, 23);
hSlider.setMajorTickSpacing(5);
hSlider.setPaintTicks(true);
myFrame.getContentPane().add(hSlider);
vSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
gP.repaint();
}
});
vSlider.setOrientation(SwingConstants.VERTICAL);
vSlider.setBounds(x_pos, y_pos, 30, 374);
vSlider.setMajorTickSpacing(5);
vSlider.setPaintTicks(true);
vSlider.setInverted(true);
myFrame.getContentPane().add(vSlider);
}
private class GPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.ORANGE);
g2.fillOval(x_pos, y_pos, radius*2, radius*2);
gP.setD(radius*2);
int fw = myFrame.getWidth();
int fh = myFrame.getHeight();
if((fw - 66 > 10) && (fh - 121 > 10)) {
btnExit.setBounds(fw - 136, fh - 82, 110, 33);
hSlider.setBounds(10, fh - 110, fw - 66, 23);
vSlider.setBounds(fw - 56, 11, 30, fh - 121);
setBounds(10, 11, fw - 66, fh - 121);
}
}
public void setD(int newD) {
if(newD >= 0) {
radius = newD;
} else {
radius = 10;
}
}
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
}
}