My keybinding didn't do its actionPerformed in JPanel - java

I've added keybinding on one of my JPanels. The problem is this keybinding didn't do its "actionPerformed". Even though I put a sysout in the actionPerformed, nothing was outputted on the console. Can someone help me with this problem? I've already tried to disable my buttons, but still my keybinding doesn't work.
package project.fin;
import java.awt.*;
import java.io.*;
import java.util.List;
import java.awt.event.*;
import javax.swing.*;
//Panel for my game
public class GamePlayPanel extends JPanel{
private Image current;
private Baby bayi;
public GamePlayPanel(String img) {
Dimension size = new Dimension(1200, 500);
this.setPreferredSize(size);
this.setMaximumSize(size);
this.setMinimumSize(size);
this.setSize(size);
this.setLayout(null);
//An baby object
bayi = new Baby(100, 410, 5);
//this is where my keyBinding initialized
bayi.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0), "moveRight");
bayi.getActionMap().put("moveRight", new Move_it(1));
}
//this is the action class that i want to put in my keybinding
private class Move_it extends AbstractAction{
int code;
public Move_it(int code) {
this.code=code;
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("test\n");
if (this.code==1) {
bayi.MoveRight();
}
repaint();
}
}
//To draw my baby
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
bayi.draw(g);
}
}
This is my baby class:
package project.fin;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import java.awt.event.*;
public class Baby extends JComponent{
float x, y;
float speed;
Image current;
private List <Image> ImgPool;
private int Current;
public Baby(float x, float y, float speed) {
// TODO Auto-generated constructor stub
this.x = x;
this.y = y;
this.speed = speed;
ImgPool = new ArrayList<Image>();
//These are just some images that i use to build my moving baby
ImgPool.add(new ImageIcon("baby1_50.png").getImage());
ImgPool.add(new ImageIcon("baby2_50.png").getImage());
ImgPool.add(new ImageIcon("baby1_50.png").getImage());
ImgPool.add(new ImageIcon("baby3_50.png").getImage());
this.current = ImgPool.get(0);
this.Current = 0;
}
//The action that i want my baby to do when a key is pressed
public void MoveRight() {
if (x>600) return;
this.x+=speed;
if (this.Current==3)this.Current=0;
else
this.Current++;
this.current = this.ImgPool.get(Current);
}
public void draw(Graphics g) {
g.drawImage(this.current, (int)this.x, (int)this.y, null);
}
}

Baby isn't attached to the component hierarchy and therefore won't receive any key events. In fact, the design doesn't make sense. There's no need for Bady to extend from JPanel at all.
Instead, make use of the GamePlayPanel directly
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.*;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new GamePlayPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class GamePlayPanel extends JPanel {
private Baby bayi;
public GamePlayPanel() {
//An baby object
bayi = new Baby(100, 410, 5);
//this is where my keyBinding initialized
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "moveRight");
getActionMap().put("moveRight", new Move_it(1));
}
#Override
public Dimension getPreferredSize() {
return new Dimension(1200, 500);
}
//this is the action class that i want to put in my keybinding
private class Move_it extends AbstractAction {
int code;
public Move_it(int code) {
this.code = code;
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("test\n");
if (this.code == 1) {
bayi.moveRight();
}
repaint();
}
}
//To draw my baby
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
bayi.draw(g);
}
}
public class Baby {
float x, y;
float speed;
Image current;
private List<Image> ImgPool;
private int Current;
public Baby(float x, float y, float speed) {
// TODO Auto-generated constructor stub
this.x = x;
this.y = y;
this.speed = speed;
ImgPool = new ArrayList<Image>();
//These are just some images that i use to build my moving baby
ImgPool.add(new ImageIcon("baby1_50.png").getImage());
ImgPool.add(new ImageIcon("baby2_50.png").getImage());
ImgPool.add(new ImageIcon("baby1_50.png").getImage());
ImgPool.add(new ImageIcon("baby3_50.png").getImage());
this.current = ImgPool.get(0);
this.Current = 0;
}
//The action that i want my baby to do when a key is pressed
public void moveRight() {
if (x > 600) {
return;
}
this.x += speed;
if (this.Current == 3) {
this.Current = 0;
} else {
this.Current++;
}
this.current = this.ImgPool.get(Current);
}
public void draw(Graphics g) {
g.drawImage(this.current, (int) this.x, (int) this.y, null);
}
}
}

