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

    Unhappy Need Help with graphics / Jframe

    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();
    
    }
    
    }
    
    
    
    }
    //JFrame / graphics 1
    Java 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.

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

    Default

    This is the third time you've posted the same question -- please enough! Don't keep reposting the same question. Please go to your original question, edit the first post so that it uses code tags, and paste only code that is already formatted, not this left-justified code like you have above. Locking this post.

Similar Threads

  1. Question on Graphics/JFrame/KeyEvents
    By loopsnhoops in forum New To Java
    Replies: 4
    Last Post: 02-11-2011, 12:22 AM
  2. 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
  3. Drawing a graphics onto another Graphics ?
    By Ziden in forum Java Applets
    Replies: 0
    Last Post: 01-08-2011, 08:30 PM
  4. problem with jframe&jmenu&graphics
    By beni.vd in forum AWT / Swing
    Replies: 1
    Last Post: 12-31-2010, 08:07 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
  •