Question on Graphics/JFrame/KeyEvents
I want the user to be able to control the oval on the screen with the arrow keys. Here is my code.
//main
Code:
package Game;
public class main {
public static void main(String[] args){
new Starter();
}
}
//initiator
Code:
package Game;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Starter extends JFrame implements ActionListener {
Starter(){
super("Game Selector");
JButton Close = new JButton("Close");
JButton maze = new JButton("Maze");
JButton idk = new JButton("idk");
JLabel tell = new JLabel(" Please Select A Game");
GridLayout Lay = new GridLayout(4,2);
setLayout(Lay);
add(tell);
add(maze);
add(idk);
add(Close);
this.setSize(400, 400);
setVisible(true);
Close.setActionCommand("Close");
Close.addActionListener(this);
maze.setActionCommand("Maze");
maze.addActionListener(this);
idk.setActionCommand("idk");
idk.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if("Close".equals(e.getActionCommand())){
System.exit(0);
setVisible(false);
}
if("Maze".equals(e.getActionCommand())){
new Maze();
setVisible(false);
}
if("idk".equals(e.getActionCommand())){
//new idk();
}
}
Code:
}
//JFrame / graphics 1
Code:
package Game;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.Scanner;
import java.awt.image.*;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Maze extends JFrame {
BufferedImage img;
Oval oval = new Oval();
JFrame frame;
int x = 0;
int y = 0;
//Graphics g;
JPanel panel = new JPanel();
JComponent comp;
Maze(){
frame = new JFrame();
frame.setTitle("Maze");
frame.getContentPane();
frame.setSize(800,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setBackground(Color.yellow);
//setPreferredSize(new Dimension(800,800));
setBackground(Color.black);
frame.setVisible(true);
}
public void paintComponent(Graphics2D g) {
super.paintComponents(g);
Graphics2D g2 = (Graphics2D)g;
//img = (BufferedImage)(this.createImage(getWidth(),getHei ght()));
Graphics2D gc = img.createGraphics();
gc.setColor(Color.RED);
oval.drawOval(gc);
}
//public void keyPressed(KeyEvent e) {
// if(e.getKeyCode()==KeyEvent.VK_DOWN){
// System.out.println("key down was pressed");
// g.drawRect(x, y-1, 10, 10);
// g.fillRect(x, y-1, 10, 10);
// g.setColor(Color.black);
// }
// if(e.getKeyCode()==KeyEvent.VK_UP){
// System.out.println(".");
// g.drawRect(x, y+1, 10, 10);
// g.fillRect(x, y+1, 10, 10);
// g.setColor(Color.black);
// }
// if(e.getKeyCode()==KeyEvent.VK_LEFT){
// System.out.println("k");
// g.drawRect(x+1, y, 10, 10);
// }
// if(e.getKeyCode()==KeyEvent.VK_RIGHT){
// System.out.println("lol");
// g.drawRect(x-1, y, 10, 10);
// }
//}
{
}
}
//oval creation / graphics2
Code:
package Game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.*;
public class Oval {
BufferedImage img;
public Oval() {
img = new BufferedImage(800, 800, BufferedImage.OPAQUE);
}
public void drawOval(Graphics a){
Graphics g = img.getGraphics();
int x = 0;
int y = 0;
g.setColor(Color.black);
g.drawOval(x, y, 10, 10);
}
public BufferedImage getOval() {
return img;
}
public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
Thanks, and I know it might be too long for a forum but the tutorials really haven't helped my understanding of Graphics.