Why is only one rectangle being painted here even though I call the repaint() method multiple times in my for() loop? Additionally I want to display 8 rectangles per row and 8 rectangles per column with 2 pixels space in between. Any ideas or help?
package PROG2;
import java.awt.*;
import java.util.*;
import javax.swing.*;
class Model {
int m_width;
int m_height;
int m_x1 = 50;
int m_y1 = 50; //Information about the model
int m_x2 = 100;
int m_y2 = 30;
int counter = 0; //Assisting variable
Model(int width,int height) {
m_width = width;
m_height = height;
}
void math() {
counter = counter + 2;
counter = counter + m_x2;
m_x1 = counter;
}
}
class View extends JComponent {
private Model m_Mod;
View(Model mod) {
m_Mod = mod;
}
#Override
protected void paintComponent(Graphics g) {
//super.paintComponent(g);
g.setColor(Color.green);
g.drawRect(m_Mod.m_x1,m_Mod.m_y1,
m_Mod.m_x2,m_Mod.m_y2);
g.fillRect(m_Mod.m_x1,m_Mod.m_y1,
m_Mod.m_x2,m_Mod.m_y2);
}
}
class Controller {
private Model m_Mod;
private View m_View;
Controller(){
m_Mod = new Model(500,500);
m_View = new View(m_Mod);
JFrame frame = new JFrame();
frame.add(m_View);
frame.setSize(m_Mod.m_width,m_Mod.m_height);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Graphics g = frame.getGraphics();
}
void simulate(int right, int down){
for( int i = 0; i < right; i++) {
m_Mod.math();
m_View.repaint();
}
}
}
class Übung2{
public static void main(String[] args) throws Exception {
Controller c = new Controller();
c.simulate(8, 8);
}
}
Even when you call repaint() multiple times, the actual drawing will take place only once at a later time (until swing will trigger a repaint again). At that time, the content of the paintComponent() method is executed. The code you have in your method only draws one rectangle (drawRect()) and one filled rectangle (fillRect()) and that's it, nothing more. So you don't get more than what you wrote in your method.
You can use arrays (or lists) to store the multiple XY coordinates of your rectangles and then iterate over the array/list to draw multiple rectangles while inside the single call of paintComponent(). The code can look like this:
#Override
protected void paintComponent(Graphics g) {
//super.paintComponent(g);
g.setColor(Color.green);
foreach (RectModel rect: m_Mod.getRectangles()) {
g.drawRect(rect.x1, rect.y1, rect.x2, rect.y2);
}
}
This assumes you have such a class RectModel which holds the data of one rectangle (x1, y1, x2, y2) and that you have a method m_Mod.getRectangles() which returns an array/list of RectModel instances. When you execute the simulate() method in your controller, you will calculate all the rectangles you want to draw and save them in the this.rectangles array/list of the Model class. After that the paintComponent() method fill use that array/list and draws the rectangles.
Related
I'm trying to draw squares to make grid system to make snake game in java but when i run my code i can't see any thing.
I made node class that has the square info like position, and grid class that has the data of all the squares and GUI class and i tried to use drawRect method but i have no result.
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.*;
import java.util.List;
public class SnakeGame {
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(300,300);
frame.setLayout(null);
Grids grids = new Grids(300);
GUI gui = new GUI(grids.nodesList,grids.nodeSize);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(gui);
frame.setVisible(true);
}
public static class Node{
public float[] position = new float[2];
public Node(float[] Position){
//this is the x position
position[0] = Position[0];
//this is the y position
position[1] = Position[1];
}
}
public static class Grids{
public int nodesNum = 6;
public float screenSize;
public float nodeSize;
public float[] startNodePos = new float[2];
public List<Node> nodesList = new ArrayList<Node>();
public Grids(float ScreenSize){
screenSize = ScreenSize;
nodeSize = screenSize / nodesNum;
//set the start node position
//x
startNodePos[0] = nodeSize /2;
//y
startNodePos[1] = nodeSize /2;
//use for loop to create nodes
for (float X = startNodePos[0] ; X <= nodeSize * 5; X += nodeSize / 2) {
for (float Y = startNodePos[1]; Y <= nodeSize * 5; Y += nodeSize / 2) {
float[] Pos = {X, Y};
Node node = new Node(Pos);
nodesList.add(node);
}
}
}
}
public static class GUI extends JPanel{
public List<Node> nodes;
public float NodeSize;
public GUI(List<Node> Nodes,float nodeSize){
nodes = Nodes;
NodeSize = nodeSize;
}
#Override
public void paintComponents(Graphics g) {
super.paintComponents(g);
for(Node node : nodes){
g.setColor(Color.BLACK);
g.drawRect((int)node.position[0],(int)node.position[1],(int)NodeSize,(int)NodeSize);
}
}
}
}
There are two primary mistakes
First
frame.setLayout(null);
This means that you will become completely responsible for determine the location and size of all child components.
In this case, it's probably just easier to get rid of it and use the default BorderLayout
Second
You're overriding paintComponents, not paintComponent (not the s at the end)
public void paintComponents(Graphics g) {
//...
}
Since, there are no components to be painted, it's not getting called
Simple change it to paintComponent (and make sure you call it's super properly)
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Node node : nodes) {
g.setColor(Color.BLACK);
g.drawRect((int) node.position[0], (int) node.position[1], (int) NodeSize, (int) NodeSize);
}
}
Observations
While running through the code, I note some ... interesting things, which might cause you issues in the long run.
The size of the view area is not the same as the size of the window. A window also needs to display the window decorations (title bar, borders, etc), which are inset into the window. This reduces the available viewable area. It's better to have the content provide a sizing hint and pack the window around, which brings me to...
The model seems to be taking into consideration view properties. You should aim to have the model and view be as agnostic as possible, meaning that the model should not be dictating display state to the view. Instead, the size of the cell's, and by extensions, the size of the panel, should be determined by the view.
I'm trying to write a simple program: a bouncing ball that appears and starts bouncing after you press the "Start" button on the screen. The program should be closed by pressing "X".
For some reason, it runs very slowly. The ball is blinking, and I have to wait for a long time after I press the "X" for program to close.
Here is the code:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class Bounce
{
public static void main(String[] args)
{
JFrame frame = new BounceFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class BounceFrame extends JFrame
{
public BounceFrame()
{
setSize(WIDTH, HEIGHT);
setTitle("Bounce");
Container contentPane = getContentPane();
canvas = new BallCanvas();
contentPane.add(canvas, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
addButton(buttonPanel, "Start", new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
addBall();
}
});
contentPane.add(buttonPanel, BorderLayout.SOUTH);
}
public void addButton(Container c, String title, ActionListener listener)
{
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);
}
public void addBall()
{
try
{
Ball b = new Ball(canvas);
canvas.add(b);
for (int i = 1; i <= 10000; i++)
{
b.move();
Thread.sleep(10);
}
}
catch (InterruptedException exception)
{
}
}
private BallCanvas canvas;
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
}
class BallCanvas extends JPanel
{
public void add(Ball b)
{
balls.add(b);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for (int i = 0; i < balls.size(); i++)
{
Ball b = (Ball)balls.get(i);
b.draw(g2);
}
}
private ArrayList balls = new ArrayList();
}
class Ball
{
public Ball(Component c) { canvas = c; }
public void draw(Graphics2D g2)
{
g2.fill(new Ellipse2D.Double(x, y, XSIZE, YSIZE));
}
public void move()
{
x += dx;
y += dy;
if (x < 0)
{
x = 0;
dx = -dx;
}
if (x + XSIZE >= canvas.getWidth())
{
x = canvas.getWidth() - XSIZE;
dx = -dx;
}
if (y < 0)
{
y = 0;
dy = -dy;
}
if (y + YSIZE >= canvas.getHeight())
{
y = canvas.getHeight() - YSIZE;
dy = -dy;
}
canvas.paint(canvas.getGraphics());
}
private Component canvas;
private static final int XSIZE = 15;
private static final int YSIZE = 15;
private int x = 0;
private int y = 0;
private int dx = 2;
private int dy = 2;
}
The slowness comes from two related problems, one simple and one more complex.
Problem #1: paint vs. repaint
From the
JComponent.paint docs:
Invoked by Swing to draw components.
Applications should not invoke paint directly, but should instead use the repaint method to schedule the component for redrawing.
So the canvas.paint() line at the end of Ball.move must go.
You want to call
Component.repaint
instead...
but just replacing the paint with repaint will reveal the second problem, which prevents the ball from even appearing.
Problem #2: Animating inside the ActionListener
The ideal ActionListener.actionPerformed method changes the program's state and returns as soon as possible, using lazy methods like repaint to let Swing schedule the actual work for whenever it's most convenient.
In contrast, your program does basically everything inside the actionPerformed method, including all the animation.
Solution: A Game Loop
A much more typical structure is to start a
javax.swing.Timer
when your GUI starts, and just let it run
"forever",
updating your simulation's state every tick of the clock.
public BounceFrame()
{
// Original code here.
// Then add:
new javax.swing.Timer(
10, // Your timeout from `addBall`.
new ActionListener()
{
public void actionPerformed(final ActionEvent ae)
{
canvas.moveBalls(); // See below for this method.
}
}
).start();
}
In your case, the most important
(and completely missing)
state is the
"Have we started yet?"
bit, which can be stored as a boolean in BallCanvas.
That's the class that should do all the animating, since it also owns the canvas and all the balls.
BallCanvas gains one field, isRunning:
private boolean isRunning = false; // new field
// Added generic type to `balls` --- see below.
private java.util.List<Ball> balls = new ArrayList<Ball>();
...and a setter method:
public void setRunning(boolean state)
{
this.isRunning = state;
}
Finally, BallCanvas.moveBalls is the new
"update all the things"
method called by the Timer:
public void moveBalls()
{
if (! this.isRunning)
{
return;
}
for (final Ball b : balls)
{
// Remember, `move` no longer calls `paint`... It just
// updates some numbers.
b.move();
}
// Now that the visible state has changed, ask Swing to
// schedule repainting the panel.
repaint();
}
(Note how much simpler iterating over the balls list is now that the list has a proper generic type.
The loop in paintComponent could be made just as straightforward.)
Now the BounceFrame.addBall method is easy:
public void addBall()
{
Ball b = new Ball(canvas);
canvas.add(b);
this.canvas.setRunning(true);
}
With this setup, each press of the space bar adds another ball to the simulation.
I was able to get over 100 balls bouncing around on my 2006 desktop without a hint of flicker.
Also, I could exit the application using the 'X' button or Alt-F4, neither of which responded in the original version.
If you find yourself needing more performance
(or if you just want a better understanding of how Swing painting works),
see
"Painting in AWT and Swing:
Good Painting Code Is the Key to App Performance"
by Amy Fowler.
I would suggest you to use 'Timer' class for running your gameloop.It runs infinitely and you can stop it whenever you want using timer.stop()
You can also set its speed accordingly.
I'm working on the old multi-threaded bouncing balls problem in swing. I've got everything set up so far, but I'd like to add an explosion animation when two balls collide. I've got collision detection and I can display text where the collision took place, but I was wondering on the best modular approach to creating a small animation (for example, pixels exploding 360 degrees around the point, fading out over time)
Class structure:
Ball
public class Ball {
private double x,y,dx,dy;
private static final int XSIZE = 15;
private static final int YSIZE = 15;
public Ball(){
// make x,y,dx,dy random
}
public int getX(){//}
public int getY(){//}
public Point position(){
return new Point(x,y);
}
public void move(Rectangle2D bounds){
//do movement (change x,y,dx,dy)
}
public Ellipse2D getShape(){
return new Ellipse
}
public boolean collide(Ball other){
if (this.position().distance(other.position()) < XSIZE)
return true;
return false;
}
}
BallComponent
public class BallComponent extends JPanel {
public ArrayList<Ball> balls = new ArrayList<Ball>();
private ArrayList<Color> colors = new ArrayList<Color>();
private ArrayList<Point> explosions = new ArrayList<Point>();
Random rnd = new Random();
private boolean exploding = false;
public void add(Ball b) {
balls.add(b);
colors.add(new Color(rnd.nextFloat(),rnd.nextFloat(),rnd.nextFloat()));
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for(int i=0; i<balls.size(); i++){
for(int j=0; j<i; j++){
if (balls.get(i).collide(balls.get(j))){
exploding = true;
explosions.add(balls.get(i).position());
balls.remove(i);
colors.remove(i);
balls.remove(j);
colors.remove(j);
return;
}
}
g2.setColor(colors.get(i));
g2.fill(balls.get(i).getShape());
if (exploding){
for (Point p : explosions){
g2.drawString("boom", p.x, p.y);
}
}
}
}
public void reset(){
balls = new ArrayList<Ball>();
colors = new ArrayList<Color>();
explosions = new ArrayList<Point>();
}
}
Thanks in advance
One thing you must do is get program logic out of the painting method, here the paintComponent method. You have collision detection within this method, your removing logical items from collections from within this method, you're even returning out of the method before it has fully done its primary job -- which is drawing all the components.
Instead I suggest that you re-arrange your program more along M-V-C or Model-View-Controller lines where the state of the balls is held by the model classes, the game loop controlled by the controller class, where collision detection is done in the controller using the model's state, and where the GUI classes, the "view" holds no program logic whatsoever and simply displays the state of the model.
I want to practice paintComponent method. My program is consist of two classes(test and paintClass) in different files. I want to divide the area into 25 squares by drawing lines vertically and horizontally by using paintComponent method. My constructor only has paintComponent now. I know it is not very efficient but I will add buttons, labels and other things in the future so I need to use the program like this. When I run the program I get a nullpointerexception error.Can you help?
EDIT : I changed the page into g and added some lines into code. Still has the same error.
import javax.swing.*;
import java.awt.*;
public class test
{
public static void main(String[] args)
{
JFrame frame = new JFrame("buttons");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
paintClass paint = new paintClass();//This line has error.
paint.repaint();//ADDED
frame.add(paint);
frame.pack();
frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
public class paintClass extends JPanel
{
private Graphics g;
private int interval,side,number;
public paintClass()
{
this.repaint();//ADDED
paintComponent(g);//This line has error.
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);//ADDED and has an error.
this.repaint();//ADDED
side = 250;
number = 5;
interval = side / number;
g.drawRect(0,0, side, side);
for(int i = 0; i <= number - 1; i++)
{
for(int j = 0; j <= number - 1; j++)
{
g.drawLine(i * interval, 0, i * interval, side);
}
g.drawLine(0, i * interval, side, i * interval);
}
}
}
You are calling paintComponent in the paintClass's constructor with a Graphics object that has not been instantiated. For simple drawing, you shouldn't need to call this method directly - it will be called by the EDT (with the appropriate Graphics object) when necessary (in other words, remove that line and don't hold onto a reference to the Graphics object).
//constructor of Class - note Classnames should start with uppercase
public paintClass(){
//no need to call paintComponent or repaint here
}
You can request a repaint by calling the repaint() method defined by JComponent (the parent class of JPanel). You should also call the parent method
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
//do your custom drawing here, and never call repaint from this method
}
I would recommend, if you have not yet, is to study the Oracle tutorials about Custom painting: http://docs.oracle.com/javase/tutorial/uiswing/painting/
First rule of painting in Swing, you don't control the paint process. Swing will paint when and if it wants. The best you can do is provide hints to the system that you would like something updated.
Never maintain a reference to a Graphics or to you didn't create yourself
Never call repaint directly or indirectly from inside any paint method, this will set up a recursive paint cycle which will eventually consume your CPU
It's pointless to call repaint on a component which isn't displayable on the screen
For example
public class paintClass extends JPanel
{
private int interval,side,number;
public paintClass()
{
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
side = 250;
number = 5;
interval = side / number;
g.drawRect(0,0, side, side);
for(int i = 0; i <= number - 1; i++)
{
for(int j = 0; j <= number - 1; j++)
{
g.drawLine(i * interval, 0, i * interval, side);
}
g.drawLine(0, i * interval, side, i * interval);
}
}
}
You need to Override the paintComponent(Graphics g) not to create one. Also, if you want the paintComponent(...) to be executed, call repaint() on that component, do not call paintComponent(...) directly.
In your class "paintClass", you should instantiate the page var :
public paintClass()
{
page = new Graphics();
paintComponent(page);
}
What code should I add so that the rectangles painted before continue to exist on the screen when new ones are printed.Here is the code
import javax.sound.midi.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MiniMusicPlayer3
{
private boolean fire = false;
private JFrame frame;
public static void main(String args[])
{
MiniMusicPlayer3 mini = new MiniMusicPlayer3();
mini.go();
}
public void go()
{
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,600);
frame.setVisible(true);
MyDrawPanel boxes = new MyDrawPanel();
frame.getContentPane().add(boxes);
try
{
Sequencer player =MidiSystem.getSequencer();
player.open();
Sequence seq = new Sequence(Sequence.PPQ,4);
Track track = seq.createTrack();
int arr[] ={127};
player.addControllerEventListener(new MyDrawPanel(),arr);
//add notes to the track
for(int i = 5;i<61;i+=4)
{
track.add(makeEvent(144,1,i,100,i));
track.add(makeEvent(176,1,127,0,i));
track.add(makeEvent(128,1,i,100,(i+2)));
}
player.setSequence(seq);
player.setTempoInBPM(220);
player.start();
}
catch(Exception ex)
{
}
}
public MidiEvent makeEvent(int onOff,int one,int note,int vel,int tick)
{
MidiEvent event = null;
try
{
ShortMessage a = new ShortMessage();
a.setMessage(onOff,one,note,vel);
event = new MidiEvent(a,tick);
}
catch(Exception e)
{
}
finally
{
return event;
}
}
class MyDrawPanel extends JPanel implements ControllerEventListener
{
public void controlChange(ShortMessage message)
{
System.out.println("control change happens");
fire = true;
frame.repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(fire)
{
Graphics2D g2d = (Graphics2D)g;
int red = (int)(Math.random()*255);
int blue = (int)(Math.random()*255);
int green = (int)(Math.random()*255);
Color color = new Color(red,blue,green);
g2d.setColor(color);
int height = (int)((Math.random()*120)+10);
int width = (int)((Math.random()*120)+10);
int x = (int)((Math.random()*40)+10);
int y = (int)((Math.random()*40)+10);
g2d.fillRect(x, y, width, height);
fire = false;
}
}
}
}
Also why does the code above not let the rectangles persist as opposed to the code below that allows the circles to persist
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Animate
{
private JFrame frame;
private int x=10,y=10;
public static void main(String args[])
{
Animate ballRoll = new Animate();
ballRoll.go();
}
public void go()
{
frame = new JFrame();
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyRoll ball = new MyRoll();
frame.getContentPane().add(ball);
for(x = 5;x<=350;x++)
{
y=x;
try
{
Thread.sleep(50);
}
catch(Exception e)
{
System.out.println("dsfsd");
}
ball.repaint();
}
}
class MyRoll extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.ORANGE);
g.fillOval(x, y, 100, 100);
}
}
}
Replace frame.repaint() with this.repaint() and remove super.paintComponent(g) if you want to persist the previous painting as well but I never suggest you to use this approach. You have to redraw all the objects again in paintComponent().
Please have a look at below sections for detail information about Paint processing in Swing application.
Painting in AWT and Swing
The Paint processing
A Closer Look at the Paint Mechanism
"What code should I add so that the rectangles previously printed persist,instead of getting erased when the new rectangles are painted?"
Create a list of Rectangle2D object (as a class member).
Loop through the list in the paintComponent and paint each rectangle.
When you want to add a new rectangle, well, add a new rectangle to the list and repaint.
If you want different colors (or and other state) for each rectangle, create a wrapper like (See some of the examples below).
"Also why does the code above not let the rectangles persist as opposed to the code below that allows the circles to persist"
No rectangle are not "persisting" per se. What you are seeing are paint artifacts from not calling super.paintComponent which clears the previous paint. You should always call super.paintComponent though, like in your first example. So your best options is to go with the first part of my answer.
See a bunch of examples here and here and here and here and here and here.
The basic premise of all those examples is storing a list of similar object in a list and iterating through the list to paint all the objects. Each object can have its own specific state.
You could also extend your MyDrawPanel class so you keep track of which rectangles to paint plus their colors. So you could add each new rectangle to a list of rectangles and keep track of the rectangle colors using a map. Then you just need to add new rectangles to the list and the new color to the map. Finally, you'll need to loop through the list of rectangles and paint these one by one.
Here is how it could be done:
class MyDrawPanel extends JPanel implements ControllerEventListener
{
// List of all rectangles that needs to be painted
java.util.List<Rectangle> rectangles = new ArrayList<Rectangle>();
// Map over all colors for each rectangle that must be painted
Map<Rectangle, Color> rectangleColors = new HashMap<Rectangle, Color>();
public void controlChange(ShortMessage message)
{
System.out.println("control change happens");
fire = true;
frame.repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(fire)
{
Graphics2D g2d = (Graphics2D)g;
int red = (int)(Math.random()*255);
int blue = (int)(Math.random()*255);
int green = (int)(Math.random()*255);
Color color = new Color(red,blue,green);
g2d.setColor(color);
int height = (int)((Math.random()*120)+10);
int width = (int)((Math.random()*120)+10);
int x = (int)((Math.random()*40)+10);
int y = (int)((Math.random()*40)+10);
// Create a new rectangle to paint
Rectangle newRect = new Rectangle(x, y, width, height);
// Store the rectangle in the list over rectangles to paint
rectangles.add(newRect);
// Add the color of the rectangle in the map over rectangle colors
rectangleColors.put(newRect, color);
// Paint all the rectangles using their colors one by one
for (Rectangle rect : rectangles) {
// Get the color of the rectangle
Color rectColor = rectangleColors.get(rect);
// Set the rectangle color
g2d.setColor(rectColor);
// Fill the rectangle with the rectangle color
g2d.fill(rect);
}
fire = false;
}
}
}
There are two common approaches:
as has already been mentioned a couple of times, you keep a List of objects to paint and then iterate through the List in the paintComponent(...) method.
draw to a BufferedImage.
Take a look at Custom Painting Approaches which exams both of these approaches and contains working examples of both.