This is my code. I want the key listener to create a rectangle when I press a key. I am having trouble implementing the fill rectangle method into the key listener.
I've tried to separate the key listener, but then, I don't know how to connect them to each other to use the JPanel methods.
import javax.swing.*;
import java.awt.*;
public class Keyboard
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Keyboard");
frame.setLayout(new BorderLayout());
MyPanel displayPanel = new MyPanel();
frame.add(displayPanel, BorderLayout.CENTER);
frame.addKeyListener(displayPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width, java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height);
frame.setVisible(true);
}
}
MyPanel class
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.JComponent;
import javax.swing.JPanel;
public class MyPanel extends JPanel implements KeyListener {
private boolean one;
private boolean two;
private boolean three;
private boolean four;
private boolean five;
private boolean six;
private boolean seven;
private boolean eight;
private boolean nine;
private boolean zero;
private boolean dash;
public MyPanel() {
one = false;
two = false;
three = false;
four = false;
five = false;
six = false;
seven = false;
eight = false;
nine = false;
zero = false;
dash = false;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
setBackground(new Color(51, 25, 0));
for(int i=0; i<11; i++)
{
if(i%2 == 0)
{
g.setColor(Color.WHITE);
g.fillRect(i*(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width/11), 450, 212, 1000);
}
else
{
g.setColor(Color.BLACK);
g.fillRect(i*(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width/11), 450, 174, 1000);
}
if(one==true)
{
g.setColor(Color.BLUE);
g.fillRect(50,50,50,50);
repaint();
}
}
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() ==KeyEvent.VK_1 && !one)
{
System.out.println("asdasda");
one = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_2 && !two)
{
two = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_3 && !three)
{
three = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_4 && !four)
{
four = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_5 && !five)
{
five = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_6 && !six)
{
six = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_7 && !seven)
{
seven = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_8 && !eight)
{
eight = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_9 && !nine)
{
nine = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_0 && !zero)
{
zero = true;
}
else if(e.getKeyCode() == 189 && !dash)
{
dash = true;
}
}
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() ==KeyEvent.VK_1)
{
one = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_2)
{
two = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_3)
{
three = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_4)
{
four = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_5)
{
five = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_6)
{
six = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_7)
{
seven = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_8)
{
eight = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_9)
{
nine = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_0)
{
zero = false;
}
else if(e.getKeyCode() == 189)
{
dash = false;
}
}
public void keyTyped(KeyEvent e)
{
System.out.println("Typed: " + e.getKeyChar());
}
}
I had a similar problem a while ago, I fixed it by creating a small rectangle class like this
package notDeafult;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
*
*/
public class rectangle {
//The basic outline of a rectangle making up the grid
Rectangle rect;
int width;
int height;
public rectangle() {
}
public rectangle(int x, int y) {
this.width = 80; //Setting the size
this.height = 80;
this.rect = new Rectangle(x + 80, y + 30, width, height);//Moving the object to the centre
}
public void draw(Graphics g) {
g.setColor(Color.green);
//.drawRect only draws the outline
g.drawRect(rect.x, rect.y, width, height);
}
}
Then when you're ready simply call a paint method under a new Jpanel, like this:
jPanel3 = new javax.swing.JPanel(){
public void paintComponent(Graphics g) {
super.paintComponent(g);
rectangle rect = new rectangle(width, height);
rect.draw(g)
}
And then you could just give the paintComponent its own method name like paintRectangles or something
and call it here when you get your key press
else if(e.getKeyCode() ==KeyEvent.VK_5){
paintRectangles();
}
Something like that should work, I think.
I did some changes in your class:
paintRectangle
method public class MyPanel extends JPanel implements KeyListener {
private boolean one, two, three, four, five, six, seven, eight, nine, zero, dash;
public MyPanel() {
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(new Color(51, 25, 0));
for (int i = 0; i < 11; i++) {
if (i % 2 == 0) {
g.setColor(Color.WHITE);
g.fillRect(
i * (java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width
/ 11),
450, 212, 1000);
} else {
g.setColor(Color.BLACK);
g.fillRect(
i * (java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width
/ 11),
450, 174, 1000);
}
}
paintRectangle(g);
}
private void paintRectangle(Graphics g) {
if (one) {
g.setColor(Color.BLUE);
g.fillRect(50, 50, 50, 50);
} else if (two) {
g.setColor(Color.CYAN);
g.fillRect(50, 50, 50, 50);
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_1 && !one) {
System.out.println("asdasda");
one = true;
repaint();
} else if (e.getKeyCode() == KeyEvent.VK_2 && !two) {
two = true;
repaint();
} else if (e.getKeyCode() == KeyEvent.VK_3 && !three) {
three = true;
} else if (e.getKeyCode() == KeyEvent.VK_4 && !four) {
four = true;
} else if (e.getKeyCode() == KeyEvent.VK_5 && !five) {
five = true;
} else if (e.getKeyCode() == KeyEvent.VK_6 && !six) {
six = true;
} else if (e.getKeyCode() == KeyEvent.VK_7 && !seven) {
seven = true;
} else if (e.getKeyCode() == KeyEvent.VK_8 && !eight) {
eight = true;
} else if (e.getKeyCode() == KeyEvent.VK_9 && !nine) {
nine = true;
} else if (e.getKeyCode() == KeyEvent.VK_0 && !zero) {
zero = true;
} else if (e.getKeyCode() == 189 && !dash) {
dash = true;
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_1) {
one = false;
} else if (e.getKeyCode() == KeyEvent.VK_2) {
two = false;
} else if (e.getKeyCode() == KeyEvent.VK_3) {
three = false;
} else if (e.getKeyCode() == KeyEvent.VK_4) {
four = false;
} else if (e.getKeyCode() == KeyEvent.VK_5) {
five = false;
} else if (e.getKeyCode() == KeyEvent.VK_6) {
six = false;
} else if (e.getKeyCode() == KeyEvent.VK_7) {
seven = false;
} else if (e.getKeyCode() == KeyEvent.VK_8) {
eight = false;
} else if (e.getKeyCode() == KeyEvent.VK_9) {
nine = false;
} else if (e.getKeyCode() == KeyEvent.VK_0) {
zero = false;
} else if (e.getKeyCode() == 189) {
dash = false;
}
}
public void keyTyped(KeyEvent e) {
System.out.println("Typed: " + e.getKeyChar());
}
}
I figured out the problem. Here is the code for your problem.
import javax.swing.*;
import java.awt.*;
public class Keyboard
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Keyboard");
frame.setLayout(new BorderLayout());
MyPanel displayPanel = new MyPanel();
frame.add(displayPanel, BorderLayout.CENTER);
frame.addKeyListener(displayPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width, java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height);
frame.setVisible(true);
}
}
Here is MyPanel Class
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.ImageObserver;
import java.text.AttributedCharacterIterator;
import javax.swing.JComponent;
import javax.swing.JPanel;
public class MyPanel extends JPanel implements KeyListener {
private boolean one;
private boolean two;
private boolean three;
private boolean four;
private boolean five;
private boolean six;
private boolean seven;
private boolean eight;
private boolean nine;
private boolean zero;
private boolean dash;
Graphics g;
public MyPanel() {
one = false;
two = false;
three = false;
four = false;
five = false;
six = false;
seven = false;
eight = false;
nine = false;
zero = false;
dash = false;
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
setBackground(new Color(51, 25, 0));
for(int i=0; i<11; i++)
{
if(i%2 == 0)
{
g.setColor(Color.WHITE);
g.fillRect(i*(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width/11), 450, 212, 1000);
}
else
{
g.setColor(Color.BLACK);
g.fillRect(i*(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width/11), 450, 174, 1000);
}
if(one==true)
{
g.setColor(Color.BLUE);
g.drawRect(50, 50, 50, 50);
g.fillRect(50,50,50,50);
repaint();
}
}
}
@Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() ==KeyEvent.VK_1)
{
one = true;
repaint();
System.out.println("asdasda");
}
else if(e.getKeyCode() ==KeyEvent.VK_2 && !two)
{
two = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_3 && !three)
{
three = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_4 && !four)
{
four = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_5 && !five)
{
five = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_6 && !six)
{
six = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_7 && !seven)
{
seven = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_8 && !eight)
{
eight = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_9 && !nine)
{
nine = true;
}
else if(e.getKeyCode() ==KeyEvent.VK_0 && !zero)
{
zero = true;
}
else if(e.getKeyCode() == 189 && !dash)
{
dash = true;
}
}
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() ==KeyEvent.VK_1)
{
one = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_2)
{
two = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_3)
{
three = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_4)
{
four = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_5)
{
five = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_6)
{
six = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_7)
{
seven = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_8)
{
eight = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_9)
{
nine = false;
}
else if(e.getKeyCode() ==KeyEvent.VK_0)
{
zero = false;
}
else if(e.getKeyCode() == 189)
{
dash = false;
}
}
public void keyTyped(KeyEvent e)
{
System.out.println("Typed: " + e.getKeyChar());
}
}
The reason your code is not working is that you did not calling repaint() method after setting one = true; . Now your code is working. Check and let me know if there is any confusion.
Note: This only works for Key 1. Now do similar for other Keys.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.