Move a rectangle down using Graphics - java

I'm newbie in Java and I want to move a rectangle down in a JFrame. I want to see this movement.
Why can't I see the rectangle moving down? Do I have to use other library or what?
import java.awt.Color;
import java.awt.Graphics;
import java.lang.Thread;
import javax.swing.JComponent;
public class Draw extends JComponent {
public void paintComponent(Graphics g) {
g.setColor(new Color(0, 128, 128, 128));
try {
for(int i = 70; i < 100; i++) {
g.fillRect(40, i, 100, 70);
Thread.sleep(10); // To see the moviment.
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
import javax.swing.JFrame;
public class Main extends JFrame {
public Main() {
setSize(300, 200);
Draw draw = new Draw();
add(draw);
}
public static void main(String[] args) {
Main m = new Main();
m.setVisible(true);
}
}

Swing is single threaded
This means that if you perform any long running or blocking operations within the context of the Event Dispatching Thread, you will prevent Swing from performing any painting operations or processing using input
Swing is not thread safe
This means that you should never try and update the UI, or any state the UI relies on, from outside the context of the Event Dispatching Thread, this can run you into all kinds of weirdness and unpredictable states
See Concurrency in Swing for more details
A simple solution...
Possibly the simplest solution is to use a Swing Timer, for example...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
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 Draw());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class Draw extends JPanel {
private int yPos = 0;
public Draw() {
Timer timer = new Timer(10, new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
yPos += 1;
repaint();
}
});
timer.start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(new Color(0, 128, 128, 128));
g2d.fillRect(40, yPos, 100, 70);
g2d.dispose();
}
}
}
See How to Use Swing Timers for more details
Why use a JPanel over a JComponent?
For simplicity. A JPanel will paint it's background (using the background property) automatically (if you call super.paintComponent) saving your the hassle

Your "time" consideration is not following Swing UI threading concept.
Think of it this way: Swing has a Frame, you paint the content, then wait for something to change, and paint again.
A suggestion could be a Thread, that every x milliseconds, requests the JFrame to update its content and then by using repaint() method.
An example:
Draw() {
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// Move x and y of the desired rectangle and request repaint
x+=10;
repaint();
}}, 50);
}

Related

Implementing a timer in java

import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.*;
import javax.swing.*;
import java.util.Timer;
import java.awt.event.*;
import java.awt.event.*;
import javax.swing.*;
class autos extends JLabel
{
#SuppressWarnings("serial")
int z=100,i=50;
public static void main(String[] args)
{
JFrame frame=new JFrame("Rectangle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(1000,1000);
frame.add(new autos());
}
#Override
public void paintComponent( Graphics g )
{
for(i=1;i<=7;i++)
{
g.drawRect(z,100,100,100);
z=z+120;
//timer delay
}
}
}
Hello,I'm trying to create a program in java that draws mre rectangles one after another with a delay(not all of them at once).
Since sleep and TimeUnit will freeze the paintComponent, I'm a bit clueless.I tried to use a timer to make a delay, but I failed.I cannot understand how to use the timer in this case.
How do I make a time delay between the rectangles?
You should start by taking a look at How to use Swing Timers and Concurrency in Swing
You have to think of a Swing Timer as pseudo loop, which, every time it ticks, you update some value which, when your call repaint and your paintComponent method is called, you can update the UI appropriately
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
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 int count = 0;
public TestPane() {
Timer timer = new Timer(500, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
count++;
if (count == 7) {
((Timer)e.getSource()).stop();
}
repaint();
}
});
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
timer.stop();
count = 0;
timer.start();
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension((120 * 7) + 100, 300);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
super.paintComponent(g);
int x = 100;
for (int rect = 1; rect <= count; rect++) {
g2d.drawRect(x, 100, 100, 100);
x += 120;
//timer delay
}
g2d.dispose();
}
}
}
ps- Click the panel to get the timer to start
You don't need a for loop to do this job.You can use a swing Timer like this:
int delay=500;// delay time in ms
Timer t=new Timer(delay,new ActionListener() {
#Override
public void actionPerformed(ActionEvent e){
autos.this.getGraphics().drawRect(z,100,100,100); // draw rectangle in your panel's graphics
repaint(); // this line calls paint component to show changes
i++;
z+=120;
if (i>70) t.stop(); // for loop's condition
}
});

Thread.sleep() delaying entire program instead of only what's after it

