paintComponent is called, but nothing is displayed - java

I'm new to AWT, this is just a sample program to see how it works.
My problem is that on events like resize/minimize, the window is cleared.
I have overridden paintComponent as specified in many SO questions, and the code is actually called (because if I put a println in there, I see the output), however nothing is displayed.
I've tried this on Windows and Linux and the result is the same, so I guess I'm missing something.
Here is the code:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Site {
private Integer x;
private Integer y;
private Integer width;
private Integer height;
private Graphics2D g2d;
private boolean isOpen = false;
private Integer id = null;
private Integer root = null;
public Site(Integer id, Integer x, Integer y, Integer width, Integer height, Graphics2D g2d) {
this.id = id;
this.root = id;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.g2d = g2d;
}
public void Draw() {
g2d.setPaint(new Color(0, 0, 0));
g2d.setStroke(new BasicStroke(2));
Integer x_top_corner = x - (width / 2);
Integer y_top_corner = y - (height / 2);
g2d.drawRect(x_top_corner, y_top_corner, width, height);
System.out.println(x_top_corner);
if (isOpen) {
g2d.setPaint(new Color(150, 150, 200));
} else {
g2d.setPaint(new Color(200, 200, 200));
}
g2d.fillRect(x_top_corner + 1, y_top_corner + 1, width - 2, height - 2);
}
public void Open() {
this.isOpen = true;
}
public void Close() {
this.isOpen = false;
}
}
class Surface extends JPanel {
private ArrayList<Site> sites = new ArrayList<Site>();
private Graphics2D g2d = null;
private void initSites() {
Integer width = 30;
Integer height = 30;
Integer index = 0;
for (Integer y = height; y < 800 - height - 30; y += height) {
for (Integer x = width; x < 800 - width; x += width) {
sites.add(new Site(index, x, y, width, height, g2d));
index++;
}
}
Integer x = 20;
Integer y = 3;
sites.get(x + (y * 25)).Open();
sites.get(x + 1 + (y * 25)).Open();
sites.get(x).Open();
sites.get(4).Open();
sites.get(9).Open();
sites.get(13).Open();
sites.get(18).Open();
}
private void doDrawing(Graphics g) {
if (g2d == null) {
g2d = (Graphics2D) g;
}
if (sites.size() == 0) {
initSites();
}
for (Site site : sites) {
site.Draw();
}
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
public class Squares extends JFrame {
public Squares() {
initUI();
}
private void initUI() {
add(new Surface());
setTitle("Squares demo");
setSize(800, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Squares ex = new Squares();
ex.setVisible(true);
}
}

You're using Graphics2D as a field assuming that one graphics object is as good as the next, and that's not how you do Swing drawing. The Graphics object given into the paintComponent method is not long-lived or stable, and it must be used and not stored into a field for later use. Instead only use the Graphics object dynamically passed into your program from the JVM via the paintComponent's parameter. For example if you give Draw (rename it to draw) a Graphics or Graphics2D parameter, then pass it in from the paintComponent method:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
class Site {
private Integer x;
private Integer y;
private Integer width;
private Integer height;
// private Graphics2D g2d;
private boolean isOpen = false;
private Integer id = null;
private Integer root = null;
// !! public Site(Integer id, Integer x, Integer y, Integer width, Integer
// height, Graphics2D g2d) {
public Site(Integer id, Integer x, Integer y, Integer width, Integer height) {
this.id = id;
this.root = id;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
// this.g2d = g2d;
}
public void draw(Graphics2D g2d) {
g2d.setPaint(new Color(0, 0, 0));
g2d.setStroke(new BasicStroke(2));
Integer x_top_corner = x - (width / 2);
Integer y_top_corner = y - (height / 2);
g2d.drawRect(x_top_corner, y_top_corner, width, height);
System.out.println(x_top_corner);
if (isOpen) {
g2d.setPaint(new Color(150, 150, 200));
} else {
g2d.setPaint(new Color(200, 200, 200));
}
g2d.fillRect(x_top_corner + 1, y_top_corner + 1, width - 2, height - 2);
}
public void Open() {
this.isOpen = true;
}
public void Close() {
this.isOpen = false;
}
}
class Surface extends JPanel {
private ArrayList<Site> sites = new ArrayList<Site>();
private Graphics2D g2d = null;
private int width;
private int height;
public Surface(int width, int height) {
this.width = width;
this.height = height;
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(width, height);
}
private void initSites() {
Integer width = 30;
Integer height = 30;
Integer index = 0;
for (Integer y = height; y < 800 - height - 30; y += height) {
for (Integer x = width; x < 800 - width; x += width) {
// sites.add(new Site(index, x, y, width, height, g2d));
sites.add(new Site(index, x, y, width, height)); // !!
index++;
}
}
Integer x = 20;
Integer y = 3;
sites.get(x + (y * 25)).Open();
sites.get(x + 1 + (y * 25)).Open();
sites.get(x).Open();
sites.get(4).Open();
sites.get(9).Open();
sites.get(13).Open();
sites.get(18).Open();
}
private void doDrawing(Graphics g) {
if (sites.size() == 0) {
initSites();
}
for (Site site : sites) {
site.draw((Graphics2D) g);
}
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
public class Squares extends JFrame {
private static final int SIDE_LENGTH = 800;
public Squares() {
initUI();
}
private void initUI() {
add(new Surface(SIDE_LENGTH, SIDE_LENGTH));
setTitle("Squares demo");
// setSize(800, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Squares ex = new Squares();
ex.setVisible(true);
});
}
}
Also,
run the program from the Swing event thread only
Don't set sizes directly as you're doing. Override getPreferredSize instead

Related

Why doesn't my Platform draw When using for loops (to access multiple platforms) with array lists

These are all of my classes, I'm trying to make a platformer game with an array list to hold my platforms, this is so I can add more platforms any time and anywhere. For some reason, it's not drawing the platforms.
Can someone please help me with this issue or give me an alternative?
NOTE: some of the variables and methods I either haven't used yet or forgot to delete when i was re-creating my code.
package Game;
import Game.Frame;
public class Main {
public static void main(String[] args) {
new Frame();
}
}
package Game;
import javax.swing.*;
import java.awt.*;
public class Frame extends JFrame {
GamePanel panel;
public Frame() {
panel = new GamePanel();
this.add(panel);
this.setTitle("Platformer Game");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
}
package Game;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Timer;
public class GamePanel extends JPanel implements ActionListener{
Player player1;
Map map1;
final int SCREEN_WIDTH = 1000;
final int SCREEN_HEIGHT = 600;
final int PLAYER_WIDTH = 50;
final int PLAYER_HEIGHT = 60;
final Dimension SCREEN_SIZE = new Dimension(SCREEN_WIDTH,SCREEN_HEIGHT);
boolean falling = false;
boolean playing = true;
Image backgroundImage;
Thread gameThread;
Image image;
Graphics graphics;
Timer gameTimer;
ArrayList<Map> platform = new ArrayList<>();
public GamePanel() {
java.net.URL imgIcon = Main.class.getResource(
"/Resources/spaceImage.jpg");
backgroundImage = new ImageIcon(imgIcon).getImage();
newPlayer();
newMap();
this.setFocusable(true);
this.setPreferredSize(SCREEN_SIZE);
this.setOpaque(true);
this.addKeyListener(new KeyListener(
) {
#Override
public void keyTyped(KeyEvent e) {}
#Override
public void keyPressed(KeyEvent e) {
KeyPressed(e);
}
#Override
public void keyReleased(KeyEvent e) {
KeyReleased(e);
}
});
gameTimer = new Timer();
gameTimer.schedule(new TimerTask(){
#Override
public void run() {
player1.move();
repaint();
}
}, 0 , 17);
}
public void paint(Graphics g) {
image = createImage(getWidth(),getHeight());
graphics = image.getGraphics();
draw(graphics);
g.drawImage(image, 0,0, null);
}
public void draw(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(backgroundImage, 0,0, null);
player1.paint(g);
for(Map map1: platform) {
map1.paint(g2D);
}
}
public void KeyPressed(KeyEvent e) {
if(e.getKeyChar()=='a') {
player1.keyLeft = true;
}
if(e.getKeyChar()=='d') player1.keyRight = true;
if(e.getKeyChar()=='s') player1.keyDown = true;
if(e.getKeyChar()=='w') player1.keyUp = true;
}
public void KeyReleased(KeyEvent e) {
if(e.getKeyChar()=='a') player1.keyLeft = false;
if(e.getKeyChar()=='d') player1.keyRight = false;
if(e.getKeyChar()=='s') player1.keyDown = false;
if(e.getKeyChar()=='w') player1.keyUp = false;
}
public void newPlayer() {
player1 = new Player((SCREEN_WIDTH/2)-(PLAYER_WIDTH/2), (SCREEN_HEIGHT/2)-(PLAYER_WIDTH/2), PLAYER_WIDTH, PLAYER_HEIGHT, this);
}
public void newMap() {
for(int i=50;i<650;i+=50){
platform.add(new Map(i,600,50,50));
}
}
public void gameOver() {
}
#Override
public void actionPerformed(ActionEvent e) {
}
}
package Game;
import Game.GamePanel;
import java.awt.*;
import java.awt.event.KeyEvent;
public class Player extends Rectangle{
double velocityY = 0;
double velocityX = 0;
final int PLAYER_WIDTH = 50;
final int PLAYER_HEIGHT = 50;
static int speed = 2;
GamePanel panel;
boolean keyRight = false;
boolean keyLeft = false;
boolean keyUp = false;
boolean keyDown = false;
Rectangle hitbox;
public Player(int x, int y, int PLAYERWIDTH, int PLAYERHEIGHT, GamePanel panel) {
super(x,y,PLAYERWIDTH,PLAYERHEIGHT);
this.panel = panel;
hitbox = new Rectangle();
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.setColor(Color.red);
g2D.fillRect(x, y, PLAYER_WIDTH, PLAYER_HEIGHT);
}
public void move() {
if(keyLeft && keyRight || !keyLeft && !keyRight) {
velocityX *= 0.8;
}
if(keyLeft && !keyRight) {
velocityX--;
}
if(keyRight && !keyLeft) {
velocityX++;
}
if(velocityX > 0 && velocityX < 0.75) velocityX = 0;
if(velocityX < 0 && velocityX > -0.75) velocityX = 0;
if(velocityX > 7) velocityX = 7;
if(velocityX < -7) velocityX = -7;
if(keyUp) {
velocityY = -6;
}
velocityY += 0.3;
y += velocityY;
x += velocityX;
hitbox.x = x;
hitbox.y = y;
}
}
package Game;
import java.awt.*;
public class Map {
int PLATFORM_WIDTH = 600;
int PLATFORM_HEIGHT = 150;
int x;
int y;
Rectangle hitbox;
public Map(int x, int y, int PLATFORM_WIDTH, int PLATFORM_HEIGHT) {
this.x = x;
this.y = y;
this.PLATFORM_WIDTH = PLATFORM_WIDTH;
this.PLATFORM_HEIGHT = PLATFORM_HEIGHT;
hitbox = new Rectangle(x,y,PLATFORM_WIDTH, PLATFORM_HEIGHT);
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.setColor(Color.gray);
g2D.fillRect(x,y,PLATFORM_WIDTH,PLATFORM_HEIGHT);
}
}
So you set the screen height to 600, final int SCREEN_HEIGHT = 600; but then create your platforms y position to 600 as well, platform.add(new Map(i,600,50,50));.
Since they never move, this is going to paint them off screen, so, a quick solution is to change the y position to something which is within the visible range, maybe 550, that way you will see them (to start with).
Observations
There's a lot of, interesting, ideas going on and I'm not sure you entirely understand how the API works.
Start by having a look at:
Performing Custom Painting
Painting in AWT and Swing
This will give you a better understanding of how the paint system works in Swing and how you should work with it.
Having said that, Swing is double buffered by default, so you don't need your own backing buffer, just override paintComponent and paint to the Graphics context
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
draw(g2d);
g2d.dispose();
}
this will help eliminate one possible area of issues.
Swing is also not thread safe, so you should avoid making up dates to the UI (or state the UI relies on) from outside the context of the Event Dispatching Thread.
Instead of using java.util.Timer, you should be using javax.swing.Timer, which will generate it's callbacks within the context of the EDT.
See Concurrency in Swing and How to Use Swing Timers for more details
gameTimer = new Timer(5, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
player1.move();
repaint();
}
});
gameTimer.start();
KeyListener is well know for causing issues and there is a better system available which resolves these issues, see How to Use Key Bindings for more details.
I'm also not really sure what's going on with Player
public class Player extends Rectangle {
double velocityY = 0;
double velocityX = 0;
final int PLAYER_WIDTH = 50;
final int PLAYER_HEIGHT = 50;
static int speed = 2;
GamePanel panel;
boolean keyRight = false;
boolean keyLeft = false;
boolean keyUp = false;
boolean keyDown = false;
Rectangle hitbox;
public Player(int x, int y, int PLAYERWIDTH, int PLAYERHEIGHT, GamePanel panel) {
super(x, y, PLAYERWIDTH, PLAYERHEIGHT);
this.panel = panel;
hitbox = new Rectangle();
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.setColor(Color.red);
g2D.fillRect(x, y, PLAYER_WIDTH, PLAYER_HEIGHT);
}
You extend it from Rectangle, but then you create another Rectangle within it and I have no idea what all the instance fields are doing at all (you basically ignore what ever's passed in, in favour of your properties)
You could just do something like and use Player as the hotbox itself
public class Player extends Rectangle {
enum Direction {
UP, DOWN, LEFT, RIGHT
}
private double velocityY = 0;
private double velocityX = 0;
private int speed = 2;
public Player(int x, int y, int width, int height) {
super(x, y, width, height);
}
public void paint(Graphics2D g2D) {
g2D.setColor(Color.RED);
g2D.fill(this);
}
Runnable example...
Key bindings can be fun to get your head around, so I've modified your code to support them (and the above mentioned changes) to give you a better idea.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet;
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 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 GamePanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class GamePanel extends JPanel implements ActionListener {
protected static final int SCREEN_WIDTH = 1000;
protected static final int SCREEN_HEIGHT = 600;
protected static final int PLAYER_WIDTH = 50;
protected static final int PLAYER_HEIGHT = 60;
protected static final Dimension SCREEN_SIZE = new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT);
boolean falling = false;
boolean playing = true;
Player player1;
Map map1;
Image backgroundImage;
Timer gameTimer;
ArrayList<Map> platform = new ArrayList<>();
public GamePanel() {
BufferedImage img = new BufferedImage(SCREEN_WIDTH, SCREEN_HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.BLUE);
g2d.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
g2d.dispose();
backgroundImage = new ImageIcon(img).getImage();
newPlayer();
newMap();
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "Pressed.left");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "Pressed.right");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, false), "Pressed.up");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, false), "Pressed.down");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true), "Released.left");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "Released.right");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, true), "Released.up");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, true), "Released.down");
am.put("Pressed.left", new MoveAction(player1, Player.Direction.LEFT, true));
am.put("Pressed.right", new MoveAction(player1, Player.Direction.RIGHT, true));
am.put("Pressed.up", new MoveAction(player1, Player.Direction.UP, true));
am.put("Pressed.down", new MoveAction(player1, Player.Direction.DOWN, true));
am.put("Released.left", new MoveAction(player1, Player.Direction.LEFT, false));
am.put("Released.right", new MoveAction(player1, Player.Direction.RIGHT, false));
am.put("Released.up", new MoveAction(player1, Player.Direction.UP, false));
am.put("Released.down", new MoveAction(player1, Player.Direction.DOWN, false));
gameTimer = new Timer(5, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
player1.move();
repaint();
}
});
gameTimer.start();
}
#Override
public Dimension getPreferredSize() {
return SCREEN_SIZE;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
draw(g2d);
g2d.dispose();
}
public void draw(Graphics2D g2D) {
g2D.drawImage(backgroundImage, 0, 0, null);
player1.paint(g2D);
for (Map map1 : platform) {
map1.paint(g2D);
}
}
public void newPlayer() {
player1 = new Player((SCREEN_WIDTH / 2) - (PLAYER_WIDTH / 2), (SCREEN_HEIGHT / 2) - (PLAYER_WIDTH / 2), PLAYER_WIDTH, PLAYER_HEIGHT);
}
public void newMap() {
for (int i = 50; i < 650; i += 50) {
platform.add(new Map(i, 550, 50, 50));
}
}
public void gameOver() {
}
#Override
public void actionPerformed(ActionEvent e) {
}
}
public class MoveAction extends AbstractAction {
private Player player;
private Player.Direction direction;
private boolean pressed;
public MoveAction(Player player, Player.Direction direction, boolean pressed) {
this.player = player;
this.direction = direction;
this.pressed = pressed;
}
#Override
public void actionPerformed(ActionEvent e) {
if (pressed) {
player.putDirection(direction);
} else {
player.removeDirection(direction);
}
}
}
public class Player extends Rectangle {
enum Direction {
UP, DOWN, LEFT, RIGHT
}
private double velocityY = 0;
private double velocityX = 0;
private int speed = 2;
private Set<Direction> directions = new TreeSet<>();
public Player(int x, int y, int width, int height) {
super(x, y, width, height);
}
public void putDirection(Direction direction) {
directions.add(direction);
}
public void removeDirection(Direction direction) {
directions.remove(direction);
}
public void paint(Graphics2D g2D) {
g2D.setColor(Color.RED);
g2D.fill(this);
}
protected boolean hasDirection(Direction direction) {
return directions.contains(direction);
}
public void move() {
System.out.println(hasDirection(Direction.UP));
if (hasDirection(Direction.LEFT) && hasDirection(Direction.RIGHT) || !hasDirection(Direction.LEFT) && !hasDirection(Direction.RIGHT)) {
velocityX *= 0.8;
}
if (hasDirection(Direction.LEFT) && !hasDirection(Direction.RIGHT)) {
velocityX--;
}
if (hasDirection(Direction.RIGHT) && !hasDirection(Direction.LEFT)) {
velocityX++;
}
if (velocityX > 0 && velocityX < 0.75) {
velocityX = 0;
}
if (velocityX < 0 && velocityX > -0.75) {
velocityX = 0;
}
if (velocityX > 7) {
velocityX = 7;
}
if (velocityX < -7) {
velocityX = -7;
}
if (hasDirection(Direction.UP)) {
velocityY = -6;
}
velocityY += 0.3;
y += velocityY;
x += velocityX;
}
}
public class Map {
int width;
int height;
int x;
int y;
Rectangle hitbox;
public Map(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
hitbox = new Rectangle(x, y, width, height);
}
public void paint(Graphics2D g2D) {
g2D.setColor(Color.GRAY);
g2D.fill(hitbox);
}
}
}

How Do I Properly Move Shapes with a Simple Loop inside of a TimerListener?

I am trying to make a simple screen-saver, and I think I have everything else implemented properly. I just don't know how to make the shapes move. Looking at the code a bit more, I think it has something to do with the way I might have implemented the shapes themselves (the if (line), etc.)? I have looked at other answers regarding this question, but I couldn't quite find the answer I was looking for with the way my code is implemented. Any advice is super helpful, thank you!
Also, just for a side note, is the -40 necessary for the "if" conditions? I thought I heard somewhere that it is useful to leave a little space in-between the frame, but I can't remember why.
import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
public class MyScreenSaver extends JComponent
{
ArrayList <Shapes> randomShapes = new ArrayList <>();
Shapes randomShape;
public MyScreenSaver ()
{
class TimerListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent event)
{
// Create random generation of colors and shapes...
Shape shape = null;
int frameWidth = 800;
int frameHeight = 500;
for (int i = 1; i <= 10; i ++)
{
Random rng = new Random();
boolean line = rng.nextBoolean();
boolean rectangle = rng.nextBoolean();
int red = ((int) (Math.random() * 255));
int green = ((int) (Math.random() * 255));
int blue = ((int) (Math.random() * 255));
Color color = new Color (red, green, blue);
int width = (10 + (int) (40 * Math.random()));
int height = (10 + (int) (40 * Math.random()));
int x = (int) (Math.random() * (getWidth() - width));
int y = (int) (Math.random() * (getHeight() - height));
int velX = 2;
int velY = 2;
int newX = velX + x;
int newY = velY + y;
if (line)
{
shape = new Line2D.Double(x, y, x + width, y + height);
}
else if (rectangle)
{
shape = new Rectangle2D.Double(x, y, width, height);
}
else
{
shape = new Ellipse2D.Double(x, y, width, height);
}
// Here, we want the shapes to stop appearing after reaching a certain size...
if (randomShapes.size() >= 20)
{
break;
}
Shapes randomShape = new Shapes (color, shape);
// Add the shapes to the randomShapes ArrayList...
randomShapes.add (randomShape);
// Here, we are moving the shapes...
for (Shapes shapeMove : randomShapes)
{
if (x < 0 || x > frameWidth - 40)
{
velX = velX * -1;
}
else
{
x = newX;
}
if (y < 0 || y > frameHeight - 40)
{
velY = velY * -1;
}
else
{
y = newY;
}
}
repaint();
}
}
}
ActionListener listener = new TimerListener();
final int DELAY = 100;
Timer t = new Timer(DELAY, listener);
t.start();
}
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (Shapes shape : randomShapes)
{
System.out.println (randomShapes.size());
shape.paint(g2d);
}
g2d.dispose();
}
public static void main (String [] args)
{
// Set up the main frame...
final int FRAME_WIDTH = 800;
final int FRAME_HEIGHT = 500;
JFrame screenSaverFrame = new JFrame ();
screenSaverFrame.setTitle("Homework 6");
screenSaverFrame.setSize (FRAME_WIDTH, FRAME_HEIGHT);
screenSaverFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final MyScreenSaver component = new MyScreenSaver ();
screenSaverFrame.add (component);
screenSaverFrame.setVisible (true);
}
}
Here is the loop specifically...
for (Shapes shapeMove : randomShapes)
{
if (x < 0 || x > frameWidth - 40)
{
velX = velX * -1;
}
else
{
x = newX;
}
if (y < 0 || y > frameHeight - 40)
{
velY = velY * -1;
}
else
{
y = newY;
}
}
I was thinking about placing the contents of the loop inside a void method, but I really don't think that is necessary.
Also, here is my Shape class as well.
import java.awt.*;
public class Shapes
{
private final Color color;
private final Shape shape;
public Shapes (Color color, Shape shape)
{
this.color = color;
this.shape = shape;
}
public Color getColor ()
{
return color;
}
public Shape getShape ()
{
return shape;
}
public void paint(Graphics2D g2d)
{
g2d.setColor(color);
g2d.draw(shape);
g2d.fill(shape);
}
}
The following mre is based on your code. Note the comments documenting the changes made:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.Timer;
public class MyScreenSaver extends JComponent
{
private final ArrayList <ColoredShape> randomShapes = new ArrayList <>();
//define constants
private static final int FRAME_WIDTH = 800, FRAME_HEIGHT = 500, DELAY = 100, MAX_SHAPE_HEIGHT = 40,
MAX_SHAPE_WIDTH = 40, MIN_SHAPE_HEIGHT = 10, MIN_SHAPE_WIDTH = 40, MAX_SHAPE_COUNT = 20,
LINE_WIDTH = 5, X_STEP = 5, Y_STEP = 5;
//define shape types
enum ShapeType {LINE, RECTANGLE, ELIPSE};
public MyScreenSaver ()
{
ActionListener listener = new TimerListener();
Timer t = new Timer(DELAY, listener);
t.start();
}
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(LINE_WIDTH));
for (ColoredShape shape : randomShapes) //draw stored shapes
{
shape.paint(g2d);
}
g2d.dispose();
}
//invoked repeatedly by Timer. Each time it adds a shape to the collection
//moves all shapes and repaints
class TimerListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent event)
{
// Create random generation of colors and shapes and location
//up to MAX_SHAPE_COUNT
if (randomShapes.size() < MAX_SHAPE_COUNT)
{
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
Color color = new Color (red, green, blue);
int width = MIN_SHAPE_WIDTH + (int) (MAX_SHAPE_WIDTH * Math.random());
int height = MIN_SHAPE_HEIGHT + (int) (MAX_SHAPE_HEIGHT* Math.random());
int x = (int) (Math.random() * (getWidth() - width));
int y = (int) (Math.random() * (getHeight() - height));
//select a random shape type
int typeIndex = (int) (Math.random()* ShapeType.values().length);
ShapeType type = ShapeType.values()[typeIndex];
// Add the shapes to the randomShapes ArrayList...
randomShapes.add (new ColoredShape (type,color,x, y,width,height));
}
//move the shapes...
for (ColoredShape shape : randomShapes)
{
int x = shape.getX();//get current x position
if (x <= 0 || x > getWidth() - shape.getWidth())
{
shape.setXDirection(-1*shape.getXDirection());//change direction
}
shape.setX(shape.getX() + shape.getXDirection()*X_STEP);//increment
int y = shape.getY();
if (y <= 0 || y > getHeight()- shape.getHeight())
{
shape.setYDirection(-1*shape.getYDirection());
}
shape.setY(shape.getY() + shape.getYDirection()*Y_STEP);
}
repaint();
}
}
public class ColoredShape
{
private int x, y;
private final int width, height;
private int xDirection, yDirection;
private final Color color;
private final ShapeType type;
public ColoredShape(ShapeType type, Color color, int x, int y, int width, int height)
{
this.type = type;
this.color = color;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
xDirection = yDirection = 1; //1 means increase value, -1 decrease value
}
public Color getColor ()
{
return color;
}
private Shape constructShape ()
{
//construct new shape using updated bounds
if(type == ShapeType.LINE)
return new Line2D.Double(x, y, x + width, y + height);
else if (type == ShapeType.RECTANGLE)
return new Rectangle2D.Double(x, y, width, height);
else
return new Ellipse2D.Double(x, y, width, height);
}
public void paint(Graphics2D g2d)
{
g2d.setColor(color);
Shape shape = constructShape();
g2d.draw(shape);
g2d.fill(shape);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getXDirection() {
return xDirection;
}
public void setXDirection(int xDirextion) {
xDirection = xDirextion;
}
public int getYDirection() {
return yDirection;
}
public void setYDirection(int yDirection) {
this.yDirection = yDirection;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
public static void main (String [] args)
{
// Set up the main frame...
JFrame screenSaverFrame = new JFrame ();
screenSaverFrame.setTitle("Homework 6");
screenSaverFrame.setSize (FRAME_WIDTH, FRAME_HEIGHT);
screenSaverFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screenSaverFrame.add (new MyScreenSaver ());
screenSaverFrame.setVisible (true);
}
}

java - Graphics2D rendering is too slow?

I was making a class for rectangles that are able to change angle, fill color, line color, line thickness. And the rectangles are to be rendered that way. I decided to do a little check up. During the check, I found out that the rectangles are rendered too slowly. I could see them being rendered from top to bottom. What might be the reason?
GEdit
import javax.swing.*;
public class GEdit {
public static void main(String[] args)
{
frame app = new frame();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(1000,1000);
app.setVisible(true);
}
}
Figure.java
import java.awt.*;
import java.awt.geom.Point2D;
public abstract class Figure
{
public static final int TOPLEFT = 0;
public static final int TOPCENTER = 1;
public static final int TOPRIGHT = 2;
public static final int CENTERLEFT = 3;
public static final int CENTER = 4;
public static final int CENTERRIGHT = 5;
public static final int BOTTOMLEFT = 6;
public static final int BOTTOMCENTER = 7;
public static final int BOTTOMRIGHT = 8;
private boolean fill_able;
private int fill_option;
private Color fill_color;
private int fill_transparency;
private Color line_color;
private float line_thickness;
private int line_transparency;
private int width;
private int height;
private float rotate_angle;
private boolean width_height_ratio_fixed;
private Point location;
private int base;
public Figure(boolean fill_able,
int fill_option,
Color fill_color,
int fill_transparency,
Color line_color,
float line_thickness,
int line_transparency,
int width, int height,
float rotate_angle,
boolean width_height_ratio_fixed,
int x, int y,
int base)
{
this.fill_able = fill_able;
this.fill_option = fill_option;
this.fill_color = fill_color;
this.fill_transparency = fill_transparency;
this.line_color = line_color;
this.line_thickness = line_thickness;
this.line_transparency = line_transparency;
this.width = width;
this.height = height;
this.rotate_angle = rotate_angle;
this.width_height_ratio_fixed = width_height_ratio_fixed;
this.location = new Point(x,y);
this.base = base;
}
public boolean is_fill_able()
{
return fill_able;
}
public int get_fill_option()
{
return fill_option;
}
public Color get_fill_color()
{
return fill_color;
}
public int get_fill_transparency()
{
return fill_transparency;
}
public Color get_line_color()
{
return line_color;
}
public float get_line_thickness()
{
return line_thickness;
}
public int get_line_transparency()
{
return line_transparency;
}
public int get_width()
{
return width;
}
public int get_height()
{
return height;
}
public float get_rotate_angle()
{
return rotate_angle;
}
public boolean is_width_height_ratio_fixed()
{
return width_height_ratio_fixed;
}
public Point get_location()
{
return location;
}
public Point get_render_location()
{
int x, y;
switch(base)
{
case TOPLEFT:
x = location.x;
y = location.y;
break;
case TOPCENTER:
x = location.x - (int)Math.round(width / 2);
y = location.y;
break;
case TOPRIGHT:
x = location.x - width;
y = location.y;
break;
case CENTERLEFT:
x = location.x;
y = location.y - (int)Math.round(height / 2);
break;
case CENTER:
x = location.x - (int)Math.round(width / 2);
y = location.y - (int)Math.round(height / 2);
break;
case CENTERRIGHT:
x = location.x - width;
y = location.y - (int)Math.round(height / 2);
break;
case BOTTOMLEFT:
x = location.x;
y = location.y - height;
break;
case BOTTOMCENTER:
x = location.x - (int)Math.round(width / 2);
y = location.y - height;
break;
case BOTTOMRIGHT:
x = location.x - width;
y = location.y - height;
break;
default:
x = 0; y = 0;
break;
}
return new Point(x, y);
}
public int get_base()
{
return base;
}
public void set_fill_option(int option)
{
this.fill_option = option;
}
public void set_fill_color(Color color)
{
this.fill_color = color;
}
public void set_fill_transparency(int transparency)
{
this.fill_transparency = transparency;
}
public void set_line_color(Color color)
{
this.line_color = color;
}
public void set_line_transparency(int transparency)
{
this.line_transparency = transparency;
}
public void set_width(int width)
{
if(this.width_height_ratio_fixed)
{
float ratio = this.height / this.width;
this.width = width;
this.height = (int)Math.round(width * ratio);
}
else { this.width = width;}
}
public void set_height(int height)
{
if(this.width_height_ratio_fixed)
{
float ratio = this.width / this.height;
this.height = height;
this.width = (int)Math.round(height * ratio);
}
else{ this.height = height;}
}
public void set_rotate_angle(float angle)
{
if(angle > 360)
{
this.rotate_angle = angle % 360;
}
else this.rotate_angle = angle;
}
public void set_location(int x, int y)
{
this.location.setLocation(x, y);
}
public void set_location(Point location)
{
this.location = location;
}
public void set_base(int base)
{
this.base = base;
}
abstract public void render(Graphics2D g);
}
Rectangle.java
import java.awt.*;
import java.awt.Graphics2D;
import java.awt.geom.*;
public class Rectangle extends Figure {
public Rectangle(Color fill_color, Color line_color, float line_thickness, int width, int height, int x, int y)
{
super(true,
0,
fill_color,
255,
line_color,
line_thickness,
255,
width, height,
0,
false,
x, y,
TOPLEFT);
}
public void render(Graphics2D g)
{
Rectangle2D rectangle = new Rectangle2D.Float(get_width(),get_height(),get_render_location().x,get_render_location().y);
g.rotate(Math.toRadians(get_rotate_angle()),rectangle.getCenterX(),rectangle.getCenterY());
if(is_fill_able())
{
Color color = new Color(
get_fill_color().getRed(),
get_fill_color().getGreen(),
get_fill_color().getBlue(),
get_fill_transparency());
g.setColor(color);
g.fill(rectangle);
}
Stroke old_stroke = g.getStroke();
g.setStroke(new BasicStroke(get_line_thickness()));
Color line_color = new Color(
get_line_color().getRed(),
get_line_color().getGreen(),
get_line_color().getBlue(),
get_line_transparency());
g.setColor(line_color);
g.draw(rectangle);
g.setStroke(old_stroke);
g.rotate(Math.toRadians(get_rotate_angle() * -1),rectangle.getCenterX(),rectangle.getCenterY());
}
}
frame.java
import javax.print.DocFlavor;
import javax.swing.JFrame;
import java.awt.*;
import java.util.Random;
public class frame extends JFrame{
public frame()
{
super("test");
}
#Override
public void paint(Graphics g) {
Graphics2D graphics2D = (Graphics2D)g;
RenderingHints renderingHints1 = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
RenderingHints renderingHints2 = new RenderingHints(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
graphics2D.setRenderingHints(renderingHints1);
graphics2D.setRenderingHints(renderingHints2);
Random random = new Random();
g.setColor(Color.WHITE);
g.fillRect(0,0,1000,1000);
for(int i =0;i < 10; i++)
{
Rectangle rectangle = new Rectangle(Color.CYAN,Color.BLACK,random.nextInt(10),random.nextInt(1000),random.nextInt(1000),random.nextInt(300),random.nextInt(300));
rectangle.set_fill_transparency(random.nextInt(255));
rectangle.set_rotate_angle(random.nextInt(180));
rectangle.render(graphics2D);
}
}
}
Don't override paint of top level containers like JFrame, start with something like JPanel and override it's paintComponent method - that way you get double buffering for free.
Consider not creating new objects on each paint cycle - this is create a number of short lived objects which can affect performance
You could perform some of the operations outside of the paint method (setting the properties) and just focus on painting the inside the paint method
Remember, transformations (such as rotate and translate) are compounding, this means they will affect everything painted after them. You either need to take a snapshot of the Graphics context before hand and dispose of it when you're finished, or reverse the transformations.
You might like to take a look at Performing Custom Painting and Painting in AWT and Swing for more details about how painting works in Swing.
Without changing anything, other then using a JPanel and paintComponent, it works fine.
I even threw in a Swing Timer and repainted the panel at 25fps a second without issue. And just for fun, I set it to 200fps without issue
import java.awt.BasicStroke;
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.RenderingHints;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.util.Random;
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 Random random = new Random();
public TestPane() {
Timer timer = new Timer(40, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
repaint();
}
});
timer.start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
RenderingHints renderingHints1 = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
RenderingHints renderingHints2 = new RenderingHints(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(renderingHints1);
g2d.setRenderingHints(renderingHints2);
for (int i = 0; i < 10; i++) {
Rectangle rectangle = new Rectangle(Color.CYAN,
Color.BLACK,
random.nextInt(10),
random.nextInt(200),
random.nextInt(200),
random.nextInt(200), random.nextInt(200));
rectangle.set_fill_transparency(random.nextInt(255));
rectangle.set_rotate_angle(random.nextInt(180));
Graphics2D iDontTrustYou = (Graphics2D) g2d.create();
rectangle.render(iDontTrustYou);
iDontTrustYou.dispose();
}
g2d.dispose();
}
}
public abstract class Figure {
public static final int TOPLEFT = 0;
public static final int TOPCENTER = 1;
public static final int TOPRIGHT = 2;
public static final int CENTERLEFT = 3;
public static final int CENTER = 4;
public static final int CENTERRIGHT = 5;
public static final int BOTTOMLEFT = 6;
public static final int BOTTOMCENTER = 7;
public static final int BOTTOMRIGHT = 8;
private boolean fill_able;
private int fill_option;
private Color fill_color;
private int fill_transparency;
private Color line_color;
private float line_thickness;
private int line_transparency;
private int width;
private int height;
private float rotate_angle;
private boolean width_height_ratio_fixed;
private Point location;
private int base;
public Figure(boolean fill_able,
int fill_option,
Color fill_color,
int fill_transparency,
Color line_color,
float line_thickness,
int line_transparency,
int width, int height,
float rotate_angle,
boolean width_height_ratio_fixed,
int x, int y,
int base) {
this.fill_able = fill_able;
this.fill_option = fill_option;
this.fill_color = fill_color;
this.fill_transparency = fill_transparency;
this.line_color = line_color;
this.line_thickness = line_thickness;
this.line_transparency = line_transparency;
this.width = width;
this.height = height;
this.rotate_angle = rotate_angle;
this.width_height_ratio_fixed = width_height_ratio_fixed;
this.location = new Point(x, y);
this.base = base;
}
public boolean is_fill_able() {
return fill_able;
}
public int get_fill_option() {
return fill_option;
}
public Color get_fill_color() {
return fill_color;
}
public int get_fill_transparency() {
return fill_transparency;
}
public Color get_line_color() {
return line_color;
}
public float get_line_thickness() {
return line_thickness;
}
public int get_line_transparency() {
return line_transparency;
}
public int get_width() {
return width;
}
public int get_height() {
return height;
}
public float get_rotate_angle() {
return rotate_angle;
}
public boolean is_width_height_ratio_fixed() {
return width_height_ratio_fixed;
}
public Point get_location() {
return location;
}
public Point get_render_location() {
int x, y;
switch (base) {
case TOPLEFT:
x = location.x;
y = location.y;
break;
case TOPCENTER:
x = location.x - (int) Math.round(width / 2);
y = location.y;
break;
case TOPRIGHT:
x = location.x - width;
y = location.y;
break;
case CENTERLEFT:
x = location.x;
y = location.y - (int) Math.round(height / 2);
break;
case CENTER:
x = location.x - (int) Math.round(width / 2);
y = location.y - (int) Math.round(height / 2);
break;
case CENTERRIGHT:
x = location.x - width;
y = location.y - (int) Math.round(height / 2);
break;
case BOTTOMLEFT:
x = location.x;
y = location.y - height;
break;
case BOTTOMCENTER:
x = location.x - (int) Math.round(width / 2);
y = location.y - height;
break;
case BOTTOMRIGHT:
x = location.x - width;
y = location.y - height;
break;
default:
x = 0;
y = 0;
break;
}
return new Point(x, y);
}
public int get_base() {
return base;
}
public void set_fill_option(int option) {
this.fill_option = option;
}
public void set_fill_color(Color color) {
this.fill_color = color;
}
public void set_fill_transparency(int transparency) {
this.fill_transparency = transparency;
}
public void set_line_color(Color color) {
this.line_color = color;
}
public void set_line_transparency(int transparency) {
this.line_transparency = transparency;
}
public void set_width(int width) {
if (this.width_height_ratio_fixed) {
float ratio = this.height / this.width;
this.width = width;
this.height = (int) Math.round(width * ratio);
} else {
this.width = width;
}
}
public void set_height(int height) {
if (this.width_height_ratio_fixed) {
float ratio = this.width / this.height;
this.height = height;
this.width = (int) Math.round(height * ratio);
} else {
this.height = height;
}
}
public void set_rotate_angle(float angle) {
if (angle > 360) {
this.rotate_angle = angle % 360;
} else {
this.rotate_angle = angle;
}
}
public void set_location(int x, int y) {
this.location.setLocation(x, y);
}
public void set_location(Point location) {
this.location = location;
}
public void set_base(int base) {
this.base = base;
}
abstract public void render(Graphics2D g);
}
public class Rectangle extends Figure {
public Rectangle(Color fill_color, Color line_color, float line_thickness, int width, int height, int x, int y) {
super(true,
0,
fill_color,
255,
line_color,
line_thickness,
255,
width, height,
0,
false,
x, y,
TOPLEFT);
}
public void render(Graphics2D g) {
Rectangle2D rectangle = new Rectangle2D.Float(get_width(), get_height(), get_render_location().x, get_render_location().y);
g.rotate(Math.toRadians(get_rotate_angle()), rectangle.getCenterX(), rectangle.getCenterY());
if (is_fill_able()) {
Color color = new Color(
get_fill_color().getRed(),
get_fill_color().getGreen(),
get_fill_color().getBlue(),
get_fill_transparency());
g.setColor(color);
g.fill(rectangle);
}
Stroke old_stroke = g.getStroke();
g.setStroke(new BasicStroke(get_line_thickness()));
Color line_color = new Color(
get_line_color().getRed(),
get_line_color().getGreen(),
get_line_color().getBlue(),
get_line_transparency());
g.setColor(line_color);
g.draw(rectangle);
g.setStroke(old_stroke);
g.rotate(Math.toRadians(get_rotate_angle() * -1), rectangle.getCenterX(), rectangle.getCenterY());
}
}
}
A slightly better optimised version might look something like...
public class TestPane extends JPanel {
private Random random = new Random();
private List<Rectangle> rectangles = new ArrayList<>(25);
public TestPane() {
for (int i = 0; i < 1000; i++) {
Rectangle rectangle = new Rectangle(Color.CYAN,
Color.BLACK,
random.nextInt(10),
random.nextInt(200),
random.nextInt(200),
random.nextInt(200), random.nextInt(200));
rectangle.set_fill_transparency(random.nextInt(255));
rectangle.set_rotate_angle(random.nextInt(180));
rectangles.add(rectangle);
}
Timer timer = new Timer(5, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
for (Rectangle rectangle : rectangles) {
rectangle.set_height(random.nextInt(200));
rectangle.set_width(random.nextInt(200));
rectangle.set_location(random.nextInt(200), random.nextInt(200));
rectangle.set_fill_transparency(random.nextInt(255));
rectangle.set_rotate_angle(random.nextInt(180));
}
repaint();
}
});
timer.start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
RenderingHints renderingHints1 = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
RenderingHints renderingHints2 = new RenderingHints(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(renderingHints1);
g2d.setRenderingHints(renderingHints2);
for (Rectangle rectangle : rectangles) {
Graphics2D iDontTrustYou = (Graphics2D) g2d.create();
rectangle.render(iDontTrustYou);
iDontTrustYou.dispose();
}
g2d.dispose();
}
}
If you still find that the performance isn't up to what you want, you might consider having a look at BufferStrategy which will give you complete control over the painting process
You are overriding the method paint, which is usually a bad idea.
You are also calculating all the information needed DURING the paint, which is another bad idea: they should be calculated before. If you need 4x10 random numbers, maybe calculate them before the paint and store the 40 numbers in a int[] array.
As someone mentioned, you should want to use a JPanel and the method paintComponent.
The following link might be an interesting reading for you:
http://www.billharlan.com/papers/Improving_Swing_Performance.html