Related

Use MouseListener to draw circles?

I'm trying to write a program that allows user to specify a circle with 2 mouse presses, the first one on the center and the second on a point on the periphery, and the program I wrote is this.
import javax.swing.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.*;
public class CircleFrame extends JFrame
{
public CircleFrame()
{
circle = new Ellipse2D.Double();
hasCenter = false;
createComponent();
setSize(400, 400);
}
private void createComponent()
{
class CircleComponent extends JComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
circle.setFrame(xTop, yTop, radius*2, radius*2);
g2.draw(circle);
}
}
class MousePressListener1 extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
if(!hasCenter)
{
xCenter = event.getX();
yCenter = event.getY();
hasCenter = true;
}
}
}
class MousePressListener2 extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
if (hasCenter)
{
xOut = event.getX();
yOut = event.getY();
xTop = xCenter - Math.abs(xOut - xCenter);
yTop = yCenter - Math.abs(yOut - yCenter);
radius =
Math.sqrt((xOut - xCenter)*(xOut - xCenter) + (yOut - yCenter)*(yOut - yCenter));
hasCenter = false;
}
}
}
addMouseListener(new MousePressListener1());
addMouseListener(new MousePressListener2());
CircleComponent component = new CircleComponent();
add(component);
}
private double xTop;
private double yTop;
private int xCenter;
private int yCenter;
private int xOut;
private int yOut;
private Ellipse2D.Double circle;
private double radius;
private boolean hasCenter;
}
And this is the main class
import javax.swing.JFrame;
public class CircleFrameViewer
{
public static void main(String[] args)
{
JFrame frame = new CircleFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Somehow it only shows a blank frame, and no clicks make anything happen.
Anyone can help me?
Don't use two listeners, use one, both listeners will be called whenever a mouse event occurs.
MouseEvents are contextual to the component which generated them. This means you should be adding your listener to the CircleComponent
Make sure you are calling repaint when you want the UI to be updated.
Something like...for example
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test2 {
public static void main(String[] args) {
new Test2();
}
public Test2() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Point centerPoint;
private Shape circle;
public TestPane() {
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if (centerPoint == null) {
centerPoint = e.getPoint();
repaint();
} else {
double xTop = Math.min(e.getPoint().x, centerPoint.x);
double yTop = Math.min(e.getPoint().y, centerPoint.y);
double xBottom = Math.max(e.getPoint().x, centerPoint.x);
double yBottom = Math.max(e.getPoint().y, centerPoint.y);
double radius = Math.max(xBottom - xTop, yBottom - yTop);
xTop = centerPoint.x - radius;
yTop = centerPoint.y - radius;
radius *= 2;
circle = new Ellipse2D.Double(xTop, yTop, radius, radius);
repaint();
}
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (circle != null) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.draw(circle);
g2d.dispose();
}
}
}
}

Why my JPanel is not painting anything after I changed the design to same class [duplicate]

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.

Yet another KeyListener/KeyBinding Issue