Pretty much title. The code is supposed to draw one box, wait 1 second, then draw a new one at a different location and repaint. Instead, it will wait for 1 second then paint both boxes. Thanks for the help and sorry if I messed up on formatting.
import javax.swing.*;
import java.awt.*;
public class GameRunner extends JPanel{
#Override
public void paintComponent (Graphics g){
int x = 0;
boolean directionRight = true;
g.setColor(Color.blue);
g.fillRect(300,400,100,100);
repaint();
try{
Thread.sleep(1000);
}
catch (Exception ex){}
g.fillRect(600,400,100,100);
repaint();
}
public static void main (String[] args){
JFrame frame = new JFrame("Submarine");
GameRunner gameRunner = new GameRunner();
frame.add(gameRunner);
frame.setSize(1200,700);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
Thread.sleep(1000); will block the current running thread
paintComponent is called from within the context of the Event Dispatching Thread.
Swing won't update the state of the UI until it's finished processing the current (in this case "paint") event, meaning that while it's blocked at Thread.sleep, nothing will be updated on the UI and no new events will be processed.
Swing is a single threaded framework. You should never perform any blocking or long running operations from within the context of the Event Dispatching Thread.
Have a look at Concurrency in Swing for more details and How to use Swing Timers for a possible solution.
As a side note, you should NEVER modify the state if the UI or any variable the UI relies on from within any paint method. Painting should only paint the current state of the component, never modify it, this includes calling repaint directly or indirectly
For example...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class GameRunner extends JPanel {
private int xPos = 300;
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.blue);
g.fillRect(xPos, 400, 100, 100);
repaint();
}
public GameRunner() {
Timer timer = new Timer(1000, new ActionListener() {
private boolean state = false;
#Override
public void actionPerformed(ActionEvent e) {
if (state) {
xPos = 300;
} else {
xPos = 600;
}
state = !state;
repaint();
}
});
timer.start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(700, 500);
}
public static void main(String[] args) {
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 GameRunner());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}

How do I fire MouseMotionListener events in real-time?

I am trying to create a graphics drawing program that allows the user to draw red pixels on the screen by dragging their mouse over it. So in a way, you can think of this program as Microsoft's Paint program but with only the pencil drawing tool and color red.
Unfortunately the mouseDragged() function in my program is not working properly. It will skip some of the pixels on the screen if I move my mouse too fast, like this:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameView extends JFrame {
JPanel panel;
Graphics2D drawingContext;
public static void main(String[] args) {
new FrameView();
}
public FrameView() {
panel = new JPanel();
panel.addMouseMotionListener(new MouseControls());
panel.setBackground(Color.WHITE);
this.add(panel);
this.setSize(new Dimension(500, 500));
this.setTitle("Drawing Program");
this.setVisible(true);
drawingContext = (Graphics2D)panel.getGraphics();
}
private class MouseControls extends MouseAdapter {
#Override
public void mouseDragged(MouseEvent e) {
int x = e.getX();
int y = e.getY();
final int WIDTH = 1;
final int HEIGHT = 1;
Shape pixel = new Rectangle(x, y, WIDTH, HEIGHT);
drawingContext.setColor(Color.RED);
drawingContext.draw(pixel);
}
}
}
getGraphics is NOT how painting works in Swing, instead, you should be overriding the paintComponent method of the component and performing your custom painting there.
Painting is destructive, it is expected that when ever paintComponent is called, you will completely repaint the current state of the component.
Have a look at Painting in AWT and Swing and Performing Custom Painting for more details
As to you "mouse" problem, this is actually how it works, you won't be notified of EVERY pixel position the mouse has to pass through, your mouse would lag horribly across the screen if it did. Instead, the OS moves the mouse in ever increasing steps based on the speed of the movement of the user input.
Instead of drawing just the points, draw lines between them, for example
nb: I've deliberately painted the points larger so you can see where they are been reported, you will see that all the dots (for a single drag) are connected
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
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 List<List<Point>> points = new ArrayList<>(25);
private List<Point> activeList;
public TestPane() {
MouseAdapter ma = new MouseAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
if (activeList != null) {
activeList.add(e.getPoint());
repaint();
}
}
#Override
public void mousePressed(MouseEvent e) {
activeList = new ArrayList<>(25);
points.add(activeList);
}
#Override
public void mouseReleased(MouseEvent e) {
if (activeList != null && activeList.isEmpty()) {
points.remove(activeList);
}
activeList = null;
}
};
addMouseMotionListener(ma);
addMouseListener(ma);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
for (List<Point> group : points) {
Point previous = null;
for (Point p : group) {
// You can get rid of this, it's simply to show
// where the points would actually be rendered
g2d.fill(new Ellipse2D.Float(p.x - 2, p.y - 2, 4, 4));
if (previous != null) {
g2d.draw(new Line2D.Float(previous, p));
}
previous = p;
}
}
g2d.dispose();
}
}
}

