Results 1 to 5 of 5
  1. #1
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Exclamation 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

    Java Code:
    package Game;
    
    public class main {
    
    public static void main(String[] args){
    
    new Starter();
    
    }
    
    }
    //initiator
    Java 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();
    
    }
    
    }
    Java 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

    Java 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.
    Last edited by loopsnhoops; 02-11-2011 at 12:20 AM. Reason: Because some f@g made me.

  2. #2
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default

    Whats the problem? Is the even triggering? Does it even compile?

  3. #3
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    The problem is there is no oval drawn to the screen and I just want some tips on how to make the keys tie to the oval.

  4. #4
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Can you put your code in [code ][/code ] tags? (no spaces)
    It will be easier to understand.

  5. #5
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    Ok, happy moderator? Anyways PhQ do you have any suggestions? They would be greatly appreciated. :)

    Thanks

Similar Threads

  1. Question on Java Graphics, KeyBoard Events and JFrame
    By loopsnhoops in forum New To Java
    Replies: 1
    Last Post: 02-10-2011, 04:12 AM
  2. problem with jframe&jmenu&graphics
    By beni.vd in forum AWT / Swing
    Replies: 1
    Last Post: 12-31-2010, 08:07 PM
  3. java graphics question
    By bobCallahan24 in forum AWT / Swing
    Replies: 1
    Last Post: 01-15-2010, 07:04 AM
  4. Question about java keyevents?
    By Godsent in forum AWT / Swing
    Replies: 2
    Last Post: 10-26-2009, 10:38 PM
  5. Graphics with JFrame
    By kandt in forum New To Java
    Replies: 3
    Last Post: 12-02-2008, 01:51 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •