12 Replies - 1001 Views - Last Post: 08 March 2014 - 04:44 PM Rate Topic: -----

#1 Anonymous1337   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-November 13

JFrame not repainting

Posted 08 March 2014 - 02:38 PM

I have been trying to repaint some rectangles I created when I press Key Up and nothing is happening at all and I have no Idea why here is my code :
package Graphics;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JComponent;

public class Graphics extends JComponent implements KeyListener{
	private int i = 15;
	private static final long serialVersionUID = 1L;
	public Graphics(){
		
	}
	public void paintComponent(java.awt.Graphics g){
		
		g.setColor(Color.black);
		g.fillRect(0, i, 15, 80);
		java.awt.Graphics g2 = getGraphics();
		g2.setColor(Color.black);
		g.fillRect(380, i, 15, 80);
		

	}
	public void setI(int i){
		this.i = i;
	}
	public int getI(){
		
		return i;
	}
	

	public void keyPressed(KeyEvent e) {
		int KeyCode = e.getKeyCode();
	
		
		if(KeyCode == KeyEvent.VK_W){
			i+=20;
			repaint();
                        System.out.println("You Clicked it");//Yes it does go through this line of code
		}
		
	}
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}



Is This A Good Question/Topic? 0
  • +

Replies To: JFrame not repainting

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: JFrame not repainting

Posted 08 March 2014 - 02:55 PM

Make sure to invoke super.paintComponent() immediately in your paintComponent() method. It handles key super class functionality, such as clearing the screen initially.

Also, it's poor practice and very confusing to name your class Graphics, as well as use the java.awt.Graphics class.
Was This Post Helpful? 0
  • +
  • -

#3 Anonymous1337   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-November 13

Re: JFrame not repainting

Posted 08 March 2014 - 03:06 PM

View Postmacosxnerd101, on 08 March 2014 - 02:55 PM, said:

Make sure to invoke super.paintComponent() immediately in your paintComponent() method. It handles key super class functionality, such as clearing the screen initially.

Also, it's poor practice and very confusing to name your class Graphics, as well as use the java.awt.Graphics class.

Thanks but it is still not working and I will change the class to another name
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: JFrame not repainting

Posted 08 March 2014 - 03:13 PM

Did you remember to add itself as a keylistener?
Also your JComponent needs to be focusable to receive key events
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class GraphicsTest extends JFrame {

	public static void main(String[] args) {
		new GraphicsTest();
	}
	
	public GraphicsTest() {
		add(new MyComponent(), BorderLayout.CENTER);
		setSize(300, 300);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public class MyComponent extends JComponent implements KeyListener{
		private int i = 15;
		private static final long serialVersionUID = 1L;
		
		public MyComponent(){
			setFocusable(true);
			addKeyListener(this);
		}
		
		public void paintComponent(java.awt.Graphics g){
			super.paintComponent(g);
			g.setColor(Color.black);
			g.fillRect(0, i, 15, 80);
			java.awt.Graphics g2 = getGraphics();
			g2.setColor(Color.black);
			g.fillRect(380, i, 15, 80);
			

		}
		public void setI(int i){
			this.i = i;
		}
		public int getI(){
			
			return i;
		}
		

		public void keyPressed(KeyEvent e) {
			int KeyCode = e.getKeyCode();
			System.out.println("Key pressed");
			
			if(KeyCode == KeyEvent.VK_W){
				i+=20;
				repaint();
	                        System.out.println("You Clicked it");//Yes it does go through this line of code
			}
			
		}
		@Override
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
		@Override
		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 Anonymous1337   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-November 13

Re: JFrame not repainting

Posted 08 March 2014 - 03:17 PM

View PostCasiOo, on 08 March 2014 - 03:13 PM, said:

Did you remember to add itself as a keylistener?
Also your JComponent needs to be focusable to receive key events
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class GraphicsTest extends JFrame {

	public static void main(String[] args) {
		new GraphicsTest();
	}
	
	public GraphicsTest() {
		add(new MyComponent(), BorderLayout.CENTER);
		setSize(300, 300);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public class MyComponent extends JComponent implements KeyListener{
		private int i = 15;
		private static final long serialVersionUID = 1L;
		
		public MyComponent(){
			setFocusable(true);
			addKeyListener(this);
		}
		
		public void paintComponent(java.awt.Graphics g){
			super.paintComponent(g);
			g.setColor(Color.black);
			g.fillRect(0, i, 15, 80);
			java.awt.Graphics g2 = getGraphics();
			g2.setColor(Color.black);
			g.fillRect(380, i, 15, 80);
			

		}
		public void setI(int i){
			this.i = i;
		}
		public int getI(){
			
			return i;
		}
		

		public void keyPressed(KeyEvent e) {
			int KeyCode = e.getKeyCode();
			System.out.println("Key pressed");
			
			if(KeyCode == KeyEvent.VK_W){
				i+=20;
				repaint();
	                        System.out.println("You Clicked it");//Yes it does go through this line of code
			}
			
		}
		@Override
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
		@Override
		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}
}


Yes I did in my Frame Class here :
package Display;
import javax.swing.JFrame;

import Graphics.Graphics;

public class Frame extends JFrame{

	private static final long serialVersionUID = 1L;
	java.awt.Graphics g;

	public Frame() {
		super("Pong v0.01 Pre-Alpha");
		initDisplay();
	}
	public void initDisplay(){
		setSize(400,400);//Sets the size of the frame
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Completely close out the program when you exit the program
		setLocationRelativeTo(null);//Puts the frame in the middle of the screen when you first run the program
		setResizable(false);//Makes it so you cannot resize the frame
		addKeyListener(new Graphics());
		add(new Graphics());//adds the graphics to the screen
		
		setVisible(true);//Makes the frame visible
	}
	

}


and Yes I do have a main method its just in another class
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: JFrame not repainting

Posted 08 March 2014 - 03:20 PM

Those are two different Graphic's instances
You need to add the KeyListener to the same Graphic that is being added to the JFrame
Was This Post Helpful? 0
  • +
  • -

#7 Anonymous1337   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-November 13

Re: JFrame not repainting

Posted 08 March 2014 - 03:24 PM

View PostCasiOo, on 08 March 2014 - 03:20 PM, said:

Those are two different Graphic's instances
You need to add the KeyListener to the same Graphic that is being added to the JFrame

You mean like this? :
public Graphics(){
   addKeyListener(this);
}



View PostAnonymous1337, on 08 March 2014 - 03:24 PM, said:

View PostCasiOo, on 08 March 2014 - 03:20 PM, said:

Those are two different Graphic's instances
You need to add the KeyListener to the same Graphic that is being added to the JFrame

You mean like this? :
public Graphics(){
   addKeyListener(this);
}


I tried this and it didnt work
Was This Post Helpful? 0
  • +
  • -

#8 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: JFrame not repainting

Posted 08 March 2014 - 03:29 PM

Well is the component focusable? It needs to be focusable as I wrote earlier
Was This Post Helpful? 0
  • +
  • -

#9 Anonymous1337   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-November 13

Re: JFrame not repainting

Posted 08 March 2014 - 03:31 PM

View PostCasiOo, on 08 March 2014 - 03:29 PM, said:

Well is the component focusable? It needs to be focusable as I wrote earlier

What do you mean by focusable?
Was This Post Helpful? 0
  • +
  • -

#10 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: JFrame not repainting

Posted 08 March 2014 - 03:39 PM

I spend time writing that example
Run it and study the code...
Was This Post Helpful? 0
  • +
  • -

#11 Anonymous1337   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-November 13

Re: JFrame not repainting

Posted 08 March 2014 - 03:57 PM

View PostCasiOo, on 08 March 2014 - 03:39 PM, said:

I spend time writing that example
Run it and study the code...

Thanks it works I have another question I am now trying to make a new Class for input Only and it isnt working at all it only works if I am in the same class here is my code for the input class:
package Graphics.input;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import Graphics.Graphics;

public class Input extends Graphics implements KeyListener {

	private static final long serialVersionUID = 1L;

	public Input() {
		setFocusable(true);
		addKeyListener(this);
	}
	
	
	@Override
	public void keyPressed(KeyEvent e) {

		int KeyCode = e.getKeyCode();


		if(KeyCode == KeyEvent.VK_UP){
			int i = getI();
			i+=20;
			repaint();
			System.out.println("It works");
		}
	}
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}


}



This is the code for the graphics class :
package Graphics;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JComponent;

import Graphics.input.Input;

public class Graphics extends JComponent{
	protected int i = 15;
	private static final long serialVersionUID = 1L;
	public Graphics(){
		
	}
	
	public void setI(int i){
		this.i = i;
	}
	public int getI(){
		
		return i;
	}
	
	@Override 
	public void paintComponent(java.awt.Graphics g){
		super.paintComponent(g);
		g.setColor(Color.black);
		g.fillRect(0, i, 15, 80);
		java.awt.Graphics g2 = getGraphics();
		g2.setColor(Color.black);
		g.fillRect(380, i, 15, 80);


	}
}


Was This Post Helpful? 0
  • +
  • -

#12 Anonymous1337   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-November 13

Re: JFrame not repainting

Posted 08 March 2014 - 04:28 PM

Ummmm anyone else viewing this know What I did wrong??
Was This Post Helpful? 0
  • +
  • -

#13 Anonymous1337   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-November 13

Re: JFrame not repainting

Posted 08 March 2014 - 04:44 PM

View PostCasiOo, on 08 March 2014 - 03:39 PM, said:

I spend time writing that example
Run it and study the code...

You must be busy I know but I really need Help with this Could you help me with my code in my last post
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1