I want to see all the Points one after another but I see only able to see 1
point. What shold I change to see all the Points ?
In the System.out you can see 10 times "set" and then 2 times
"paintComponent". what should I change that after each time set is
called it change the "paintComponente" ?
==================================================================================
public class exampe extends JPanel
{
int x;
int y;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.fillOval(x-2,y-2,4,4);
System.out.println("paintComponent");
}
public void set(int X, int Y)
{
x = X;
y = Y;
System.out.println("set");
super.repaint();
}
public static void main(String args[])
{
int e=1;
JFrame frame = new JFrame("TEST");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
exampe ex= new exampe();
JScrollPane scroll = new JScrollPane(ex);
frame.getContentPane().add(scroll);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
for(int i=0; i< 10; ++i)
ex.set(e+i,e+i);
}
}
*SIMPLE EXPLANATION AS TO WHY YOU COULD ONLY SEE THE LAST UPDATE : *
A quote taken from Filthy Rich Clients by Chet Haase and Romain Guy
It is important to note that repaint requests get “coalesced,” or combined.
So, for example, if you request a repaint and there is already one on the
queue that has not yet been serviced, then the second request is ignored
because your request for a repaint will already be fulfilled by the earlier
request. This behavior is particularly helpful in situations where many
repaint requests are being generated, perhaps by very different situations
and components, and Swing should avoid processing redundant requests and
wasting effort.
Try your hands on this, and ask what is not clear to you :
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class PointsExample
{
private CustomPanel contentPane;
private Timer timer;
private int x = 1;
private int y = 1;
private ActionListener timerAction = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
contentPane.set(x, y);
x++;
y++;
if (x == 450)
timer.stop();
}
};
/*
* This is just JFrame, that we be
* using as the Base for our Application.
* Though here we are calling our
* JPanel (CustomPanel), whose
* paintComponent(...) method, we had
* override.
*/
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("Locate Mouse Position");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new CustomPanel();
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
timer = new Timer(100, timerAction);
timer.start();
}
public static void main(String\u005B\u005D args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new PointsExample().createAndDisplayGUI();
}
});
}
}
class CustomPanel extends JComponent
{
private int x;
private int y;
public void set(int a, int b)
{
x = a;
y = b;
repaint();
}
#Override
public Dimension getPreferredSize()
{
return (new Dimension(500, 500));
}
#Override
public void paintComponent(Graphics g)
{
g.clearRect(0, 0, getWidth(), getHeight());
Graphics2D g2 =(Graphics2D) g;
g2.fillOval(x, y, 4, 4);
}
}
Here is the code, that will allow you to have a look at your points while iterating inside a for loop, though this approach is highly discouraged, for many cons associated with it. Though try your hands on this instead of calling repaint() call paintImmediately(int ...) or paintImmediately(Rectangle rect)
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class PointsExample
{
private CustomPanel contentPane;
private Timer timer;
private int x = 1;
private int y = 1;
/*
* This is just JFrame, that we be
* using as the Base for our Application.
* Though here we are calling our
* JPanel (CustomPanel), whose
* paintComponent(...) method, we had
* override.
*/
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("Locate Mouse Position");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new CustomPanel();
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
for (int i = 0; i < 500; i++)
{
contentPane.set(x, y);
x++;
y++;
if (x == 450)
break;
}
}
public static void main(String\u005B\u005D args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new PointsExample().createAndDisplayGUI();
}
});
}
}
class CustomPanel extends JComponent
{
private int x;
private int y;
public void set(int a, int b)
{
x = a;
y = b;
paintImmediately(0, 0, getWidth(), getHeight());
}
#Override
public Dimension getPreferredSize()
{
return (new Dimension(500, 500));
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.fillOval(x, y, 4, 4);
}
}
1: first line of paintComponent() should be your super.paintComponent()
2: why are you calling super.repaint(), make it simply repaint()
Your Drow should be like this.
public class drow extends JPanel {
...........
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 =(Graphics2D) g;
}
public void set_list(LinkedList <point> p){
Points =p;
repaint();
}
try with this.
i hope this is simply a structure, your paintComponent() isn't drawing anything.
EDIT
public void set_list(LinkedList <point> p){
Points =p;
System.out.println("set_ist");// 1:First this line will be displayed then..
repaint();//2: Then this is called, which in turn calls your `paintComponent()`
}
Now when your paintComponent() is called it has
system.out.println("paintComponent");
//3: so now this will be displayed.
Where is the problem here?
EDIT- SWING TIMER
Your code was ok, but the function processing is way faster than GUI updation, thats why you were unable to see the changes in front of you. The way you were doing, of calling thread.sleep() between function calls to slow down it's call, was not a good approach. For any timing thing's in swing, use swing timer, i changed your code for swing timer.
Using Swing Timer:
public class exampe extends JPanel implements ActionListener {
int x;
int y;
int temp = 0;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.fillOval(x - 2, y - 2, 4, 4);
}
public void set(int X, int Y) {
x = X;
y = Y;
}
public static void main(String args[]) {
JFrame frame = new JFrame("TEST");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
exampe ex = new exampe();
JScrollPane scroll = new JScrollPane(ex);
frame.getContentPane().add(scroll);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Timer PointTimer = new Timer(1000, ex);
PointTimer.setInitialDelay(1000);
PointTimer.start();
System.out.println("started");
}
#Override
public void actionPerformed(ActionEvent e) {
// set(rand.nextInt(350), rand.nextInt(350));
set(temp+10,temp+10);
temp=temp+2;
repaint();
}
}
Related
EDIT: First question solved, see Roberto Attias 's answer and maybe read the comment . Still there's the second issue.
I have to do a small 2D game in Java and I want to stick to AWT as much as possible, ( so no SWING nor Java2D unless absolutely necessary).
I have a window and I can draw an image on it but there's two issues.
First I can't draw more than one Image. In fact with some of my test when in debug I can see that my program will draw my two images only to delete them and re-draw the first image.
Second, that first image which is re-drawn is not at the coordinate it should ( its slightly on the left and on below )
For now I have something like that:
public class AwtManager {
private Frame frame;
private Panel panel;
public AwtManager(){
frame = new Frame("a");
frame.setSize(800,600);
frame.setLocation(100, 100);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEnvent){
System.exit(0);
}
});
panel = new Panel();
// here I tried to setBounds to solve my second issue, it didn't work
//panel.setBounds(0, 0, 800, 600);
frame.add(panel);
frame.setVisible(true);
}
This part open my windows and works quite nicely but my second issue seems to be caused by the borders of my frame / panel so there might be some changement to do here.
public void showMytwoImagesFFS(ArrayList<ImageComponent> images){
for (int i = 0; i < images.size(); i++) {
panel.add(images.get(i);
//And this is where I'm lost
}
// or here maybe
frame.setVisible(true);
}
}
In this second part I tried every combination of Component.paint(g), Component.update(g), Component.repaint(), Component.setVisible(true) I could think of.
My object ImageComponent is simply this:
public class ImageComponent extends Component {
private static final long serialVersionUID = 1L;
private BufferedImage img;
private int x;
private int y;
public ImageComponent(String path,int x, int y){
try{
img = ImageIO.read(new File(path));
this.x = x;
this.y = y;
}
catch (IOException e){
e.printStackTrace();
}
}
This function getPreferredSize() disturbs me like hell, it should return the preferred size of the ImageComponent but apparently I have to put the size of my frame/ panel otherwise it won't work.
public Dimension getPreferredSize(){
return new Dimension(800,600);
}
And finally my paint, update, repaint:
public void paint(Graphics g){
g.drawImage(img, x, y,null);
}
public void update(Graphics g){
super.update(g);
}
public void repaint(){
this.getGraphics().drawImage(img, x, y, null);
}
}
I highly doubt that those three look like what they should but I find the documents on the subject very hard to understand.
So pleas, could you help me with those issues, and by the way if you know how Component.setVisible(boolean) works i would like to know because in debug mod, this function made me loose some hair.
EDIT:
Here's a screenshot of my window knowing that I asked for two red square (there are Images not Rectangle), one a 0,0, the other at 200, 200.
EDIT 2:
Here's a fully runnable code (i think):
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class AwtManager {
private Frame frame;
private Panel panel;
public static void main(String args[]) {
new AwtManager();
ArrayList<ImageComponent> images = new ArrayList<>();
images.add(new ImageComponent("myimage.jpg", 0, 0));
images.add(new ImageComponent("myimage.jpg", 200, 200));
showMytwoImagesFFS(images);
}
public AwtManager(){
frame = new Frame("a");
frame.setSize(800,600);
frame.setLocation(100, 100);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEnvent){
System.exit(0);
}
});
panel = new Panel();
frame.add(panel);
frame.setVisible(true);
}
public void showMytwoImagesFFS(ArrayList<ImageComponent> images){
for (int i = 0; i < images.size(); i++) {
panel.add(images.get(i));
}
frame.setVisible(true);
}
}
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
public class ImageComponent extends Component {
private static final long serialVersionUID = 1L;
private BufferedImage img;
private int x;
private int y;
public ImageComponent(String path,int x, int y){
try{
img = ImageIO.read(new File(path));
this.x = x;
this.y = y;
}
catch (IOException e){
e.printStackTrace();
}
}
public Dimension getPreferredSize(){
return new Dimension(800,600);
}
public void paint(Graphics g){
g.drawImage(img, x, y,null);
}
public void update(Graphics g){
super.update(g);
}
public void repaint(){
this.getGraphics().drawImage(img, x, y, null);
}
}
Just a quick thought, but do you know the visibility of each of your images in that ArrayList? I.e.
public void showMytwoImagesFFS(ArrayList<ImageComponent> images){
for (int i = 0; i < images.size(); i++) {
images.get(i).setVisible(true);//is the visibility actually true?
panel.add(images.get(i);
}
// or here maybe
frame.setVisible(true);
}
Also, do you have screenshots of how your program looks at the moment? With gui problems, I find they're easier to solve if I can see what's going on.
This is your code, slightly modified to run. In the future, please post fully runnable examples.
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class AwtManager {
private Frame frame;
private Panel panel;
public static void main(String args[]) {
new AwtManager();
}
public AwtManager(){
frame = new Frame("a");
frame.setSize(800,600);
frame.setLocation(100, 100);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEnvent){
System.exit(0);
}
});
panel = new Panel();
// here I tried to setBounds to solve my second issue, it didn't work
//panel.setBounds(0, 0, 800, 600);
frame.add(panel);
ArrayList<ImageComponent> images = new ArrayList<>();
images.add(new ImageComponent("myimage.jpg", 0, 0));
showMytwoImagesFFS(images);
frame.setVisible(true);
}
public void showMytwoImagesFFS(ArrayList<ImageComponent> images){
for (int i = 0; i < images.size(); i++) {
panel.add(images.get(i));
}
}
}
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
public class ImageComponent extends Component {
private static final long serialVersionUID = 1L;
private BufferedImage img;
private int x;
private int y;
public ImageComponent(String path,int x, int y){
try{
img = ImageIO.read(new File(path));
this.x = x;
this.y = y;
}
catch (IOException e){
e.printStackTrace();
}
}
public Dimension getPreferredSize(){
return new Dimension(800,600);
}
public void paint(Graphics g){
g.drawImage(img, x, y,null);
}
public void update(Graphics g){
super.update(g);
}
public void repaint(){
this.getGraphics().drawImage(img, x, y, null);
}
}
Let say I have an image. I put the image in a JPanel and add the JPanel inside a JFrame. The image moves from the bottom part of the frame to top of the frame while its size is also decreased using AffineTransform. The variable changes using thread.
So here's the following code:
public class SplashScreen extends JFrame{
Image img1;
int w=1,h=1;
int x=0,y=0;
Thread th = new Thread(new Runnable() {
#Override
public void run() {
while(true){
w-=0.05;
h-=0.05;
y-=2;
x+=1;
if(y==-100){
new MainMenu_BlueJay().setVisible(true);
dispose();
}
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
Logger.getLogger(SplashScreen.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
JPanel p = new JPanel();
public SplashScreen(){
setLayout(new BorderLayout());
p.setPreferredSize(new Dimension(900,600));
p.setBackground(Color.black);
p.setLayout(new GridLayout());
add(p);
setTitle("BlueJay");
setSize(900,600);
getContentPane().setBackground(Color.black);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
th.start();
requestFocus();
setFocusable(true);
}
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
img1 = new ImageIcon("images/Intro/BJ Production 2013.png").getImage();
AffineTransform at = new AffineTransform();
at.scale(w,h);
g2d.setTransform(at);
g2d.drawImage(img1, x, y, p);
}
public static void main(String[] args) {
new SplashScreen();
}
However what I get from code above is only black screen. What's the matter? Anyway, If I don't use the AffineTransform function (just move it from bottom to top), the image is shown and moves BUT the frame is flickered (blinking) rapidly.
Any idea to solve this problem so I could move the image while decrease its size and also solve the flickered/rapid blinking frame?
You should not override the paint method of the JFrame. If you want to paint anything, you should paint this in a class that extends JPanel, where you override the paintComponent method.
You should NOT load the image in the painting method. This is horribly inefficient. You should load the image only ONCE, probably in a constructor.
You should not call Graphics2D#setTransform(). Have a look at the JavaDoc at http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html#setTransform%28java.awt.geom.AffineTransform%29 , which explicitly states
WARNING: This method should never be used to apply a new coordinate transform on top of an existing transform
You should think about your w and h values. Should they be the size of the image that is painted, or used as scaling factors for the image? Setting them as the scaling factors of an AffineTransform will NOT have the effect of scaling an image to the desired size. At the moment, they are declared as int values, so something like w-=0.05 does not really make sense anyhow.
You should have a clear idea of how you are going to describe the animation that the image should perform.
One could possibly summarize this:
You should not write code and assume it is "correct" only because there are no compilation errors ;-)
However, the following snippet may be a first step towards your goal:
package stackoverflow;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class SplashScreen extends JFrame
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
new SplashScreen();
}
});
}
private PaintPanel paintPanel;
public SplashScreen()
{
setTitle("BlueJay");
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setBackground(Color.BLACK);
getContentPane().setLayout(new BorderLayout());
paintPanel = new PaintPanel();
getContentPane().add(paintPanel, BorderLayout.CENTER);
setSize(900,600);
setLocationRelativeTo(null);
setFocusable(true);
requestFocus();
setVisible(true);
startAnimation();
}
void startAnimation()
{
Thread thread = new Thread(new Runnable()
{
int x = 100;
int y = 100;
int w = 0;
int h = 0;
#Override
public void run()
{
try
{
Thread.sleep(500);
}
catch (InterruptedException ex)
{
Thread.currentThread().interrupt();
return;
}
while (true)
{
if (y == 200)
{
// new MainMenu_BlueJay().setVisible(true);
dispose();
}
x += 2;
y += 1;
w += 1;
h += 1;
paintPanel.setImageCoordinates(x, y, w, h);
repaint();
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
Thread.currentThread().interrupt();
return;
}
}
}
});
thread.start();
}
}
class PaintPanel extends JPanel
{
private final Image image;
private int imageX, imageY;
private int imageW, imageH;
PaintPanel()
{
image = new ImageIcon("Clipboard02.jpg").getImage();
imageX = 0;
imageY = 0;
imageW = 0;
imageH = 0;
}
void setImageCoordinates(int imageX, int imageY, int imageW, int imageH)
{
this.imageX = imageX;
this.imageY = imageY;
this.imageW = imageW;
this.imageH = imageH;
repaint();
}
#Override
protected void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D) gr;
float scalingX = (float) imageW / image.getWidth(null);
float scalingY = (float) imageH / image.getHeight(null);
g.scale(scalingX, scalingY);
int ix = (int)(imageX / scalingX);
int iy = (int)(imageY / scalingY);
g.drawImage(image, ix, iy, null);
}
}
Don't paint on top-level containers like JFrame. Instead use a JPanel and override its paintComponent method and call super.paintComponent
No need to call Thread.sleep(). Instead Use a javax.swing.Timer.
Run your program from the EDT
Don't create a new ImageIcon in the paint method. It will create new ImageIcon object every time repaint() is called. Instead, instantiate it in the constructor.
Here's a refactor of the code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SplashScreen extends JFrame {
Image img1;
int w = 900, h = 600;
int x = 0, y = 0;
public SplashScreen() {
setLayout(new BorderLayout());
add(new MyPanel());
setTitle("BlueJay");
setSize(900, 600);
getContentPane().setBackground(Color.black);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
requestFocus();
setFocusable(true);
}
private class MyPanel extends JPanel {
public MyPanel() {
img1 = new ImageIcon(SplashScreen.class.getResource("/resources/stackoverflow5.png")).getImage();
setBackground(Color.black);
setLayout(new GridLayout());
Timer timer = new Timer(20, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
w -= 5;
h -= 5;
y -= 2;
x += 1;
if (y == -250) {
new MainMenu_BlueJay().setVisible(true);
dispose();
}
repaint();
}
});
timer.start();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//AffineTransform at = new AffineTransform();
// at.scale(w, h);
// g2d.setTransform(at);
g2d.drawImage(img1, x, y, w, h, this);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(900, 600);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new SplashScreen();
}
});
}
}
I'm not too familiar with Graphics2D so I commented out the AffirmTransformation stuff, but fixed your other problems.
I have a problem, when i call repaint() on JDialog, I see nothing on the screen, but when i move the JDialog by my self, I see what i wanted to paint.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class dude extends JFrame {
private static int cnt = 0;
public dude() {
super("ff");
makeFrame();
}
public void makeFrame() {
new Dialog(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setSize(400, 400);
setVisible(true);
}
private class Dialog extends JDialog {
public Dialog(JFrame frame) {
super(frame, "ff", true);
makeFrame();
}
public void makeFrame() {
getContentPane().addMouseListener(new M(this));
setDefaultCloseOperation(HIDE_ON_CLOSE);
pack();
setLocation(200, 200);
setSize(400, 400);
setVisible(true);
}
private class M extends MouseAdapter {
private JDialog dialog;
public M(JDialog dialog) {
this.dialog = dialog;
}
public void mouseClicked(MouseEvent e) {
P p = new P(e.getX(), e.getY());
p.repaint();
dialog.add(p);
}
private class P extends JPanel {
private int x, y;
public P(int x, int y) {
this.x = x;
this.y = y;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.drawOval(x, y, 10, 10);
}
/*public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawOval(x,y,10,10);
}*/
}
}
}
}
That's weirdest piece of code I've seen for a while, but. You're immeditate problem is with you mouseClicked event...
Replace your p.repaint call with a call to the dialogs revalidate method.
P p = new P(e.getX(), e.getY());
dialog.add(p);
dialog.revalidate();
Your repaint method would have done nothing any way, it was being called before you panel was realized (connected to the screen)
Seems like you need to look into the coding style you adhering to. Though, leave that for latter part, simply add this method to your M Class
public void setValues(int x, int y)
{
this.x = x;
this.y = y;
repaint();
}
And make p an Instance Variable of your Dialog Class. And inside your mouseClicked() method, simply call this method. And remove the constructor part, since you initializing a new JPanel for each drawing which I guess is not good in any sense. When you simply can draw the new thingy on the same JPanel
It seems like if my graphics object were final, as the error says it should be that I would never be able to change it. I have been reading about assigning variables to final variables before using them in my timer loop in order to get around this, but I don't even know how to begin to approach that for a graphics object. Would I need to copy the final graphics object back to the normal graphics object? Here is some code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.util.ArrayList;
public class Test extends JPanel{
abstract class graphic {
public Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public int[] location = new int[] {screenSize.width/2,screenSize.height/2};
}
public class gladiator extends graphic {
void draw(Graphics g) {
g.setColor(Color.green);
g.fillArc(location[0], location[1], 100, 100, 45, 90);
g.setColor(Color.black);
g.fillArc((location[0]+50-10),(location[1]+50-10), 20, 20, 0, 360);
}
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
gladiator[] gladiator = new gladiator[2];
ArrayList<gladiator> gladiatorList = new ArrayList<gladiator>();
for (int a =0; a < 2; a++) {
final gladiator[a] = new gladiator();
final gladiatorList.add(gladiator[a]);
}
new Timer(200, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
for (int a = 0; a < gladiatorList.size(); a++) {
gladiator[a].draw(g);
}
repaint();
System.out.println("repainting");
}
}).start();
}
public void setLocation(int x, int y){
//this.location[0] = x;
//this.location[1] = y;
}
public static void main(String[] args){
JFrame jf=new JFrame();
jf.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
jf.setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
jf.add(new Test());
jf.pack();
jf.setVisible(true);
}
}
This is the bit which returns that pretty much all of the line inside the for loop should be final.
new Timer(200, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
for (int a = 0; a < gladiatorList.size(); a++) {
gladiator[a].draw(g);
}
repaint();
System.out.println("repainting");
}
}).start();
Thanks!
To use a local variable inside an anonymous class defined in the same method, you must make the local variable final.
This doesn't prevent you from modifying the object to which a reference variable points.
In your case, your anonymous class is using g, gladiator and gladiatorList. So mark all these final:
protected void paintComponent( final Graphics g) {
...
final gladiator[] gladiator = new gladiator[2];
final ArrayList<gladiator> gladiatorList = new ArrayList<gladiator>();
You really shouldn't have the timer in your paintComponent, it fires off a new timer whenever the OS feels like painting the component. You can see this by simply changing your repainting sysout to System.out.println("repainting in: " + this);
As for the finality of the Graphics variable:
final Graphics2D g2d = (Graphics2D) g.create();
Use g2d inside the timer.
Edit:
A full example:
public class ExampleAnimationOfMyStuff extends JPanel {
MovingRectangle[] rectangles = new MovingRectangle[20];
public ExampleAnimationOfMyStuff() {
for (int i = 0; i < rectangles.length; i++) {
rectangles[i] = new MovingRectangle();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Animated rectangles");
ExampleAnimationOfMyStuff anime = new ExampleAnimationOfMyStuff();
frame.getContentPane().add(anime);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
anime.animate();
frame.setVisible(true);
}
#Override
#Transient
public Dimension getPreferredSize() {
return new Dimension(1000, 1000);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (MovingRectangle rectangle : rectangles) {
g.setColor(rectangle.color);
g.fillRect(rectangle.x, rectangle.y, rectangle.width,
rectangle.height);
}
}
public void animate() {
new Timer(100, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
for (MovingRectangle rectangle : rectangles) {
rectangle.tick();
}
repaint();
System.out.println("repainting");
}
}).start();
}
public static class MovingRectangle extends Rectangle {
public static Random random = new Random();
int speedX, speedY;
Color color;
public void tick() {
if (getX() + speedX > 1000 || getX() + speedX < 0) {
speedX *= -1;
}
if (getY() + speedY > 1000 || getY() + speedY < 0) {
speedY *= -1;
}
setRect(getX() + speedX, getY() + speedY, getWidth(), getHeight());
}
public MovingRectangle() {
super(random.nextInt(1000), random.nextInt(1000), random
.nextInt(40), random.nextInt(40));
this.speedX = (random.nextDouble() > 0.5) ? 4 : -4;
this.speedY = (random.nextDouble() > 0.5) ? 4 : -4;
this.color = new Color(random.nextInt(256), random.nextInt(256),
random.nextInt(256));
}
}
}
The above code seperates the timer from the paintcomponent using an array of custom objects (like you have), no need to declare things final and also removes the flickering you experienced (due to new timers firing off). It paints pretty rectangles on the screen that move about ;)
If you make your variable final, it means that that variable will always be a reference to the same object instance. It does not mean that the contents of the object instance cannot be changed. You cannot assign a new reference to that variable, but you can do anything you want to the instance itself (call methods that might modify its state, read/write the fields, etc.).
I have the following program which has some very strange and unwanted behavior when it runs. Its supposed to have two buttons, "Start" and "Stop, but when I click "Start" another button shows up right below "Start". Here's a print screen of what I'm talking about:
What am I doing wrong and how do I fix this ugly problem?
Here's the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class TwoButtonsTest {
JFrame frame;
Timer timer;
boolean isClicked;
public static void main(String[] args) {
TwoButtonsTest test = new TwoButtonsTest();
test.go();
}
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
JButton startButton = new JButton("Start");
startButton.addActionListener(new StartListener());
JButton stopButton = new JButton("Stop");
stopButton.addActionListener(new StopListener());
final DrawPanel myDraw = new DrawPanel();
frame.getContentPane().add(BorderLayout.CENTER, myDraw);
frame.getContentPane().add(BorderLayout.NORTH, startButton);
frame.getContentPane().add(BorderLayout.SOUTH, stopButton);
frame.setVisible(true);
timer = new Timer(50, new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
myDraw.repaint();
}
});
}
class StartListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//needs to be implemented
if(!isClicked) {
}
isClicked = true;
timer.start();
}
}
class StopListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//needs to be implemented
timer.stop();
isClicked = false;
}
}
class DrawPanel extends JPanel {
public void paintComponent(Graphics g) {
int red = (int)(Math.random()*256);
int blue = (int)(Math.random()*256);
int green = (int)(Math.random()*256);
g.setColor(new Color(red, blue, green));
Random rand = new Random();
// following 4 lines make sure the rect stays within the frame
int ht = rand.nextInt(getHeight());
int wd = rand.nextInt(getWidth());
int x = rand.nextInt(getWidth()-wd);
int y = rand.nextInt(getHeight()-ht);
g.fillRect(x,y,wd,ht);
}
} // close inner class
}
Also I'm trying to get the Start button to do two things. One is to of course start the animation but when the Stop button is pressed and I press Start again, I want it to clean the screen so to speak and start the animation again a new. Any tips on that?
You do not call super.paintComponent(Graphics g) in overriden paintComponent(..) method which you should in order to honor the paint chain and thus the painting of other components.
This call should also be the first call within the method:
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//do painting here
}
A probem might arise that drawings are not persistent. You must than have a way to store drawings and redraw every time. The most common is an ArrayList which will hold objects to be drawn (thus you cann add to the list remove etc), you would than iterate over the list and redraw each object in paintComponent. See my answer here for an example.
Also please remember to create and manipulate Swing components on Event Dispatch Thread :
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
//create UI and components here
}
});
Dont call setSize(..) on JFrame rather override getPreferredSize() of JPanel and return an appropriate height which fits all components, than call JFrame#pack() before setting JFrame visible (but after adding all components).
No need for getContentPane().add(..) as of Java 6+ add(..) defaults to contentPane
Do not re declare Random i.e Random r=new Random() each time paintComponent is called as this will make the distributions of the values less random rather initiate it once when class is created and call methods on the instance
Here is the fixed code (with above fixes implemented):
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
public class TwoButtonsTest {
JFrame frame;
Timer timer;
boolean isClicked;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
TwoButtonsTest test = new TwoButtonsTest();
test.go();
}
});
}
final DrawPanel myDraw = new DrawPanel();
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton startButton = new JButton("Start");
startButton.addActionListener(new StartListener());
JButton stopButton = new JButton("Stop");
stopButton.addActionListener(new StopListener());
frame.add(myDraw, BorderLayout.CENTER);
frame.add(startButton, BorderLayout.NORTH);
frame.add(stopButton, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
timer = new Timer(50, new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
myDraw.repaint();
}
});
}
class StartListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
//needs to be implemented
if (!isClicked) {
}
myDraw.clearRects();
isClicked = true;
timer.start();
}
}
class StopListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//needs to be implemented
timer.stop();
isClicked = false;
}
}
class DrawPanel extends JPanel {
private ArrayList<MyRectangle> rects = new ArrayList<>();
private Random rand = new Random();
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
addRect();
for (MyRectangle r : rects) {
g.setColor(r.getColor());
g.fillRect(r.x, r.y, r.width, r.height);
}
}
#Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
public void clearRects() {
rects.clear();
}
public void addRect() {
// following 4 lines make sure the rect stays within the frame
int ht = rand.nextInt(getHeight());
int wd = rand.nextInt(getWidth());
int x = rand.nextInt(getWidth() - wd);
int y = rand.nextInt(getHeight() - ht);
int red = (int) (Math.random() * 256);
int blue = (int) (Math.random() * 256);
int green = (int) (Math.random() * 256);
rects.add(new MyRectangle(x, y, wd, ht, new Color(red, blue, green)));
}
} // close inner class
}
class MyRectangle extends Rectangle {
Color color;
public MyRectangle(int x, int y, int w, int h, Color c) {
super(x, y, w, h);
this.color = c;
}
public Color getColor() {
return color;
}
}
I wish I could offer a solution, but as of yet I haven't found one. I can tell you the root of the "problem" here lies in the way you are drawing the Center section of your BorderLayout. You are overriding the whole paintComponent() function for this program and having whatever it creates put into the Center of your BoarderLayout. In this case, each time you click a button, the program calls the repaint to draw the image of a clicked button, but since you have also added ANY of the drawn objects to the Center panel, it also is drawn there. Since this specific repaint doesn't specify a location, it goes in the upper left corner.
I fixed your button problem on my Windows XP computer by invoking SwingUtilities.
I formatted your Java code.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class TwoButtonsTest implements Runnable {
JFrame frame;
Timer timer;
boolean isClicked;
public static void main(String[] args) {
SwingUtilities.invokeLater(new TwoButtonsTest());
}
#Override
public void run() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
JButton startButton = new JButton("Start");
startButton.addActionListener(new StartListener());
JButton stopButton = new JButton("Stop");
stopButton.addActionListener(new StopListener());
final DrawPanel myDraw = new DrawPanel();
frame.getContentPane().add(BorderLayout.CENTER, myDraw);
frame.getContentPane().add(BorderLayout.NORTH, startButton);
frame.getContentPane().add(BorderLayout.SOUTH, stopButton);
frame.setVisible(true);
timer = new Timer(50, new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
myDraw.repaint();
}
});
}
class StartListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// needs to be implemented
if (!isClicked) {
}
isClicked = true;
timer.start();
}
}
class StopListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// needs to be implemented
timer.stop();
isClicked = false;
}
}
class DrawPanel extends JPanel {
#Override
public void paintComponent(Graphics g) {
int red = (int) (Math.random() * 256);
int blue = (int) (Math.random() * 256);
int green = (int) (Math.random() * 256);
g.setColor(new Color(red, blue, green));
Random rand = new Random();
// following 4 lines make sure the rect stays within the frame
int ht = rand.nextInt(getHeight());
int wd = rand.nextInt(getWidth());
int x = rand.nextInt(getWidth() - wd);
int y = rand.nextInt(getHeight() - ht);
g.fillRect(x, y, wd, ht);
}
} // close inner class
}
To clean the screen when you press the Start button, you're going to have to add some methods to your DrawPanel class.
Here's one way to do it.
class DrawPanel extends JPanel {
protected boolean eraseCanvas;
public void setEraseCanvas(boolean eraseCanvas) {
this.eraseCanvas = eraseCanvas;
}
#Override
public void paintComponent(Graphics g) {
if (eraseCanvas) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
} else {
int red = (int) (Math.random() * 256);
int blue = (int) (Math.random() * 256);
int green = (int) (Math.random() * 256);
g.setColor(new Color(red, blue, green));
Random rand = new Random();
// following 4 lines make sure the rect stays within the frame
int ht = rand.nextInt(getHeight());
int wd = rand.nextInt(getWidth());
int x = rand.nextInt(getWidth() - wd);
int y = rand.nextInt(getHeight() - ht);
g.fillRect(x, y, wd, ht);
}
}
} // close inner class