why isnt this working
the JFrame is made and the paint is working but I can't get the keylistener to work. Ive tried to print something inside the keylistener but it did not show when left arrow was pressed.
import java.awt.event.KeyEvent;
public class movingsquare extends runpaintgui{
public void key(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT){
x = x - 5;
repaint();
System.out.println( x);
}
}
}
other class
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
public class runpaintgui extends JFrame{
int x = 30;
public static void main(String[] args){
runpaintgui frame = new runpaintgui();
frame.setSize(1275, 775);
frame.setResizable(false);
frame.setTitle("game");
frame.setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
g.fill3DRect(x, 30, 60, 60, true);
}
}
Change your code this way:
package de.swisslife.muellerj.test;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class runpaintgui extends JFrame implements KeyListener{
public runpaintgui(){
this.setSize(1275, 775);
this.setResizable(false);
this.setTitle("game");
this.setVisible(true);
this.addKeyListener(this);
this.setVisible(true);;
}
int x = 30;
public static void main(String[] args){
runpaintgui runpaintgui = new runpaintgui();
}
public void paint(Graphics g){
super.paint(g);
g.fill3DRect(x, 30, 60, 60, true);
}
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT){
x = x - 5;
repaint();
System.out.println( x);
}
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
Firstly I would not have a subclass implement from you main program.
I am not exactly sure what you want to do in your program, but you probably just need to implement a key listener like this :
public class Test extends JFrame {
static int x = 30;
public static void main(String[] args) {
final Test frame = new Test();
frame.setSize(1275, 775);
frame.setResizable(false);
frame.setTitle("game");
frame.setVisible(true);
frame.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_LEFT){
x = x - 5;
frame.repaint();
System.out.println( x);
}
}
#Override
public void keyReleased(KeyEvent arg0) {}
#Override
public void keyPressed(KeyEvent arg0) {}
});
}
public void paint(Graphics g) {
super.paint(g);
g.fill3DRect(x, 30, 60, 60, true);
}
}
Related
Hello I'm trying to create a brick breaker game. There is a main class called Real_Main and another class called Real_Create that extends JPanel. I feel like my code is right but I can't seem to move my rectangle with the right and left arrow keys. Could anyone tell me what's wrong with it? And why do we need a timer to move the rectangle?
import javax.swing.JFrame;
public class Real_Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jf= new JFrame();
Real_Create panel=new Real_Create();
jf.setSize(500, 500);
jf.setVisible(true);
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(panel);
}
}
Real_Create:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Real_Create extends JPanel implements KeyListener,ActionListener {
int baseX=200;
int ballX=250;
int ballY=250;
int delay=20;
Timer tim;
Boolean play=false;
public Real_Create(){
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
tim=new Timer(delay,this);
tim.start();
}
public void paint(Graphics g){
//background
g.setColor(Color.black);
g.fillRect(1,1,500,500 );
//baseplayer
g.setColor(Color.white);
g.fillRect(baseX, 430, 80, 10);
//ball
g.setColor(Color.green);
g.fillOval(ballX, ballY, 20, 20);
g.dispose();
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
tim.start();
repaint();
}
#Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()== KeyEvent.VK_RIGHT){
if(baseX >=420){
baseX=420;
}
else{
moveRight();
}
}
if(e.getKeyCode()== KeyEvent.VK_LEFT){
if(baseX <=0){
baseX=0;
}
else{
moveLeft();
}
}
}
#Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public void moveRight(){
play=true;
baseX+=10;
}
public void moveLeft(){
play=true;
baseX-=10;
}
}
Image from the game:
What you are dealing with is unpredictable Swing behaviour due to its lack of thread safety. According to documentation "all code that creates or interacts with Swing components must run on the event dispatch thread". To achieve that you should rewrite your GUI creation like so:
public class Real_Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
JFrame jf = new JFrame();
Real_Create panel = new Real_Create();
jf.setSize(500, 500);
jf.setVisible(true);
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(panel);
}
}
You can read more on the subject here.
So I'm trying to draw a circle where a user clicks, which can then be re-sized by the bar down below. Everything works except for that the circle won't draw where I want it to. Any suggestions?
Here is my Panel
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.event.*;
public class TestClass extends JFrame {
private JSlider slide;
private MainClass myPanel;
public int x1=0;
public int y1=0;
public TestClass(){
super("The Title");
myPanel = new MainClass();
myPanel.setBackground(Color.YELLOW);
slide = new JSlider(SwingConstants.HORIZONTAL, 0, 200, 10);
slide.setMajorTickSpacing(10);
slide.setPaintTicks(true);
slide.addChangeListener(
new ChangeListener(){
public void stateChanged(ChangeEvent e){
myPanel.checkDiameter(slide.getValue());
}
}
);
HandlerClass handler = new HandlerClass();
slide.addMouseListener(handler);
add(slide, BorderLayout.SOUTH);
add(myPanel, BorderLayout.CENTER);
}
public int setX1(){
return x1;
}
public int setY1(){
return y1;
}
private class HandlerClass implements MouseListener{
#Override
public void mouseClicked(MouseEvent event) {
// TODO Auto-generated method stub
x1=event.getX();
y1=event.getY();
repaint();
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
}
Here is the other important class which creates a window and call TestClass;
import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class MainClass extends JPanel{
private int d = 10;
public void paintComponent(Graphics g){
super.paintComponent(g);
TestClass values = new TestClass();
g.setColor(Color.CYAN);
g.fillOval(values.setX1()+50, values.setY1(), d, d);
}
public void checkDiameter(int newD)
{
//New format for if statements
d = (newD >= 0 ? newD //if
: 10//else
);
repaint();
}
public Dimension getPreferredSize(){
return new Dimension(200,200);
}
public Dimension getMinimumSize(){
return getPreferredSize();
}
}
Hey Guys I found a way to get it working.
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.event.*;
public class TestClass extends JFrame {
private JSlider slide;
private MainClass myPanel;
public static int x1=0,y1=0;
public TestClass(){
super("The Title");
myPanel = new Panel();
myPanel.setBackground(Color.YELLOW);
//Allows you to re-size the drawn circle
slide = new JSlider(SwingConstants.HORIZONTAL, 0, 200, 10);
slide.setMajorTickSpacing(10);
slide.setPaintTicks(true);
slide.addChangeListener(
new ChangeListener(){
public void stateChanged(ChangeEvent e){
myPanel.checkDiameter(slide.getValue());
}
}
);
//Create a way to handle user mouse events
HandlerClass handler = new HandlerClass();
myPanel.addMouseListener(handler);
add(slide, BorderLayout.SOUTH);
add(myPanel, BorderLayout.CENTER);
}
private class HandlerClass implements MouseListener{
#Override
public void mouseClicked(MouseEvent event) {
// TODO Auto-generated method stub
Right here is what is new with the code, this method gets the x and y coordinates of a click and sends it to MainClass object myPanel which has the setPosition method; class show before.
myPanel.setPosition(event.getX(),event.getY());
repaint(); }
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
}
Here is the MainClass class, it is poorley named as it's not actually the main class...
import java.awt.;
import javax.swing.;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Panel extends JPanel{
private int x1,y1;
private int d = 10;
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.CYAN);
g.fillOval(x1, y1, d, d);
}
public void checkDiameter(int newD)
{
//New format for if statements
d = (newD >= 0 ? newD //if
: 10//else
);
repaint();
}
public void setPosition(int newX, int newY) {
this.x1 = newX;
this.y1 = newY;
repaint();
}
public Dimension getPreferredSize(){
return new Dimension(200,200);
}
public Dimension getMinimumSize(){
return getPreferredSize();
}
}
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.*;
import java.util.*;
public class test1 extends JFrame implements MouseListener {
private JPanel JP = new JPanel();
public test1() {
JP.setBorder(BorderFactory.createLineBorder(Color.black));
JP.addMouseListener(this);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.add(JP);
this.pack();
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
test1 frame = new test1();
frame.setSize(400,400);
frame.setVisible(true);
}
});
}
public void mouseClicked(MouseEvent e) {
//drawCircle(e.getX(), e.getY());
//repaint();
ballball ball;
ball = new ballball();
//ball.paintComponent(Graphics g);
System.out.println("ballball");
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
//this.mouseX=e.getX();
//this.mouseY=e.getY();
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
}
class ballball extends test1 implements Runnable {
private int squareX = 50;
private int squareY = 50;
private int squareW = 100;
private int squareH = 100;
public boolean draw;
private Vector<Object> v = new Vector<Object>();
public ballball() {
/*addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
draw = true;
//Thread thread1 = new Thread(this.moveSquare(50, 50));
repaint();
//moveSquare(e.getX(),e.getY());
}
});*/
/*addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
moveSquare(e.getX(),e.getY());
}
});*/
System.out.println("ball created");
this.repaint();
}
public void run() {
}
private void moveSquare(int x, int y) {
int OFFSET = 1;
if ((squareX!=x) || (squareY!=y)) {
repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
squareX=x;
squareY=y;
repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
}
}
public void paint(Graphics g) {
g.drawString("abcasdfasffasfas", 10, 10);
}
//#Override
public void paintComponent(Graphics g) {
//if (draw) {
// existing code
System.out.println("paint");
//super.paintComponent(g);
//g.drawString("This is my custom Panel!",10,20);
//g.setColor(Color.RED);
//g.fillRect(squareX,squareY,squareW,squareH);
//g.setColor(Color.BLACK);
//g.drawRect(squareX,squareY,squareW,squareH);
Shape circle = new Ellipse2D.Float(squareX,squareY,100f,100f);
Graphics2D ga = (Graphics2D)g;
ga.draw(circle);
//}
}
}
The aim of the program is to click to create the circle, the ballball class extends the test1, when test1 detect the mouse click, the ballball object created. But the paint/paintComponent method is never be executed. In my program structure, is it possible to paint the circle to the super class JPanel?
JFrame is not a JComponent, it doesn't have a paintComponent method you can override. Instead you could extend a JPanel and add it to the frame.
I am trying to create a canvas in which I can paint,and a button which resets the canvas in an empty state.
However using BorderLayout my button gets duplicated,the second one is an image copy of the first one but still it doesnt look good.
Is there anything wrong in my code and how can it be fixed.
PaintCanvas.java
import javax.swing.* ;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
class framakryesore extends JFrame {
PaintCanvas p1 = new PaintCanvas();
JButton b1 = new JButton ("Reset");
public framakryesore (){
b1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
clearcanvas(p1);
repaint();
}
});
this.add(b1,BorderLayout.NORTH);
this.add(p1,BorderLayout.CENTER);
}
public void clearcanvas(PaintCanvas p){
p.setX(0);
p.setY(0);
}
}
public class PaintCanvas extends JPanel {
private int x = 0 ;
private int y = 0 ;
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public PaintCanvas() {
addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseDragged(MouseEvent e){
x=e.getX();
y=e.getY();
repaint();
}
public void mouseMoved(MouseEvent e){
}
});
}
#Override
protected void paintComponent (Graphics g){
if (x==0 && y==0){
super.paintComponent(g);
}
else
{
g.setFont(new Font("TimesRoman", Font.BOLD, 30));
g.drawString(".", x, y);
}
}
}
PaintCanvasTest.java
import javax.swing.JFrame;
public class PaintCanvasTest {
public static void main(String args[]){
framakryesore pikturo = new framakryesore();
pikturo.setVisible(true);
pikturo.setSize(640, 480);
pikturo.setTitle("Pikturo");
pikturo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
if (x==0 && y==0){
super.paintComponent(g);
You should always invoke super.paintComponent(). It is responsible for clearing the background of the panel before you do your custom painting.
Can someone please tell me how i can get the value of int x in my following code:
when i move the square left and right, the output from Test stays as 500.
I am wanting the Thread in Test to get the value of x every time it loops, but x is always 500 (from the thread) even though the value of x is changing in Drawing.java. please help me with this.
package block;
public class Test extends Drawing implements Runnable{
Thread collision = new Thread(this);
public Test() {
collision.start();
}
#Override
public void run() {
while(true) {
System.out.println(x);
}
}
}
package block;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Drawing extends Canvas implements Runnable, KeyListener{
Thread thread = new Thread(this);
int x = 500;
int y = 540;
public Drawing() {
setSize(1000, 800);
addKeyListener(this);
thread.start();
}
public void update(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, 1000, 700);
paint(g);
}
public void paint(Graphics g) {
g.setColor(Color.cyan);
g.fillRect(x, y, 50, 50);
}
#Override
public void run() {
while(true) {
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Drawing.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
#Override
public void keyTyped(KeyEvent ke) {
}
#Override
public void keyPressed(KeyEvent ke) {
if(ke.getKeyCode() == KeyEvent.VK_LEFT) {
x -= 5;
}
if(ke.getKeyCode() == KeyEvent.VK_RIGHT) {
x += 5;
}
}
#Override
public void keyReleased(KeyEvent ke) {
}
}
EDIT: --------------------
package block;
import javax.swing.JFrame;
public class Block extends JFrame{
Test test = new Test();
public Block() {
setSize(1000, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(new Drawing());
setVisible(true);
createBufferStrategy(2);
}
public static void main(String[] args) {
new Block();
}
}
You are creating multiple instances of x because you are not using inheritance correctly. When you call new Test(), that creates one instance of a Test (which contains a Drawing, which contains a member x), and then the call to new Drawing() creates another instance of a Drawing (which contains a different member x).
To fix this, you could pass a reference to a Drawing object to your Test constructor, and access the x member through the reference.