Skip to content

Instantly share code, notes, and snippets.

Created August 18, 2014 23:09
Show Gist options
  • Save anonymous/42800dd8ab2380486e38 to your computer and use it in GitHub Desktop.
Save anonymous/42800dd8ab2380486e38 to your computer and use it in GitHub Desktop.
Moving Shapes
package com.angeliquenguyen_le.a6drawshapes;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class DrawShapes extends JFrame {
// Drawing area constants
public static final int CANVAS_WIDTH = 500;
public static final int CANVAS_HEIGHT = 300;
public static final Color CANVAS_BACKGROUND = Color.YELLOW;
// Set line coordinates (x1, y1 to x2, y2) starting at center
private int x1 = CANVAS_WIDTH / 4;
private int y1 = CANVAS_HEIGHT / 4;
private int rectWidth = CANVAS_WIDTH / 2;
private int rectHeight = CANVAS_HEIGHT / 2;
private String[] shapeName = {"Rectangle", "Circle", "Triangle"};
private CanvasDrawArea canvas; // the custom drawing canvas (extends JPanel)
/** Constructor to set up the GUI */
public DrawShapes() {
// Set up a panel for the buttons
JPanel btnPanel = new JPanel(new FlowLayout());
JComboBox shapes = new JComboBox(shapeName);
btnPanel.add(shapes);
JButton btnLeft = new JButton("Move Left ");
btnPanel.add(btnLeft);
btnLeft.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
x1 -= 10;
canvas.repaint();
requestFocus(); // change the focus to JFrame to receive
// KeyEvent
}
});
JButton btnRight = new JButton("Move Right");
btnPanel.add(btnRight);
btnRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
x1 += 10;
canvas.repaint();
requestFocus(); // change the focus to JFrame to receive
// KeyEvent
}
});
// Set up a custom drawing JPanel
canvas = new CanvasDrawArea();
canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
// Add both panels to this JFrame
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(canvas, BorderLayout.CENTER);
cp.add(btnPanel, BorderLayout.SOUTH);
// "this" JFrame fires KeyEvent
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_LEFT:
x1 -= 10;
//x2 -= 10;
repaint();
break;
case KeyEvent.VK_RIGHT:
x1 += 10;
//x2 += 10;
repaint();
break;
}//end switch
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Handle the CLOSE
// button
setTitle("Drawing Shapes: Moving Shapes");
pack(); // pack all the components in the JFrame
setVisible(true); // show it
requestFocus(); // set the focus to JFrame to receive KeyEvent
}//draw shapes constructor?
/**
* CanvasDrawArea (inner class) is a JPanel used for custom drawing
*/
class CanvasDrawArea extends JPanel {
public void paintComponent(Graphics rect) {
super.paintComponent(rect);
setBackground(CANVAS_BACKGROUND);
rect.setColor(Color.BLUE);
rect.fillRect(x1, y1, rectWidth, rectHeight);
}//paintComponent method end
}//CanvasDrawArea class end
}//DrawShapes class end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment