hello , im trying to get a circle on my canvas to move( animate) , when i press go. I thought i had it down , but the circle wont move lol. im thinking maybe it has to do with the timer , maybe i didnt set it up right
heres my shape class , which is the shape im trying to animate.
heres my canvas class , which does the painting
and heres the GUI app , were my buttons supposed to work , called GO
heres my shape class , which is the shape im trying to animate.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class Shape implements ConstantTable {
private Color R ;
private int width;
private int height;
private int xPos = 0, yPos = 10;
public void increaseSize(){
width = width+DELTA;
height = height+DELTA;
}
public void decreaseSize (){
width = width-DELTA;
height= height-DELTA;
}
Shape(int w, int h) {
width = w;
height = h;
}
public void animate( ){
if (xPos < 0 || xPos > 380)
yPos = -yPos;
xPos = xPos + yPos;
}
void draw(Graphics g) {
g.setColor(R);
g.fillOval(xPos+10,yPos+10, width-100, height-100);
}
public void setColor(Color value) {
R = value;
}
}
heres my canvas class , which does the painting
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MyCanvas extends JPanel {
private Timer timer;
private Shape face ;
private Animate circle;
private static final long serialVersionUID = -5625058787715414029L;
private static final Color Color = null;
private int width;
private int height;
private int x;
private int y;
MyCanvas(int width, int height) {
super();
this.width = width;
this.height = height;
setSize(width, height);
this.setBackground(Color.WHITE);
face = new Shape(width, height);
circle= new Animate (x,y);
timer = new Timer(5, new TimerListener());
timer.start();
}
public MyCanvas() {
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
face.draw(g);
timer.start();
circle.draw(g);
}
public void increaseSize() {
face.increaseSize();
}
public void decreaseSize() {
face.decreaseSize();
}
public void setColor(Color value) {
face.setColor(value);
}
public void animate(){
circle.animate();
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
repaint();
}
}
}
and heres the GUI app , were my buttons supposed to work , called GO
package edu.calstatela.cs202.NAVA.hw3;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Gui extends JFrame {
static MyCanvas canvas = new MyCanvas();
public static void main(String[] args) {
JFrame frame = new JFrame("My GUI");
JMenuBar menuBar = new JMenuBar();
// panels
final JPanel rightPanel = new JPanel();
JPanel leftPanel = new JPanel();
// menu
frame.setJMenuBar(menuBar);
JMenu file = new JMenu("File");
menuBar.add(file);
JMenuItem load = new JMenuItem("Load");
JMenuItem save = new JMenuItem("Save");
JMenuItem exit = new JMenuItem("Exit");
file.add(load);
file.add(save);
file.add(exit);
JMenu edit = new JMenu("Menu");
menuBar.add(edit);
JMenuItem cut = new JMenuItem("Cut");
JMenuItem copy = new JMenuItem("Copy");
JMenuItem paste = new JMenuItem("Paste");
edit.add(cut);
edit.add(copy);
edit.add(paste);
JMenu about = new JMenu("About");
menuBar.add(about);
JMenuItem aboutApp = new JMenuItem("About This Application");
JMenuItem help = new JMenuItem("Help");
about.add(aboutApp);
about.add(help);
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
help.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane
.showMessageDialog(
null,
"Change Shape Color: changes the shape to a random color"
+ ", Bigger and Smaller: change the size of the shape,"
+ " Background Color: changes the background of the canvas");
}
});
aboutApp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"This application was made by Michael Nava");
}
});
GridLayout applicationLayout = new GridLayout(1, 2);
frame.setLayout(applicationLayout);
GridLayout canvasLayout = new GridLayout(1, 1);
leftPanel.setLayout(canvasLayout);
final MyCanvas canvas = new MyCanvas(300, 300);
leftPanel.add(canvas);
GridLayout buttonLayout = new GridLayout(5, 5);
rightPanel.setLayout(buttonLayout);
JButton button5 = new JButton("Change Shape Color");
rightPanel.add(button5);
button5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Color value = randomColor();
canvas.setColor(value);
canvas.repaint();
}
});
JButton button3 = new JButton("Bigger");
rightPanel.add(button3);
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String actionStr = e.getActionCommand();
System.out.println("button action: " + actionStr);
if (actionStr.equals("Bigger")) {
canvas.increaseSize();
canvas.repaint();
} else {
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
);
JButton button4 = new JButton("Smaller");
rightPanel.add(button4);
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String actionStr = e.getActionCommand();
System.out.println("button action: " + actionStr);
if (actionStr.equals("Smaller")) {
canvas.decreaseSize();
canvas.repaint();
} else {
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
);
final JSlider colorSlider = new JSlider(0, 250);
JLabel colorLabel = new JLabel("Background Color");
rightPanel.add(colorLabel);
rightPanel.add(colorSlider);
colorSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
int r = colorSlider.getValue();
// int g = colorSlider.getValue();
// int b = colorSlider.getValue();
canvas.setBackground(new Color(r, 150, 200));
}
}
);
JButton go = new JButton ("Go");
rightPanel.add(go);
go.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
canvas.animate();
canvas.repaint();
}
}
);
frame.add(leftPanel);
frame.add(rightPanel);
frame.setSize(800, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private static Color randomColor() {
int r = (int) (Math.random() * 256);
int g = (int) (Math.random() * 256);
int b = (int) (Math.random() * 256);
return (new Color(r, g, B)/>);
}
}