JAVA program Bouncing Ball, change the size (pulsating) with a boolean. How to do that?

i have looked all over the internet and in my school books but I can't seem to slove my problem.
In my program "bouncing ball" (got the code from our teacher) i need to change the size of the ball from small to bigger and reverse. I understand that i need a boolean to do that and maybe alsow an if statment. This is what I have in the Ball class rigth now regarding the size change:
private boolean changeSize = true;
int maxSize = 10;
int minSize = 1;
public void changeSize(boolean size ){
if(size == maxSize ){
return minSize;
}
else return maxSize;
}
public void changeBallSize(int d, int f){
diameter = d*f;
This is the whole code for the class Ball:
class Ball {
static int defaultDiameter = 10;
static Color defaultColor = Color.yellow;
static Rectangle defaultBox = new Rectangle(0,0,100,100);
// Position
private int x, y;
// Speen and angel
private int dx, dy;
// Size
private int diameter;
// Color
private Color color;
// Bouncing area
private Rectangle box;
// New Ball
public Ball( int x0, int y0, int dx0, int dy0 ) {
x = x0;
y = y0;
dx = dx0;
dy = dy0;
color = defaultColor;
diameter = defaultDiameter;
}
// New color
public void setColor( Color c ) {
color = c;
}
public void setBoundingBox( Rectangle r ) {
box = r;
}
// ball
public void paint( Graphics g ) {
// Byt till bollens färg
g.setColor( color );
g.fillOval( x, y, diameter, diameter );
}
void constrain() {
// Ge absoluta koordinater för det rektangulära området
int x0 = box.x;
int y0 = box.y;
int x1 = x0 + box.width - diameter;
int y1 = y0 + box.height - diameter;
// Setting speed and angels
if (x < x0)
dx = Math.abs(dx);
if (x > x1)
dx = -Math.abs(dx);
if (y < y0)
dy = Math.abs(dy);
if (y > y1)
dy = -Math.abs(dy);
}
// movingt the ball
x = x + dx;
y = y + dy;
constrain();
}
}
I am a total rookie of java! Thanks for the help!
Add the following into your Ball class:
private int changeFlag=-1;
In your constrain() function, just before the last line, after moving the ball:
if(diameter==maxSize) {
changeFlag=-1;
}
else if (diameter==minSize) {
changeFlag=1;
}
diameter=diameter+changeFlag;
Add this code to your Ball Class
private minSize = 1;
private maxSize = 10;
public void setDiameter(int newDiameter) {
this.diameter = newDiameter;
}
public int getMinSize() {
return minSize;
}
public int getMinSize() {
return maxSize;
}
Use this when you use the ball
Ball ball = new Ball(1,1,1,1);
int newDiameter = 10;
if(newDiameter == ball.getMinSize()) {
ball.setDiameter(ball.getMaxSize());
}
id(newDiameter == ball.getMaxSize()) {
ball.setDiameter (ball.getMinSize());
}
I've edited it, is this what you mean?
Main Class:
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
public class Main extends JFrame {
public static void main(String[] args) {
new Main();
}
public Main() {
// configure JFrame
setSize(640, 360);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create ball
Ball ball = new Ball(this.getWidth()/2, this.getHeight()/2, 50, 100);
add(ball);
Thread t = new Thread(ball);
t.start();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
}
}
Ball Class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JComponent;
public class Ball extends JComponent implements Runnable {
private int x, y, minDiameter, maxDiameter, currentDiameter, growRate = -1;
private Color color = Color.BLUE;
public Ball(int x, int y, int minDiameter, int maxDiameter) {
this.x = x;
this.y = y;
this.minDiameter = minDiameter;
this.maxDiameter = maxDiameter;
this.currentDiameter = minDiameter;
setVisible(true);
}
#Override
public void run() {
while (true) {
// coerce max and min size
if (this.currentDiameter + growRate > maxDiameter) {
this.currentDiameter = maxDiameter;
this.growRate = -1;
}
if (this.currentDiameter + growRate < minDiameter) {
this.currentDiameter = minDiameter;
this.growRate = 1;
}
this.currentDiameter += this.growRate;
repaint();
try {
Thread.sleep(10);
}
catch(Exception e) {
System.out.println(e.toString());
}
}
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
g2.setColor(this.color);
g2.fillOval(this.x, this.y, this.currentDiameter, this.currentDiameter);
}
}