Java Isn't Displaying gui right

I have a simple gui and when I run is it is perfect, but occasionally it doesn't display right. I have a circle in a JPanel. When it runs right it displays in the middle for the JFrame like I intended it to, but when it does not work right the circle appears closer to the bottom of the screen. How can I fix this so it displays right each time? Is my code incorrect (I hope not! :) ), or is it some bug in java. So here is my code:
Update: It seems that the window height is changing.
Run.java--------------------------------------------------------------------------------
import javax.swing.JFrame;
public class Run {
public static void main(String args[]) {
Window w = new Window();
w.setSize(800, 500);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.setVisible(true);
}
}
Window.java----------------------------------------------------------------------------
import javax.swing.JFrame;
public class Window extends JFrame {
public Window() {
super("Wheel");
Gui g = new Gui();
add(g);
}
}
Gui.java--------------------------------------------------------------------------------
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Gui extends JPanel {
private Color wheelColor = new Color(0, 0, 255);
public Gui() {
setOpaque(true);
setBackground(new Color(255, 0, 0));
}
public void paintComponent(Graphics g) {
g.setColor(wheelColor);
g.fillOval(40, 40, 420, 420);
}
}
Also I'm using Ubuntu, I don't know if that would affect it at all. Thanks in advance.
Make sure you create the UI within the context of the Event Dispatching Thread, see Initial Threads for more details.
Make sure you are calling super.paintComponent within the paintComponent method before you do any custom painting, see Painting in AWT and Swing for more details
Don't rely on magic numbers, the size of the window is the size of it's content + its frame decorations. You should get using getWidth and getHeight to determine the size of the Gui panel and overriding getPreferredSize to allow the window to determine how much room it needs to (optimally) display it
For example...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Run {
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
Window w = new Window();
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.pack();
w.setLocationRelativeTo(null);
w.setVisible(true);
}
});
}
public static class Window extends JFrame {
public Window() {
super("Wheel");
Gui g = new Gui();
add(g);
}
}
public static class Gui extends JPanel {
private Color wheelColor = new Color(0, 0, 255);
public Gui() {
setOpaque(true);
setBackground(new Color(255, 0, 0));
}
#Override
public Dimension getPreferredSize() {
return new Dimension(440, 440);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(wheelColor);
int width = getWidth();
int height = getHeight();
int x = (width - 420) / 2;
int y = (height - 420) / 2;
g.fillOval(x, y, 420, 420);
}
}
}
Also beware of naming your classes after classes that already exist within the default API, java.awt.Window already exists and can cause confusion not only for yourself, but for other developers ;)

Calling paintComponent() with parameters using repaint()

I am very new to graphics with Java, so just ask if any additional information is needed :)
I am trying to paint shapes based on where the mouse clicks on the screen. Because of this, I need to pass the x and y coordinates of where I clicked to the paintComponent() method so that it will know where to paint the shape.
public void mouseClicked(MouseEvent e) {
System.out.println("Adding Shape");
repaint();
}
class CanvasDrawArea extends JPanel{
//this should run when the program first starts
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
canvas.setBackground(CANVAS_COLOR);
}
//here is where the question is
public void paintComponent(Graphics g, int x, int y){
super.paintComponent(g);
g.fillRect(x, y, RECTANGLE_WIDTH, RECTANGLE_HEIGHT);
}
}
basically I am trying to overload the paintComponent by having one that runs right when the program starts by calling the repaint() / pack() method, and one that will run when I give it the x and y coordinates. I am unsure, however, how I am supposed to go about passing the x and y parameters, as there is no way to pass them in the repaint() method.
You should never have the need to call paintComponent or paint directly these are called automatically by the repaint manager when required...
Create java.util.List and store the Point of each mouse click in it, call repaint from the mouseClicked method after you have done this.
In your paintComponent(Graphics) method, iterate over the List of points and paint the shape you need.
As a simple example...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Dotty {
public static void main(String[] args) {
new Dotty();
}
public Dotty() {
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 DottyPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class DottyPane extends JPanel {
private List<Point> points;
public DottyPane() {
points = new ArrayList<>(25);
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
points.add(e.getPoint());
repaint();
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
for (Point p : points) {
g2d.fillOval(p.x - 5, p.y - 5, 10, 10);
}
g2d.dispose();
}
}
}

Categories

Resources