This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Graphics 2d Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Graphics 2d Issue" Watch "Graphics 2d Issue" New topic
Author

Graphics 2d Issue

J Cleary
Greenhorn

Joined: Mar 24, 2006
Posts: 13
Please i need help. Is it possible to control the paint method of the graphics 2d object so that it does not automatically update drawings? i am trying to create an application that only draws/updates graphics when specifically requested.
The program i have at the moment just executes any commands but i want to slow it down and control it.
Please find my code below, seperated into 3 classes, any suggestions will be most appreciated. Thank you.


import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.font.*;
import java.util.*;
import javax.swing.text.*;
class framo extends JFrame{
pano pane=new pano();
Container c;


public framo(){
c =getContentPane();
c.add(pane);
c.repaint();
c.setVisible(true);

}

public void unPaused(){
pane.Unpause();
}

public void pauseItt(){
pane.pauseIt();
}

public void setColour(Color w){
pane.setColour(w);
//p.repaint();
}


}

//ANOTHER CLASS

import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.font.*;
import java.util.*;
import javax.swing.text.*;
class pano extends JPanel{
JPanel p = new JPanel();
JButton b=new JButton("po");
boolean pause=true;// boolean to pause displaying not working
public pano(){
p.add(b);
p.repaint();
p.setVisible(true);
}

public void setColour(Color d){
colour=d;
}

public void pauseIt(){
pause=false;
}

public void Unpause(){
pause=true;
}

public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(colour);
if (pause){
g2d.drawString("gg", 10, 10);
g2d.translate(55,340);
}
else{
repaint(); //not working!!
}
}
//TESTING CLASS
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class framoTest {
public static void main(String args[]){
framo f = new framo();
f.setVisible(true);
f.setSize(200,200);

f.setColour(Color.red);
f.pauseItt();//pause display so screen still has gg in red(not working)
f.setColour(Color.black);
f.unPaused();//resume display from here(not working)
}
}


}
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
It is better to set up your painting code to be ready to execute at any time (the system will call it whenever needed) and control it through state/instance variables that you can set/change inside your event code. Then call "repaint" from within the event code. Calling "repaint" within a "paint" method will cause an infinite loop: repaint calls paint calls repaint...
J Cleary
Greenhorn

Joined: Mar 24, 2006
Posts: 13
Thank you for the reply, i am most grateful. i will get back if it solves my issue or not. Thanks again.
J Cleary
Greenhorn

Joined: Mar 24, 2006
Posts: 13
Hello there, i have tried the suggested code and it works but it does not totally fit the requirements.
Would it be possible for the user to specify the color in the main method (static void main) eg
f.setColour(Color.pink);//then pause until button clicked
f.setColour(Color.yellow);//then pause until button clicked
f.setColour(Color.green);//then pause until button clicked
f.setColour(Color.gray);//then pause until button clicked

and the program waits for the change color button to be clicked before updating each of the colours?
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
You might find a way to do it like that. Another option is to set up the gui to allow the user to enter or select the color for the next change. You could use a JComboBox with colors to choose from or three JSpinners, one for each color component (r, g, b) or some JButtons to change each color component up or down of a JTextField for the user to enter a color name or color components which you can use to create a color. You might get some ideas from this page in the tutorial: A Visual Index to the Swing Components.
J Cleary
Greenhorn

Joined: Mar 24, 2006
Posts: 13
My thanks go to you, i have it working fine now. Thanks again i appreciate your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Graphics 2d Issue
 
Similar Threads
Having touble adding graphics to JPanel
jpane with scrollpane is not displaying
JSlider is not working.
Problem displaying panel in an applet
JSlider is not working.