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

    Default Question on Graphics etc...

    I want the user to be able to control the oval on the screen with the arrow keys. Here is my code.

    //main

    package Game;

    public class main {

    public static void main(String[] args){

    new Starter();

    }

    }


    //initiator


    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();

    }

    }



    }



    //JFrame / graphics 1


    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



    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.

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

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Question on Graphics/JFrame/KeyEvents
    By loopsnhoops in forum New To Java
    Replies: 4
    Last Post: 02-10-2011, 11:22 PM
  2. Question on Java Graphics, KeyBoard Events and JFrame
    By loopsnhoops in forum New To Java
    Replies: 1
    Last Post: 02-10-2011, 03:12 AM
  3. Drawing a graphics onto another Graphics ?
    By Ziden in forum Java Applets
    Replies: 0
    Last Post: 01-08-2011, 07:30 PM
  4. java graphics question
    By bobCallahan24 in forum AWT / Swing
    Replies: 1
    Last Post: 01-15-2010, 06:04 AM
  5. Graphics
    By feniger in forum New To Java
    Replies: 1
    Last Post: 12-29-2007, 04:22 PM

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
  •