Stop some graphics from redrawing

I'm completely stuck on a problem i'm having with this program where I have to draw a city with swing. Basically what i'm trying to do is make it so that the windows don't change every frame. I've tried just about everything I can think of and nothing has worked yet.
Here is the main class that draws everything
import java.applet.Applet;
import java.awt.*;
import java.util.*;
import javax.swing.JFrame;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class Skyline extends JFrame implements MouseMotionListener
{
private int mX, mY; //Mouse cooddinates
private Image mImage; //Image buffer
//private Image mImage2; //Image buffer
private int num = 0;
private Building bldg1 = new Building(305, 110, 30);
private Building bldg2 = new Building(380, 125, 170);
private Building bldg3 = new Building(245, 200, 325);
private Building bldg4 = new Building(470, 170, 555);
private Building bldg5 = new Building(395, 200, 755);
private Background bg = new Background();
public void init ()
{
}
public static void main(String []args)
{
Skyline f = new Skyline();
f.setSize(1017, 661); //Sets size of window
f.setTitle("Skyline"); //Sets title of window
f.show();
}
public void paintOffscreen(Graphics page)
{
//Draws the background
bg.draw(page);
//Moving square
num++;
if (num > 1200)
num = 0;
page.setColor(Color.yellow);
page.fillRect(num,100,100,100);
//Draws the buildings
bldg1.draw(page);
bldg2.draw(page);
bldg3.draw(page);
bldg4.draw(page);
bldg5.draw(page);
//Mouse move square
int s = 100;
page.setColor(Color.yellow);
page.fillRect(mX - s / 2, mY - s / 2, s, s);
repaint();
}
//====================================BUFFER CODE========================================
public void paint(Graphics g)
{
//Clear the buffer
Dimension d = getSize();
checkOffscreenImage();
Graphics offG = mImage.getGraphics();
offG.setColor(getBackground());
offG.fillRect(0, 0, d.width, d.height);
//Save frame to buffer
paintOffscreen(mImage.getGraphics());
//Draw the buffer
g.drawImage(mImage, 0, 0, null);
}
private void checkOffscreenImage()
{
Dimension d = getSize();
if (mImage == null || mImage.getWidth(null) != d.width || mImage.getHeight(null) != d.height)
mImage = createImage(d.width, d.height);
}
//=======================================================================================
//==================================MOUSE MOVE CODE======================================
public Skyline()
{
addMouseMotionListener(this);
setVisible(true);
}
public void mouseMoved(MouseEvent me)
{
Graphics g = getGraphics();
mX = (int) me.getPoint().getX();
mY = (int) me.getPoint().getY();
update(g);
//repaint();
}
public void mouseDragged(MouseEvent me)
{
mouseMoved(me);
}
//=======================================================================================
}
And here is the window class that might be able to be fixed somehow.
import java.applet.Applet;
import java.awt.*;
import java.util.Random;
import javax.swing.JFrame;
public class Windows extends JFrame
{
private Random gen = new Random();
private int height, width, locX;
private int onOff = 0;
public Windows()
{
height = 305;
width = 110;
locX = 30;
}
public Windows(int height, int width, int locX)
{
this.height = height;
this.width= width;
this.locX = locX;
}
public void draw(Graphics page)
{
page.setColor (Color.darkGray);
page.fillRect (locX, 550 - height, width, height);
for (int i = 550 - height + 5; i < 550; i += 15)
{
for (int x = locX + 5; x < locX + width; x += 15)
{
onOff = gen.nextInt(2);
if(onOff == 0)
page.setColor(Color.black);
else
page.setColor(Color.yellow);
page.fillRect (x,i,10,10);
}
}
}
}
Heres the building class just in case.
import java.applet.Applet;
import java.awt.*;
import javax.swing.JFrame;
public class Building extends JFrame
{
private int height, width, locX;
private int onOff;
private Windows windows1;// = new Windows(height, width, locX);
public Building()
{
height = 305;
width = 110;
locX = 30;
windows1 = new Windows(height, width, locX);
}
public Building(int height, int width, int locX)
{
this.width = width;
this.height = height;
this.locX = locX;
windows1 = new Windows(height, width, locX);
}
public void draw(Graphics page)
{
page.setColor (Color.darkGray);
page.fillRect (locX, 550 - height, width, height);
windows1.draw(page);
}
}
And the bg class just to be safe
import java.applet.Applet;
import java.awt.*;
public class Background extends Applet
{
private int height, width;
public Background()
{
height = 400;
width = 2000;
}
public Background(int height, int width)
{
this.height = height;
this.width = width;
}
public void draw(Graphics page)
{
//Draws the sky
page.setColor(Color.cyan);
page.fillRect(0,0,2000,2000);
//Draws the grass
page.setColor (Color.green);
page.fillRect (0,500,width,height);
}
}
A number of things jump out at me immediately...
You're trying to use a off screen buffer, but you're recreating it each time you paint to the screen...
public void paintOffscreen(Graphics page)
{
//Draws the background
bg.draw(page);
//Moving square
num++;
if (num > 1200)
num = 0;
page.setColor(Color.yellow);
page.fillRect(num,100,100,100);
//Draws the buildings
bldg1.draw(page);
bldg2.draw(page);
bldg3.draw(page);
bldg4.draw(page);
bldg5.draw(page);
//Mouse move square
int s = 100;
page.setColor(Color.yellow);
page.fillRect(mX - s / 2, mY - s / 2, s, s);
repaint();
}
Additionally, the last call in the method is to repaint. This is a bad idea. This could cause you paint method to be recalled, again and again and again...
You would be better of rendering the backing buffer only when it needs to change...
public void paint(Graphics g)
{
super.paint(g); // YOU MUST CALL super.paint!!!!
//Clear the buffer
Dimension d = getSize();
checkOffscreenImage();
//Draw the buffer
g.drawImage(mImage, 0, 0, null);
}
private void checkOffscreenImage()
{
Dimension d = getSize();
if (mImage == null || mImage.getWidth(null) != d.width || mImage.getHeight(null) != d.height) {
mImage = createImage(d.width, d.height);
Graphics offG = mImage.getGraphics();
offG.setColor(getBackground());
offG.fillRect(0, 0, d.width, d.height);
//Save frame to buffer
paintOffscreen(offG);
offG.dispose(); // If you create it, you must dispose of it...
}
}
Now, this is going to raise some issues with invalidating the buffer. This can be achieved by overriding invalidate and setting the mImage to null
public void invalidate() {
mImage = null;
super.invalidate();
}
You're extending most of your components from JFrame???
Building, Window and Background do no painting of there own (from the content of Swing), you are simply calling the draw method. There is no need to extend from JFrame or JApplet, they're adding no benefit to your program and are simply confusing the issues.
You should, only very rarely, need to override paint on a top level container like JFrame. You are better off using something like JPanel and override the paintComponent method, if for no other reason, they (top level containers) aren't double buffered.
I would move the logic for Skyline into a JPanel and then add it to a JFrame for displaying - IMHO
UPDATED
I've gone through the code and updated it to work the (basic) way I think it should and found a couple of other things along the way...
This this is a bad idea...
public void mouseMoved(MouseEvent me) {
Graphics g = getGraphics();
mX = (int) me.getPoint().getX();
mY = (int) me.getPoint().getY();
update(g);
//repaint();
}
There should never be any need for you to call update(Graphics), besides, the Graphics context you got is simply a snap shot of the last repaint. This will drastically slow you painting process any way, as it is repeatedly calling paint.
So, this is my take...
public class Skyline extends JFrame {
private int num = 0;
private Building bldg1 = new Building(305, 110, 30);
private Building bldg2 = new Building(380, 125, 170);
private Building bldg3 = new Building(245, 200, 325);
private Building bldg4 = new Building(470, 170, 555);
private Building bldg5 = new Building(395, 200, 755);
private Background bg = new Background();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
Skyline f = new Skyline();
f.setSize(1017, 661); //Sets size of window
f.setTitle("Skyline"); //Sets title of window
f.setVisible(true);
}
});
}
public Skyline() {
setLayout(new BorderLayout());
add(new SkyLinePane());
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public class SkyLinePane extends JPanel {
private Image mImage; //Image buffer
private boolean painting = false;
private int mX, mY; //Mouse cooddinates
public SkyLinePane() {
addMouseMotionListener(new MouseAdapter() {
#Override
public void mouseMoved(MouseEvent me) {
mX = (int) me.getPoint().getX();
mY = (int) me.getPoint().getY();
repaint();
}
});
}
protected void updateBuffer() {
if (!painting && mImage == null) {
painting = true;
new BackgroundPainter(this).execute();
}
}
//====================================BUFFER CODE========================================
#Override
public void paintComponent(Graphics g) {
Dimension d = getSize();
if (mImage != null) {
g.drawImage(mImage, 0, 0, null);
} else {
updateBuffer();
}
g.setColor(Color.RED);
g.drawOval(mX - 5, mY - 5, 10, 10);
}
//=======================================================================================
protected void setBackground(Image image) {
mImage = image;
painting = false;
repaint();
}
}
public class BackgroundPainter extends SwingWorker<Image, Image> {
private SkyLinePane skyLinePane;
public BackgroundPainter(SkyLinePane skyLinePane) {
this.skyLinePane = skyLinePane;
}
#Override
protected Image doInBackground() throws Exception {
Dimension d = skyLinePane.getSize();
Image backgroundBuffer = null;
if (d.width > 0 && d.height > 0) {
System.out.println("Paint offscreen...");
backgroundBuffer = createImage(d.width, d.height);
Graphics offG = backgroundBuffer.getGraphics();
offG.setColor(getBackground());
offG.fillRect(0, 0, d.width, d.height);
//Save frame to buffer
paintOffscreen(offG);
offG.dispose();
System.out.println("Done Paint offscreen...");
}
return backgroundBuffer;
}
#Override
protected void done() {
try {
skyLinePane.setBackground(get());
} catch (ExecutionException exp) {
exp.printStackTrace();
} catch (InterruptedException exp) {
exp.printStackTrace();
}
}
public void paintOffscreen(Graphics page) {
//Draws the background
bg.draw(page);
//Moving square
num++;
if (num > 1200) {
num = 0;
}
page.setColor(Color.yellow);
page.fillRect(num, 100, 100, 100);
//Draws the buildings
bldg1.draw(page);
bldg2.draw(page);
bldg3.draw(page);
bldg4.draw(page);
bldg5.draw(page);
}
}
//=======================================================================================
public class Windows {
private Random gen = new Random();
private int height, width, locX;
private int onOff = 0;
public Windows() {
height = 305;
width = 110;
locX = 30;
}
public Windows(int height, int width, int locX) {
this.height = height;
this.width = width;
this.locX = locX;
}
public void draw(Graphics page) {
page.setColor(Color.darkGray);
page.fillRect(locX, 550 - height, width, height);
for (int i = 550 - height + 5; i < 550; i += 15) {
for (int x = locX + 5; x < locX + width; x += 15) {
onOff = gen.nextInt(2);
if (onOff == 0) {
page.setColor(Color.black);
} else {
page.setColor(Color.yellow);
}
page.fillRect(x, i, 10, 10);
}
}
}
}
public class Building {
private int height, width, locX;
private int onOff;
private Windows windows1;// = new Windows(height, width, locX);
public Building() {
height = 305;
width = 110;
locX = 30;
windows1 = new Windows(height, width, locX);
}
public Building(int height, int width, int locX) {
this.width = width;
this.height = height;
this.locX = locX;
windows1 = new Windows(height, width, locX);
}
public void draw(Graphics page) {
page.setColor(Color.darkGray);
page.fillRect(locX, 550 - height, width, height);
windows1.draw(page);
}
}
public class Background {
private int height, width;
public Background() {
height = 400;
width = 2000;
}
public Background(int height, int width) {
this.height = height;
this.width = width;
}
public void draw(Graphics page) {
//Draws the sky
page.setColor(Color.cyan);
page.fillRect(0, 0, 2000, 2000);
//Draws the grass
page.setColor(Color.green);
page.fillRect(0, 500, width, height);
}
}
}
Basically, I moved the core rendering of the skyling to it's own panel and used JComponent#paintComponent to render the skyline.
I employed a SwingWorker to off load the rendering of the backing buffer to another thread, allowing the UI to remain responsive while the backing buffer was rendered.

Categories

Resources