Ugh, I'm sorry MadProgrammer but I just couldn't get the KeyBinding to work the way I wanted it to :(. But I'll keep looking at some more tutorials till I figure it out. For now though I've stuck to a KeyListener and it works. But now I'm having an issue where p.move(); doesn't actually move the player. All other code I've put in works fine except p.move();. I probably shouldn't be asking this many questions, so if you want me to stop just say so, but the whole SO community is really nice. Again, I'll post the code.
Main class:
import javax.swing.*;
public class Game extends JFrame{
public static void main(String[] args){
new Game();
}
public Game(){
add(new Board());
setTitle("Hi mom");
setSize(555,330);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(3);
setVisible(true);
}
}
Board Class:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Board extends JPanel implements ActionListener {
Image background;
Player p;
boolean moving;
public Board() {
setFocusable(true);
requestFocus();
addKeyListener(new KeyInputEvents());
Timer timer = new Timer(25, this);
timer.start();
ImageIcon img = new ImageIcon(getClass().getResource("images/map.png"));
background = img.getImage();
p = new Player();
}
public void paint(Graphics g) {
g.drawImage(background, 0, 0, null);
g.drawImage(p.getPlayer(), p.getX(), p.getY(), null);
}
public void actionPerformed(ActionEvent e) {
repaint();
}
public JPanel getBoard(){
return this;
}
)
Player Class (This is probably where stuff goes wrong):
import javax.swing.*;
import java.awt.*;
public class Player{
int x = 30;
int y = 187;
Image player;
public Player(){
ImageIcon img = new ImageIcon(getClass().getResource("images/player.png"));
player = img.getImage();
}
public Image getPlayer(){
return player;
}
public void move(int x, int y){
this.x += x;
this.y += y;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
)
KeyInputEvents Class:
import java.awt.event.*;
import javax.swing.*;
public class KeyInputEvents extends KeyAdapter implements ActionListener{
int k;
boolean moving = true;
Player p = new Player();
public KeyInputEvents(){
Timer timer = new Timer(25,this);
timer.start();
}
public void keyPressed(KeyEvent e){
k = e.getKeyCode();
moving = true;
}
public void keyReleased(KeyEvent e){
moving = false;
}
public void actionPerformed(ActionEvent e) {
if(k == 'D' && moving == true){p.move(5,0);}
if(k == 'A' && moving == true){p.move(-5,0);}
}
}
The Player that is on the screen is not the same Player you are moving in your KeyInputEvents class...
In Board you create an instance of Player...
public class Board extends JPanel implements ActionListener {
Player p;
public Board() {
//...
p = new Player();
}
And in KeyInputEvents you create another one...
public class KeyInputEvents extends KeyAdapter implements ActionListener {
//....
Player p = new Player();
These two instances are in no way related...
While I'm here, you shouldn't really override paint, but instead, override paintComponent and you should definitely be calling super.paintXxx
Update with a Key Bindings example
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
public class Game extends JFrame {
public static void main(String[] args) {
new Game();
}
public Game() {
add(new Board());
setTitle("Hi mom");
setSize(555, 330);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(3);
setVisible(true);
}
public class Board extends JPanel implements ActionListener {
Image background;
Player p;
private int xDelta, yDelta;
public Board() {
setFocusable(true);
requestFocus();
ImageIcon img = new ImageIcon(getClass().getResource("/images/map.jpg"));
background = img.getImage();
p = new Player();
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "Left.move");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true), "Left.stop");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "Right.move");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "Right.stop");
ActionMap am = getActionMap();
am.put("Left.move", new MoveLeft(this));
am.put("Left.stop", new StopAllMovement(this));
am.put("Right.move", new MoveRight(this));
am.put("Right.stop", new StopAllMovement(this));
Timer timer = new Timer(25, this);
timer.start();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
g.drawImage(background, 0, 0, null);
g.drawImage(p.getPlayer(), p.getX(), p.getY(), null);
}
protected void setMovement(int xDelta, int yDelta) {
this.xDelta = xDelta;
this.yDelta = yDelta;
}
public void actionPerformed(ActionEvent e) {
p.move(xDelta, yDelta);
repaint();
}
public JPanel getBoard() {
return this;
}
}
public class Player {
int x = 30;
int y = 187;
Image player;
public Player() {
ImageIcon img = new ImageIcon(getClass().getResource("/images/player.png"));
player = img.getImage();
}
public Image getPlayer() {
return player;
}
public void move(int x, int y) {
this.x += x;
this.y += y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
public abstract class AbstractMove extends AbstractAction {
private Board board;
private int xDelta;
private int yDelta;
public AbstractMove(Board board, int xDelta, int yDelta) {
this.board = board;
this.xDelta = xDelta;
this.yDelta = yDelta;
}
#Override
public void actionPerformed(ActionEvent e) {
board.setMovement(xDelta, yDelta);
}
}
public class MoveLeft extends AbstractMove {
public MoveLeft(Board board) {
super(board, -5, 0);
}
}
public class MoveRight extends AbstractMove {
public MoveRight(Board board) {
super(board, 5, 0);
}
}
public class StopAllMovement extends AbstractMove {
public StopAllMovement(Board board) {
super(board, 0, 0);
}
}
}

Key Pressed wont work

Im having trouble getting the key pressed event to work, it doesn't recognize key input, can someone please help?
I have three classes player, exe, and board
Here is the board class
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Board extends JPanel implements ActionListener{
private Timer timer;
private Player player;
//private Floor
public Board() {
addKeyListener( new TAdapter() );
setFocusable(true);
setBackground(Color.WHITE);
setDoubleBuffered(true);
player = new Player();
//floor = new Floor();
timer = new Timer(5, this);
timer.start();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(player.getImage(), player.getX(), player.getY(), this);
// g2d.drawImage(floor.getImage(), floor.getX(), floor.getY(), this);
//System.out.println(player.getX() + ", " + player.getY());
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void actionPerformed(ActionEvent e) {
// checkPlayerOnGround();
player.move();
repaint();
}
private class TAdapter extends KeyAdapter implements KeyListener{
public void keyTyped(KeyEvent e) {
player.keyReleased(e);
System.out.println("Released");
}
public void keyPressed(KeyEvent e) {
player.keyPressed(e);
System.out.println("Pressed");
}
}
}
here is the player class
import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.Timer;
import java.util.TimerTask;
public class Player{
private String player = "player.jpg";
private int moveX;
private int moveY;
private int x;
private int y;
private Image image;
private boolean canFall = false;
//private Timer jumpTimer = new Timer();
boolean canJump = true;
public Player() {
ImageIcon playeriImage = new ImageIcon(this.getClass().getResource(player));
image = playeriImage.getImage();
x = 40;
y = 60;
}
public void checkFall() {
if(canFall) {
moveY = -4;
}
}
public void move() {
//checkFall();
x = moveX + x;
y = moveY + y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Image getImage() {
return image;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_A) {
moveX = -4;
}
if (key == KeyEvent.VK_D) {
moveX = 4;
}
if (key == KeyEvent.VK_W) {
if(canJump)
this.jump();
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_A) {
moveX = 0;
}
if (key == KeyEvent.VK_D) {
moveX = 0;
}
}
here is the exe class
import javax.swing.JFrame;
public class Exe extends JFrame {
public Exe() {
add(new Board());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setLocationRelativeTo(null);
setTitle("TSA Video Game");
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new Exe();
}
}
thanks in advance
KeyListener will only respond to key events IF the component is focusable AND has focus.
It would be better to use the Key Bindings API which will allow you to overcome this limitation (if you want to)
I would also recommend that you override paintComponent instead of paint. You should also not be calling Graphics#dispose, as you did not create the Graphics context, this could actually prevent anything from being painted on some systems
I would also suggest that a delay of 5 milliseconds (on your Timer) might be a little excessive (5 milliseconds = 200fps!).
A value of 16 (60fps) or 40 (25fps) may be more suitable and reduce the overhead on the system...IMHO

Cannot manage to use ActionListener between two classes

I try to implement two classes, one dedicated to the interface and the other one dedicated to the canvas allowing user to draw shapes on it.
So basically, let's take one of my button, here is my Jbutton bCircle where I want to initialize the action in an anonymous class. I have put just the specific piece :
public class Application extends JFrame implements ActionListener {
toolBar.add(Box.createHorizontalGlue());
toolBar.add(Box.createVerticalGlue());
toolBar.add(bSquare, toolBar.getComponentCount() - 1);
toolBar.add(bCircle, toolBar.getComponentCount() - 1);
bCircle.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
//Action ?????
}
});
toolBar.add(bRectangle, toolBar.getComponentCount() - 1);
toolBar.add(bTriangle, toolBar.getComponentCount() - 1);
toolBar.add(bErase, toolBar.getComponentCount() - 1);
toolBar.setOrientation(SwingConstants.VERTICAL);
container.add(toolBar, BorderLayout.WEST);
}
And in the other class, here is the action I want the selected state of the Jbutton bCircle to execute when I click on the canvas :
public class DrawingCanvas extends JPanel implements MouseListener, MouseMotionListener {
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == bCircle) {
shapes.add(new Circle(e.getX(),e.getY()));
}
repaint();
}
}
shapes is a ArrayList stocking the shapes to make the canvas easier to clean, Circle is a class which only contains drawing of the circle.
At the end, the purpose is to allow user to click first on four buttons representing four shapes, then draw them by a mouse pressed on the canvas. But I don't know how to link theses two classes, please help me,
Thanks in advance,
P.S : the full Application class (interface) that I have updated a second time:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
public class Application extends JFrame implements ActionListener {
{
//Set appearance Look & Feel for the window
try { javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch(Exception e) {
e.printStackTrace();
}
}
private DrawingCanvas canvas = new DrawingCanvas();
private JPanel container = new JPanel();
private JPanel commandes = new JPanel();
private JMenuBar menu = new JMenuBar();
private JMenu newFile = new JMenu();
private JMenu open = new JMenu();
private JMenu save = new JMenu();
private JFileChooser fileChooser;
private JToolBar toolBar = new JToolBar();
private JButton bSquare = new JButton("Square");
private JButton bRectangle = new JButton("Rectangle");
private JButton bCircle = new JButton("Circle");
private JButton bTriangle = new JButton("Triangle");
private JButton bErase = new JButton("CLEAR");
//public static boolean bIsSelected = false;
public Application(){
this.setTitle("Animation");
this.setSize(579, 432);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
container.setBackground(Color.white);
container.setLayout(new BorderLayout());
container.add(canvas, BorderLayout.CENTER);
this.setContentPane(container);
toolBar.add(Box.createHorizontalGlue());
toolBar.add(Box.createVerticalGlue());
toolBar.add(bSquare, toolBar.getComponentCount() - 1);
toolBar.add(bCircle, toolBar.getComponentCount() - 1);
bCircle.addActionListener(new SampleActionListener(canvas, new Circle()));
toolBar.add(bRectangle, toolBar.getComponentCount() - 1);
toolBar.add(bTriangle, toolBar.getComponentCount() - 1);
toolBar.add(bErase, toolBar.getComponentCount() - 1);
bErase.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent erase){
canvas.getShapes().clear();
repaint();
}
});
toolBar.setOrientation(SwingConstants.VERTICAL);
container.add(toolBar, BorderLayout.WEST);
menu.add(newFile);
newFile.setIcon(new ImageIcon("src/images/new.png"));
menu.add(open);
open.setIcon(new ImageIcon("src/images/open.png"));
menu.add(save);
save.setIcon(new ImageIcon("src/images/save.png"));
this.setJMenuBar(menu);
}
public class SampleActionListener implements ActionListener {
private final DrawingCanvas canvas2;
private final ShapeGenerator shapeGenerator;
public SampleActionListener(DrawingCanvas canvas, ShapeGenerator shapeGenerator) {
this.canvas2 = canvas;
this.shapeGenerator = shapeGenerator;
}
public void actionPerformed(ActionEvent event) {
this.canvas2.setShapeGenerator(shapeGenerator);
}
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Application App = new Application();
}
}
Here is the class for the canvas, uploaded a second time as well :
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.JPanel;
public class DrawingCanvas extends JPanel implements MouseListener {
private ArrayList<Shape> shapes = new ArrayList<>();
private Shape shapeUnderMouse = null;
public ArrayList<Shape> getShapes() {
return shapes;
}
public void setShapes(ArrayList<Shape> shapes) {
this.shapes = shapes;
}
public DrawingCanvas() {
super();
addMouseListener(this);
}
public void paint(Graphics g){
for(int i = 0;i < shapes.size();i++){
shapes.get(i).paint(g);
}
}
#Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void addShape(Shape anyShape)
{
shapes.add(anyShape);
}
#Override
public void mousePressed(MouseEvent e) {
shapes.add(this.shapeGenerator.getGeneratedShape(e.getX(), e.getY()));
repaint();
}
#Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void setShapeGenerator(ShapeGenerator shapeGenerator) {
// TODO Auto-generated method stub
}
Now, the class shape :
import javax.swing.JPanel;
public class Shape extends JPanel{
protected int posX;
protected int posY;
/*public Shape(int posX, int posY) {
super();
this.posX = posX;
this.posY = posY;
}*/
public int getPosX() {
return posX;
}
public void setPosX(int posX) {
this.posX = posX;
}
public int getPosY() {
return posY;
}
public void setPosY(int posY) {
this.posY = posY;
}
}
My little circle class :
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Ellipse2D.Float;
import javax.swing.JPanel;
public class Circle extends Shape implements ShapeGenerator{
/*public Circle(int posX, int posY) {
super(posX, posY);
// TODO Auto-generated constructor stub
}*/
public void paint(Graphics g){
g.fillOval(posX,posY,40,40);
g.setColor(Color.blue);
}
public Shape getGeneratedShape(int x, int y) {
return new Ellipse2D.Float(x, y, 10, 10); //--> does not work, ask me to change method type to a float
return new Circle();
}
}
And finally the interface, all the class are in separate classes :
public interface ShapeGenerator {
Shape getGeneratedShape(int posX, int posY);
}
In fact, it's pretty easy. Don't use anonymous class.
For example, with your code, you just have to attach a defined listener. For example :
bCircle.addActionListener(new SampleActionListener(pan, new CircleShapeGenerator()));
Here is the SampleActionListener witch implements the ActionListener interface
public class SampleActionListener implements ActionListener {
private final DrawingCanvas canvas;
private final ShapeGenerator shapeGenerator;
public SampleActionListener(DrawingCanvas canvas, ShapeGenerator shapeGenerator) {
this.canvas = canvas;
this.shapeGenerator = shapeGenerator;
}
public void actionPerformed(ActionEvent event) {
this.canvas.setShapeGenerator(shapeGenerator);
}
}
Here is the class CircleShapeGenerator
public class CircleShapeGenerator implements ShapeGenerator {
public Shape getGeneratedShape(int x, int y) {
return new Ellipse2D.Float(x, y, 10, 10);
}
}
and the interface ShapeGenerator
public interface ShapeGenerator {
Shape getGeneratedShape(int x, int y);
}
and, if you want to add the action listener on the rectangle button, it's really easy now. create this class:
public class RectangleShapeGenerator implements ShapeGenerator {
public Shape getGeneratedShape(int x, int y) {
return new Rectangle2D.Float(x, y, 10, 10);
}
}
and in your application class, add the following code
toolBar.add(bRectangle, toolBar.getComponentCount() - 1);
bRectangle.addActionListerner(pan, new RectangleShapeGenerator() )
for your method paint in your DrawingCanvas, i think, you should use a code like this :
for (Shape s : shapes) {
Graphics2D g2 = (Graphics2D) g;
g2.draw(s);
}
BR,
Patouche
Sorry for my english, i know it's not really perfect...
First of all, remove your class Shape. You don't need to. Shape is a interface defined by Java. Use this interface instead.
Your setter for ShapeGenerator in the DrawingCanvas class do nothing. So add it. Your method paint is not correct.
public class DrawingCanvas extends JPanel implements MouseListener {
// Shape here is the java.awt.Shape from java.
private ArrayList<Shape> shapes = new ArrayList<>();
private ShapeGenerator shapeGenerator;
...
...
public void paint(Graphics g){
super.paint(g);
for (Shape s : shapes) {
Graphics2D g2 = (Graphics2D) g;
g2.draw(s);
}
}
...
#Override
public void mousePressed(MouseEvent e) {
shapes.add(this.shapeGenerator.getGeneratedShape(e.getX(), e.getY()));
repaint();
}
public void setShapeGenerator(ShapeGenerator shapeGenerator) {
this.shapeGenerator = shapeGenerator;
}
}
You class Circle is really to complex. It's simple and this method don't need any paint method. You class Circle is really simple :
public class Circle implements ShapeGenerator{
public Shape getGeneratedShape(int x, int y) {
// This will return a java.awt.Shape instance
return new Ellipse2D.Float(x, y, 10, 10);
}
}
And i thinks, that's all.
Here is the solution to your problem. But it's really dirty... Like i said, there is no sense to have a Shape that is inhereted from JPanel !!
public class Application extends JFrame implements ActionListener {
{
// Set appearance Look & Feel for the window
try {
javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}
private final DrawingCanvas pan = new DrawingCanvas();
// don't use to many field, it's confusing and not necessary.
// you can do local var instead.
private final JPanel container = new JPanel();
private final JPanel commandes = new JPanel();
private final JMenuBar menu = new JMenuBar();
private final JMenu newFile = new JMenu();
private final JMenu open = new JMenu();
private final JMenu save = new JMenu();
private JFileChooser fileChooser;
private final JToolBar toolBar = new JToolBar();
private final JButton bSquare = new JButton("Square");
private final JButton bRectangle = new JButton("Rectangle");
private final JButton bCircle = new JButton("Circle");
private final JButton bTriangle = new JButton("Triangle");
private final JButton bErase = new JButton("CLEAR");
public Application() {
this.setTitle("Animation");
this.setSize(579, 432);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
container.setBackground(Color.white);
container.setLayout(new BorderLayout());
container.add(pan, BorderLayout.CENTER);
this.setContentPane(container);
toolBar.add(Box.createHorizontalGlue());
toolBar.add(Box.createVerticalGlue());
toolBar.add(bSquare, toolBar.getComponentCount() - 1);
toolBar.add(bCircle, toolBar.getComponentCount() - 1);
bCircle.addActionListener(new SampleActionListener(pan, new CircleShapeGenerator()));
toolBar.add(bRectangle, toolBar.getComponentCount() - 1);
// bRectangle.addActionListener(new SampleActionListener(pan, new
// RectangleShapeGenerator()));
toolBar.add(bTriangle, toolBar.getComponentCount() - 1);
toolBar.add(bErase, toolBar.getComponentCount() - 1);
toolBar.setOrientation(SwingConstants.VERTICAL);
container.add(toolBar, BorderLayout.WEST);
menu.add(newFile);
newFile.setIcon(new ImageIcon("src/images/new.png"));
menu.add(open);
open.setIcon(new ImageIcon("src/images/open.png"));
menu.add(save);
save.setIcon(new ImageIcon("src/images/save.png"));
this.setJMenuBar(menu);
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Application App = new Application();
}
}
We keep the CircleShapeGenerator class
public class CircleShapeGenerator implements ShapeGenerator {
#Override
public CircleShape getGeneratedShape(int x, int y) {
CircleShape c = new CircleShape();
c.setCanvasX(x);
c.setCanvasY(y);
return c;
}
}
The circle shape
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
public class CircleShape extends Shape {
private final Ellipse2D.Float ellipse2D;
public CircleShape() {
this.ellipse2D = new Ellipse2D.Float();
this.setRadius(20);
}
private void setRadius(int r) {
this.ellipse2D.height = r;
this.ellipse2D.width = r;
}
#Override
public void setCanvasX(int canvasX) {
this.canvasX = canvasX;
this.ellipse2D.x = canvasX;
}
#Override
public void setCanvasY(int canvasY) {
this.canvasY = canvasY;
this.ellipse2D.y = canvasY;
}
#Override
public java.awt.Shape getTrueShape() {
return this.ellipse2D;
}
}
A class for the contextual menu to edit your shape
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
class ContextualShapeMenu extends JPopupMenu {
private final Shape shape;
public ContextualShapeMenu(Shape shape) {
this.shape = shape;
JMenuItem anItem = new JMenuItem("Edit the size");
add(anItem);
}
}
The DrawingCanvas class
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
import com.google.common.collect.Lists;
public class DrawingCanvas extends JPanel implements MouseListener {
protected ArrayList<Shape> shapes = new ArrayList<Shape>();
private ShapeGenerator shapeGenerator;
public ArrayList<Shape> getShapes() {
return shapes;
}
public void setShapes(ArrayList<Shape> shapes) {
this.shapes = shapes;
}
public DrawingCanvas() {
super();
addMouseListener(this);
}
#Override
public void paint(Graphics g) {
super.paint(g);
for (Shape s : shapes) {
s.paint(g);
}
}
#Override
public void mouseClicked(MouseEvent e) {
switch (e.getButton()) {
case MouseEvent.BUTTON1:
shapes.add(this.shapeGenerator.getGeneratedShape(e.getX(), e.getY()));
break;
default:
Shape shape = getShapeUnderMouse(e);
if (shape != null) {
ContextualShapeMenu menu = new ContextualShapeMenu(shape);
menu.show(e.getComponent(), e.getX(), e.getY());
}
}
repaint();
}
private Shape getShapeUnderMouse(MouseEvent e) {
List<Shape> reversed = Lists.reverse(this.shapes);
for (Shape s : reversed) {
if (s.contains(e.getX(), e.getY())) {
return s;
}
}
return null;
}
#Override
public void mouseEntered(MouseEvent e) {
// Do nothing
}
#Override
public void mouseExited(MouseEvent e) {
// Do nothing
}
#Override
public void mousePressed(MouseEvent e) {
// Do nothing
}
#Override
public void mouseReleased(MouseEvent e) {
// Do nothing
}
public void setShapeGenerator(ShapeGenerator shapeGenerator) {
this.shapeGenerator = shapeGenerator;
}
}
Still the ActionListener. Rename it.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SampleActionListener implements ActionListener {
private final DrawingCanvas canvas;
private final ShapeGenerator shapeGenerator;
public SampleActionListener(DrawingCanvas canvas, ShapeGenerator shapeGenerator) {
this.canvas = canvas;
this.shapeGenerator = shapeGenerator;
}
#Override
public void actionPerformed(ActionEvent event) {
this.canvas.setShapeGenerator(shapeGenerator);
}
}
The ShapeGenerator interface
public interface ShapeGenerator {
Shape getGeneratedShape(int x, int y);
}
And at least, the most stupid class : Shape
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
public abstract class Shape extends JPanel implements java.awt.Shape {
protected int canvasX;
protected int canvasY;
public int getCanvasX() {
return canvasX;
}
public abstract java.awt.Shape getTrueShape();
public abstract void setCanvasX(int canvasX);
public int getCanvasY() {
return canvasY;
}
public abstract void setCanvasY(int canvasY);
#Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.draw(this.getTrueShape());
}
#Override
public Rectangle getBounds() {
return getTrueShape().getBounds();
}
#Override
public Rectangle2D getBounds2D() {
return getTrueShape().getBounds2D();
}
#Override
public boolean contains(int x, int y) {
return this.getTrueShape().contains(x, y);
}
#Override
public boolean contains(double x, double y) {
return this.getTrueShape().contains(x, y);
}
#Override
public boolean contains(Point2D p) {
return this.getTrueShape().contains(p);
}
#Override
public boolean contains(Point p) {
return this.getTrueShape().contains(p);
}
#Override
public boolean intersects(double x, double y, double w, double h) {
return this.getTrueShape().intersects(x, y, w, h);
}
#Override
public boolean intersects(Rectangle2D r) {
return this.getTrueShape().intersects(r);
}
#Override
public boolean contains(double x, double y, double w, double h) {
return this.getTrueShape().contains(x, y, w, h);
}
#Override
public boolean contains(Rectangle2D r) {
return this.getTrueShape().contains(r);
}
#Override
public PathIterator getPathIterator(AffineTransform at) {
return this.getTrueShape().getPathIterator(at);
}
#Override
public PathIterator getPathIterator(AffineTransform at, double flatness) {
return this.getTrueShape().getPathIterator(at, flatness);
}
}
I have add the guava librairy, so, you will not find the Lists class. Add it to your project, use a other librairy or create a method to reverse your list.
Good luck and don't make something like that in the future !!! It's coding horror.
By the way, i came from France.

Categories