Java MouseListener graphics implementation - java

So I am trying to implement a mouse listener into my program, I got the mouseListener to work but not the graphics. I am trying to find a way to draw a blue circle every time the mouse is clicked on the JPANEL, the only problem is I can not seem to get a good call for the Graphics (that I have tried to name g).
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
class moveItMon extends JPanel implements MouseListener{
public moveItMon() {
this.setPreferredSize(new Dimension(500, 500));
addMouseListener(this);
}
public void addNotify() {
super.addNotify();
requestFocus();
}
public void mouseClicked(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e) {
movetehMon(e);
}
public void movetehMon(MouseEvent e){
int x = e.getX();
int y = e.getY();
System.out.println("(" + x + "," + y + ")");
paintMon(x,y);
}
public void paintMon( int x, int y){
Graphics g = new Graphics();
g.setColor(Color.WHITE);
g.clearRect(0,0,500,500);
g.setColor(Color.BLUE);
g.fillOval(x,y,20,20);
}
public static void main(String[] s) {
JFrame f = new JFrame("moveItMon");
f.getContentPane().add(new moveItMon());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}

Graphics g = new Graphics(); isn't going to work (as I'm sure you're aware) as the class is abstract.
Custom painting in Swing is done by overriding the paintComponent of a component that extends from JComponent (like JPanel) and using the supplied Graphics context to paint to.
Take a look at Performing Custom Painting and Painting in AWT and Swing for more details
You should also beware that painting is a destructive process, meaning that each time paintComponent is called, you are expected to update everything that you need painted.

Related

Can't get paint function to update in simple pong game

I'm a relatively inexperienced Java programmer when it comes to graphics. I'm trying to make a simple pong game to learn more about how graphics are done in Java.
In the program, the left paddle (the only paddle thus far implemented) should move up 5 pixels when 'W' is pressed on the keyboard. According to the console, it is recognizing that the key is being pressed, and it is updating a variable that represents the y-coordinate for the left paddle accordingly. However, the paddle's location isn't actually being updated on the screen.
Help?
Game class:
import javax.swing.JFrame;
public class Game {
static int WIDTH = 500;
static int HEIGHT = 500;
public static void main(String[] args) {
window();
}
public static void window() {
JFrame frame = new JFrame();
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null);
frame.setAlwaysOnTop(true);
frame.setVisible(true);
frame.setResizable(false);
frame.add(new Panel());
frame.addKeyListener(new Panel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Panel class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
public class Panel extends JPanel implements KeyListener {
int leftPaddlePos = 100;
public void paint(Graphics g) {
g.clearRect(0, 0, Game.WIDTH, Game.HEIGHT);
g.setColor(Color.black);
g.fillRect(0, 0, Game.WIDTH, Game.HEIGHT);
g.setColor(Color.white);
g.fillRect(75, leftPaddlePos, 15, 100);
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_W) {
System.out.println("W pressed");
leftPaddlePos = leftPaddlePos + 5;
System.out.println(leftPaddlePos);
}
}
#Override
public void keyReleased(KeyEvent e) {
}
}
public class Panel extends JPanel implements KeyListener {
Don't call your class Panel. There is an AWT class with that name so it is confusing. Your class name should be more descriptive. Maybe something like PongPanel.
public void paint(Graphics g) {
Don't override paint(...);
public void paintComponent(Graphics g) {
Custom painting in Swing is done by overriding paintComponent(...).
g.clearRect(0, 0, Game.WIDTH, Game.HEIGHT);
g.setColor(Color.black);
g.fillRect(0, 0, Game.WIDTH, Game.HEIGHT);
Don't use above code to paint the background of the panel. Instead, you just use:
super.paintComponent(g);
So in the constructor of your class you can invoke setBackground(...) to set the desired background color.
However, the paddle's location isn't actually being updated on the screen.
leftPaddlePos = leftPaddlePos + 5;
You update the position, but you haven't told the component that a property of the class has changed, so it doesn't know it needs to paint itself again.
You should not change the property of the class in the listener. Instead you should have a method like adjustLeftPaddlePosition(int value)
The method would be:
public void adjustLeftPaddlePostion(int value)
{
leftPaddlePos = leftPaddlePos + value;
repaint();
}
Then in the KeyListener you would use:
adjustLeftPaddlePosition(5);
Also, you should not be using a KeyListener. Instead you should be using "Key Bindings" Check out Motion Using the Keyboard for more information and working examples.

Animations in Java not Working

I am trying to animate a circle in Java. I want it to move every time I press a key, but it is not working. Is there a problem with the way I am drawing the circle? Am I forgetting a repaint() somewhere.
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel implements KeyListener {
int x = 300, y = 300;
public Main() {
super();
}
public void paintComponent(Graphics g) {
g.drawOval(x, y, 300, 300);
}
#Override
public void keyPressed(KeyEvent e) {
x++;
y++;
repaint();
}
#Override
public void keyTyped(KeyEvent e) {}
#Override
public void keyReleased(KeyEvent e) {}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(1200, 800);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.add(new Main());
f.setVisible(true);
}
}
Don't use KeyListener, honestly, make use of the Key Bindings API instead, which has been designed to resolve the issues which KeyListener creates.
paintComponent in JPanel does an important job, you are expected to call it's super method before doing any custom painting.
See Painting in AWT and Swing and Performing Custom Painting for more details
You should also make an effort to initialise you UI from within the context of the Event Dispatching Thread, this solves a number of known issues on some platforms, see Initial Threads for more details
You are not very far off. First, you need to actually add a listener for your key events.
f.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
m.keyPressed(e);
}
});
Second, you'll notice this uses 'm'. That is a reference to your Main object.
Change:
f.add(new Main());
to:
Main m = new Main();
f.add(m);
Now it should work!

Affecting Boolean Variables with Mouse Events Using Swing

I am not sure why the mouse events set by my mouse listener are not affecting whether or not the hat is drawn. The variable "mouseInside" seems not to be affected by the mouse events. How do I change the variable to false with the mouse events?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
public class EyesOpen extends JPanel{
private Ellipse2D.Double head;
private Ellipse2D.Double eyeOne;
private Ellipse2D.Double eyeTwo;
private Rectangle2D.Double hatBody;
boolean mouseInside;
public EyesOpen(){
this.setFocusable(true);
this.requestFocus();
this.setPreferredSize(new Dimension(500,500));
head = new Ellipse2D.Double(180,180,140,140);
eyeOne = new Ellipse2D.Double(220,220,20,20);
eyeTwo = new Ellipse2D.Double(260,220,20,20);
hatBody = new Rectangle2D.Double(170,180,160,20);
class MyMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e){
mouseInside=true;
}
public void mouseExited(MouseEvent e){
mouseInside=false;
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
}
this.addMouseListener(new MyMouseListener());
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(Color.BLUE);
g2.fill(head);
g2.setPaint(Color.BLACK);
g2.fill(eyeOne);
g2.setPaint(Color.BLACK);
g2.fill(eyeTwo);
if(mouseInside=true){
g2.setPaint(Color.BLACK);
g2.fill(hatBody);}
}
public static void main(String[] args){
JFrame f = new JFrame("Head");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLayout(new FlowLayout());
f.add(new EyesOpen());
f.pack();
f.setVisible(true);
}
}
Don't forget to call repaint() after changing the boolean.
To be precise, you could specify that it is the drawing component's method that gets called via EyesOpen.this.repaint();, but this isn't absolutely necessary in this current program (I don't think).
Other nitpicks:
The paintComponent(...) method should be specified as protected not public. No sense in exposing it any more than it needs to be exposed.
Don't forget to use the #Override annotation any time that you think that you're overriding a method. While it may not matter with this program, it will save your behind in the future.
You will want to start your GUI on the Swing thread in your main method by placing your JFrame creation code inside of a Runnable and then queuing that Runnable onto the event queue by placing it into a SwingUtilities.invokeLater(/** Your Runnable Goes Here **/);

Java: Using graphics component within an ActionListener

I have two separate class and driver files, and in the class file I create the paint method:
public void paint(Graphics g){
g.drawLine(......
....
//along with all of my other draw commands
}
Further down in the code, I create a JButton and within this button's action listener I don't know how to use a Graphics object to create more graphics in the JFrame. Should I be adding something to my driver to make this happen, or is there a way to use these graphics within my action listener? Thank you, and any help is appreciated.
You need to draw everything within the paint method. The actionPerformed should only change the state of something already in the paint method, and then call repaint. For example
boolean drawHello = true;
boolean drawWorld = false;
protected void paintComponent(Graphics g) {
super.paintCompoent(g);
if (drawHello)
g.drawString("Hello", 50, 50);
if (drawWorld)
g.drawString("World", 10, 10);
}
Then in your actionPerformed, you can change the state of drawWorld to true and call repaint().
public void actionPerformed(ActionEvent e) {
drawWorld = true;
repaint();
}
So as you can see, everything should be drawn in the paintComponent method. You can just hide and paint renderings, and make them "visible" from a action command. You should already have predefined what could posibly be drawn. Then just change the state of it rendering
And as #MadPrgrammer pointed out, you should not be painting on top-level containers like JFrame. Instead paint on a custom JPanel or JComponent and override the paintComponent method, instead of JFrame and paint
Here's an example where I draw a new square every time the button is pressed. If look at the code, you will see that in the paintComponent method, I loop through a list of Squares and draw them, and in the actionPerformed all I do is add a new Square to the List and call repaint()
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class AddSquares {
private int R = 0;
private int G = 0;
private int B = 0;
private int xLoc = 0;
private int yLoc = 0;
List<Square> squares = new ArrayList<>();
private JButton addSquare = new JButton("Add Square");
private RectsPanel panel = new RectsPanel();
public AddSquares() {
addSquare.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
Color color = new Color(R, G, B);
squares.add(new Square(xLoc, yLoc, color));
panel.repaint();
R += 10;
G += 20;
B += 30;
xLoc += 20;
yLoc += 20;
}
});
JFrame frame = new JFrame("Draw Squares");
frame.add(panel, BorderLayout.CENTER);
frame.add(addSquare, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
private class RectsPanel extends JPanel {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Square square : squares) {
square.drawSquare(g);
}
}
#Override
public Dimension getPreferredSize() {
return new Dimension(250, 250);
}
}
private class Square {
int x = 0;
int y = 0;
Color color;
public Square(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}
public void drawSquare(Graphics g) {
g.setColor(color);
g.fillRect(x, y, 75 ,75);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
AddSquares addSquares = new AddSquares();
}
});
}
}
It's difficult to be 100%, but it would seem as you don't understand how custom painting is performed in Swing.
Start by taking a look at Performing Custom Painting and Painting in AWT and Swing.
Essentially, painting is arranged by the Repaint Manager, which decides what and when something should be painted. It then calls (through a chain of methods) the paint method of the components it thinks need to be updated, passing it a reference to a Graphics context that should be used to actually paint on.
Basically, when ever your paint method is called, you should create paint the current state of your painting.
You should avoid overriding paint and instead use paintComponent from classes the extend JComponent
Your question is a little on the vague side as to what you are actually wondering about but generally speaking:
We don't override paint in Swing, we override paintComponent.
If you are already aware of this, you may be overriding paint because you are doing it on a JFrame and you found that JFrame does not have a paintComponent method. You shouldn't override paint on a JFrame. Instead, create a JPanel or something to put inside the frame and override paintComponent on the panel.
Question about the ActionListener.
It sounds like you are wanting to do painting outside of paintComponent in which case probably the best way is to do painting to a separate Image. Then you paint the Image on to the panel in paintComponent. You can also put an Image in a JLabel as an ImageIcon. Here is a very simple drawing program using MouseListener that demonstrates this (taken from here):
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
class PaintAnyTime {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new PaintAnyTime();
}
});
}
final BufferedImage image = (
new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB)
);
final JFrame frame = new JFrame();
final JLabel label = new JLabel(new ImageIcon(image));
final MouseAdapter drawer = new MouseAdapter() {
Graphics2D g2D;
#Override
public void mousePressed(MouseEvent me) {
g2D = image.createGraphics();
g2D.setColor(Color.BLACK);
}
#Override
public void mouseDragged(MouseEvent me) {
g2D.fillRect(me.getX(), me.getY(), 3, 3);
label.repaint();
}
#Override
public void mouseReleased(MouseEvent me) {
g2D.dispose();
g2D = null;
}
};
PaintAnyTime() {
label.setPreferredSize(
new Dimension(image.getWidth(), image.getHeight())
);
label.addMouseListener(drawer);
label.addMouseMotionListener(drawer);
frame.add(label);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
#MadProgrammer has already linked to the articles that I was going to link to.

repaint() method in java not working properly

am doing that program to paint the mouse location in a panel , the program works fine but after like 10 seconds it stops painting the points... any help?
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JFrame;
public class Draw extends JPanel {
public static int newx;
public static int newy;
public void paint(Graphics g) {
Mouse mouse = new Mouse();
mouse.start();
int newx = mouse.x;
int newy = mouse.y;
g.setColor(Color.blue);
g.drawLine(newx, newy, newx, newy);
repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.white);
frame.setSize(2000,2000 );
frame.setVisible(true);
frame.getContentPane().add(new Draw());
frame.revalidate();
frame.getContentPane().repaint();
}
}
public void paint(Graphics g) should be public void paintComponent(Graphics g).
And you isn't supposed to call repaint() inside this method.
You should add an mouse listener outside this method, too.
An adapted example from Java Tutorials
public class MouseMotionEventDemo extends JPanel
implements MouseMotionListener {
//...in initialization code:
//Register for mouse events on blankArea and panel.
blankArea.addMouseMotionListener(this);
addMouseMotionListener(this);
...
}
public void mouseMoved(MouseEvent e) {
Point point = e.getPoint();
updatePanel(point); //create this method to call repaint() on JPanel.
}
public void mouseDragged(MouseEvent e) {
}
}
}
You call repaint within the paint method, causing an infinite loop. Swing Timers are preferred for running periodic updates on components.
For custom painting in Swing the method paintComponent rather than paint should be overridden not forgetting to call super.paintComponent.

Categories