-
I'm new to JFrame and need some help
Basically, to familiarize myself with this class I'm doing my first project, which is drawing the Mendelbrot Set, but before that I have to handle much simpler things;
If I have a the class
public class Canvas extends JFrame
and the functions
Code :
public static void main(String args[])
{
System.out.println("Running Canvas.java");
Canvas canvas = new Canvas();
}
public Canvas()
{
setSize(width, height);
setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(Color.yellow);
g.fillRect(0,0, width, height);
}
Who calls the function paint() when I run Canvas.java? Is it re-called every time that it finishes? Because that's what seems to be happening with my code.
Thank you in advance for your help
-
Re: I'm new to JFrame and need some help
Quote:
Who calls the function paint()
What class is the paint() method in? If its in a GUI class, the JVM will call it every time the GUI needs to be drawn or after the code calls repaint().
Canvas is the name of a Java SE class. You should chose a different name.
If you are using swing classes, you should override the paintComponent() method, not the paint() method.
-
Re: I'm new to JFrame and need some help
I'm fresh out of highschool in terms of my java knowledge, so I don't know much yet
In your post I don't know what these terms are:
GUI class
JVM
Java SE
swing class
Also, which name should I give it? I'm used to Canvas because of the project I made in highschool
-
Re: I'm new to JFrame and need some help
Time to build up your vocabulary. Google is a good place to start
GUI class - a class that is displayed on the monitor like JFrame or JButton
JVM - Java Virtual Machine. The java program that runs the program
Java SE - The java Standard Edition packages. See the API doc:
Java Platform SE 6
swing class - The newer set of GUI classes (vs AWT)
I don't know how you can write a java program without seeing these terms.
Quote:
which name should I give it?
Use MyCanvas
-
Re: I'm new to JFrame and need some help
I might as well post all the code I have so far
Code :
package Mendelbrot;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
/**
*
* @author
*/
public class Canvas extends JFrame
{
int width=200;
int height=200;
int iterations=4;
Thread thread=new Thread();
public static void main(String args[])
{
System.out.println("Running Canvas.java");
Canvas canvas = new Canvas();
}
public Canvas()
{
setSize(width, height);
setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(Color.yellow);
g.fillRect(0,0, width, height);
int[][] grid=render(width, height);
for(int i=0;i<width;i++)
{
for(int j=0;j<height;j++)
{
if(grid[i][j]==0)
{
g.setColor(Color.WHITE);
}
if(grid[i][j]==1)
{
g.setColor(Color.BLACK);
}
g.fillRect(i,j,1,1); //fills a single pixel
}
}
}
/**
*
* @param c complex number
* @param n how many iterations are used to find the answer
* @return true if c is part of the mendelbrot set
*/
public boolean isMend(Z c,int n)
{
return isMend(c,n,c);
}
public boolean isMend(Z zn, int n, Z c)
{
// System.out.println(n);
if(n==0)
{
if(zn.getSize()<=2)
return true;
else
return false;
}
Z zn1=Z.sum(Z.multiply(zn,zn),c);
return isMend(zn1,n-1,c);
}
/**
*
* @param w width
* @param h height
* @return an image to be painted
*/
public int[][] render(int w, int h)
{
System.out.println("Render "+w+","+h);
int[][] grid=new int[w][h];
//Initialization of the grid
for(int i=0;i<w;i++)
{
for(int j=0;j<h;j++)
{
grid[i][j]=0;
}
}
//Give values
for(int i=0;i<w;i++)
{
for(int j=0;j<h;j++)
{
// -1<x<1 -1<y<1
int x=i/100-1;
int y=j/100-1;
Z c=new Z(x,y);
if(isMend(c,iterations))
{
grid[i][j]=1;
}
System.out.println("Finished rendering pixel ["+i+","+j+"]");
}
}
return grid;
}
// public void run()
// {
// while(true)
// {
// repaint();
// System.out.println("Repainting");
//
//
// try
// {
// thread.sleep(2000);
// }
// catch (InterruptedException ex)
// {
// Logger.getLogger(Canvas.class.getName()).log(Level.SEVERE, null, ex);
// }
// }
// }
}
Code :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Mendelbrot;
/**
*
* @author Alon Eitan
*/
public class Z //Imaginary Number
{
/**
* Real component
*/
public double re;
/**
* Imaginary component
*/
public double im;
/**
*
* @param a real component
* @param b imaginary component
*/
public Z(double a, double b)
{
re=a;
im=b;
}
public double getSize()
{
return re*re+im*im;
}
public double getAngle()
{
return Math.atan(im/re);
}
public static Z sum(Z a, Z b)
{
double re=a.re+b.re;
double im=a.im+b.im;
return new Z(re,im);
}
public static Z multiply(Z a,Z b)
{
double re=a.re*b.re-(a.im*b.im);
double im=a.re*b.im+a.im*b.re;
return new Z(re,im);
}
public double getReal()
{
return re;
}
public double getImaginary()
{
return im;
}
public void setReal(double re)
{
this.re = re;
}
public void setImaginary(double im)
{
this.im = im;
}
}
-
Re: I'm new to JFrame and need some help
What are your questions or problems?
-
Re: I'm new to JFrame and need some help
Then it's in a GUI class because I can see it on my screen. (which is the entire point of this program)
How does the JVM determine when the GUI needs to be drawn?
For some reason the paint() function is being called multiple times.
It also isn't drawing correctly, but that's a different issue (I think).
-
Re: I'm new to JFrame and need some help
My question is why my code calls the paint() function multiple times
-
Re: I'm new to JFrame and need some help
Quote:
why my code calls the paint() function multiple times
How do you know how many times the paint() method is called?
-
Re: I'm new to JFrame and need some help
I put a line in the start of the function paint()
System.out.println("Painting");
(not in the version I posted above)
and it prints that three times
edit: three times, not twice
-
Re: I'm new to JFrame and need some help
The java program calls the paint()method when it thinks the GUI needs to be redrawn or after the code has called the repaint() method.
Quote:
not in the version I posted
You should post the code you are executing when you ask questions about how it works. Posting an old version makes it harder to help.
-
Re: I'm new to JFrame and need some help
Right... never mind, I managed to solve my problem.
But I have a new one now regarding threads; I'm trying to activate two threads at the same time, one that repaints every half second and another that renders the image (so that I can see the image progressively getting rendered)
But for some reason, the moment I activate one thread, it stays in the thread's loop and doesn't do anything else.
Here are the threads' run() functions:
Code :
public void run()
{
while(true) //infinite loop
{
repaint();
try
{
repainter.sleep(500);
}
catch (InterruptedException ex)
{
Logger.getLogger(Canvas.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Code :
public void run()
{
render();
}
-
Re: I'm new to JFrame and need some help
Can you post more code so we can see the full context of what is happening.
Quote:
it stays in the thread's loop and doesn't do anything else.
Code :
while(true) //infinite loop
Yes that is what I'd expect.
-
Re: I'm new to JFrame and need some help
These are the relevant functions
Code :
public MyCanvas()
{
setSize(width, height);
setVisible(true);
ren.setWindow(-2.2,1.3,3.4,2.6);
ren.startRendering();
run();
}
Inside MyCanvas:
Code :
public void run()
{
while(true)
{
repaint();
try
{
repainter.sleep(500);
}
catch (InterruptedException ex)
{
Logger.getLogger(Canvas.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Inside Renderer:
Code :
public void run()
{
render();
}
-
Re: I'm new to JFrame and need some help
Do you have some code I can compile, execute and see the problem? Short pieces of code can leave out something important.
You normally do not call the run() method. That is done by the thread when it starts executing a Runnable.
-
1 Attachment(s)
Re: I'm new to JFrame and need some help
-
Re: I'm new to JFrame and need some help
Please post a small program (not the whole project) that compiles, executes and shows the problem.
I have not way to open a .rar file.
-
Re: I'm new to JFrame and need some help
Here:
Code :
import java.util.logging.Level;
import java.util.logging.Logger;
public class ThreadProblem
{
public static void main(String[]args)
{
ThreadProblem tp=new ThreadProblem();
}
public ThreadProblem()
{
Apples a=new Apples();
Oranges b=new Oranges();
}
public class Apples implements Runnable
{
Thread applesThread=new Thread();
public Apples()
{
run();
}
public void run()
{
while(true)
{
System.out.println("Apples");
try
{
applesThread.sleep(1000);
}
catch (InterruptedException ex)
{
Logger.getLogger(ThreadProblem.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public class Oranges implements Runnable
{
Thread orangesThread=new Thread();
public Oranges()
{
run();
}
public void run()
{
while(true)
{
System.out.println("Oranges");
try
{
orangesThread.sleep(2000);
}
catch (InterruptedException ex)
{
Logger.getLogger(ThreadProblem.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
I want it to print "Apples" every second and "Oranges" every two seconds, but instead it only prints "Apples" every second.
-
Re: I'm new to JFrame and need some help
Unrelated problem:
Assuming I have a variable that goes from 0 to 1, how do I use it and the class Color to get a rainbow spectrum? (0=red, 1=violet)
-
Re: I'm new to JFrame and need some help
See post #15:
You normally do not call the run() method. That is done by the thread when it starts executing a Runnable.
The run() method does not return to the constructor so it can return to its caller so that the next constructor can be called. To see, add lots of println statements to show when a method is entered and when it exits.
.
You need to create instances of the Thread class using instances of Apple and Oranges as its Runnable and then call the new thread's start() method.
-
Re: I'm new to JFrame and need some help
Actually what you have done is a wrong appreach. this ll help u i guess
public class ThreadProblem
{
public static void main(String[] arg)
{
new ThreadProblem();
}
public ThreadProblem()
{
Apples a=new Apples();
Oranges b=new Oranges();
}
public class Apples extends Thread
{
public Apples()
{
start();
}
public void run()
{
while(true)
{
System.out.println("Apples");
try
{
this.sleep(1000);
}
catch (InterruptedException ex)
{
System.out.println("into catch");
}
}
}
}
public class Oranges extends Thread
{
public Oranges()
{
start();
}
public void run()
{
while(true)
{
System.out.println("Oranges");
try
{
this.sleep(2000);
}
catch (InterruptedException ex)
{
System.out.println("into orange catch");
}
}
}
}
}
-
Re: I'm new to JFrame and need some help
I used your method and now I'm getting "java.lang.IllegalMonitorStateException"
-
Re: I'm new to JFrame and need some help
I managed to find how to make rainbow colors from
Create a rainbow with GD
But now I have two new problems (because of the exception I posted above)
1. My repainter thread doesn't work
2. I tried adding a MouseListener and a MouseMotionListener to (as of now) draw rectangles, and it isn't working properly either. It's drawing the rectangles too large:
This is the MouseListener and MouseMotionListener I used
Code :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Mendelbrot;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
/**
*
* @author Alon Eitan
*/
public class Zoomer implements MouseListener, MouseMotionListener
{
int sx,sy; //starting x y
int cx,cy; //current x y
Graphics g;
boolean dragging=false;
public Zoomer(Graphics g)
{
this.g=g;
}
public void mouseClicked(MouseEvent me)
{
}
public void mousePressed(MouseEvent me)
{
if(dragging)
return;
sx=me.getX();
sy=me.getY();
dragging=true;
}
public void mouseReleased(MouseEvent me)
{
dragging=false;
}
public void mouseEntered(MouseEvent me)
{
}
public void mouseExited(MouseEvent me)
{
}
public void mouseDragged(MouseEvent me)
{
if(!dragging)
return;
cx=me.getX();
cy=me.getY();
g.setColor(Color.white);
g.drawRect(sx, sy, cx, cy);
}
public void mouseMoved(MouseEvent me)
{
}
}
It's creating rectangles but not according to the true position of the mouse, but further right and downwards.
-
Re: I'm new to JFrame and need some help
Quote:
now I'm getting "java.lang.IllegalMonitorStateException"
Please post the code that is getting the exception and the full text of the error message.
-
Re: I'm new to JFrame and need some help
Code :
public class Repainter extends Thread
{
public void run()
{
while (true)
{
repaint();
try
{
this.wait(150); //<--- exception here
}
catch (InterruptedException ex)
{
Logger.getLogger(MyCanvas.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Code :
Exception in thread "Thread-2" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at Mendelbrot.MyCanvas$Repainter.run(MyCanvas.java:83)