How to retain Graphics after resizing java applet
Is there is any way of how to retain Graphics after resizing java applet.
Actually when i resize or minimize that applet all the graphics that i have made are disappeared.
I want to tell all of you in advance that I'm not using any paint function at all.
I have used local graphics variable for drawing shape on a panel in java applet.
The applet I'm creating is a simple Paint applet.
Re: How to retain Graphics after resizing java applet
How are you "drawing shapes on a panel" if you are not using any paint functions?
Re: How to retain Graphics after resizing java applet
I have told that i am using local graphics variable. and reference to that variable is taken from [Panelname].getgraphics() method
Re: How to retain Graphics after resizing java applet
I would guess that the Graphics are not being redrawn when the repaint method is called. The repaint method will get called every time you resize or minimize the component. By default, the repaint method clears the graphics on the component. So you may not be directly using the paint methods, but I would bet the Graphics object is. Perhaps simply creating a method that calls the draw method of the Graphics object, and putting:
Code java:
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
call-whatever-method-draws-the-graphics;
}
will solve your problem.
Re: How to retain Graphics after resizing java applet
Quote:
Originally Posted by
maz3r
I have told that i am using local graphics variable. and reference to that variable is taken from [Panelname].getgraphics() method
This is not a good way to draw Swing graphics because (as you're finding out) the Graphics object obtained will not be the correct one if a repaint occurs. A better way is as aussiemcgr showed you. Another technique you can use is to draw your images to a BufferedImage object (yes, you can get its Graphics context via getGraphics()), and then draw that BufferedImage to the JComponent in the JComponent's paintComponent method.
Re: How to retain Graphics after resizing java applet
Hey can you tell me that when and how that paintComponents method is called??
Because i didn't know how to implement this method
Re: How to retain Graphics after resizing java applet
The paintComponent() method is defined in many swing components. You need to override it and add code as needed to draw your graphics.
Take a look at the tutorial:
Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
Re: How to retain Graphics after resizing java applet
Sorry!!!!!
But i have not use any swing component in my application.
So please tell me a different solution.
If you want i can post here a snippet of my applet code.
Re: How to retain Graphics after resizing java applet
I don't understand why you are not using swing components. There are many benefits with them like double buffering to prevent flickering.
With the AWT components, you override the paint() method instead of the paintComponent() method.
If you could make a small complete program that compiles, executes and shows the problem, that would help with testing the problem.
Re: How to retain Graphics after resizing java applet
Code java:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="drawit" width=1000 height=600></applet>*/
public class dr extends Applet implements ActionListener,MouseListener,MouseMotionListener {
Button oval;
Panel buttonPanel,toolPanel;
Color xorcolor,maincolor;
String shape,color,getx,gety;
Choice brushchoice,brushsize;
int mouseX,mouseY,initialX,initialY,height,width,i,j,mainX,mainY,prevX,prevY;
boolean t=true;
boolean initialoval=true;
public void init() {
setLayout(new BorderLayout());
oval=new Button("OVAL");
buttonPanel=new Panel();
toolPanel=new Panel();
buttonPanel.setLayout(new GridLayout(12,2,5,5));
buttonPanel.setBackground(Color.blue);
toolPanel.setBackground(Color.gray);
buttonPanel.add(oval);
this.add(buttonPanel,BorderLayout.WEST);
this.add(toolPanel,BorderLayout.NORTH);
//********************LISTENER FOR ALL OBJECTS****************************
addMouseListener(this);
addMouseMotionListener(this);
oval.addActionListener(this);
}
public void mouseClicked(MouseEvent me){
}
public void mouseEntered(MouseEvent me){
}
public void mouseReleased(MouseEvent me){
repaint();
}
public void mouseDragged(MouseEvent me){
drawoval(me);
}
public void mouseExited(MouseEvent me){
repaint();
}
public void mouseMoved(MouseEvent me){
repaint();
}
public void mousePressed(MouseEvent me){
refreshValues(me);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==oval)
{
//here shape will be defined
shape="OVAL";
}
}
public void refreshValues(MouseEvent me)
{
mainX=me.getX();
mainY=me.getY();
initialX=me.getX();
initialY=me.getY();
height=0;width=0;
mouseX=me.getX();
mouseY=me.getY();
}
public void setBoundary(MouseEvent me)
{
if(t)
{
initialX=me.getX();
initialY=me.getY();
height=0;width=0;
t=false;
}
mouseX=me.getX();
mouseY=me.getY();
if(mouseX<initialX || mouseY<initialY)
{
if(mouseX<initialX)
{
width=initialX-mouseX;
mainX=initialX-width;
}
else
{
width=mouseX-initialX;
mainX=initialX;
}
if(mouseY<initialY)
{
height=initialY-mouseY;
mainY=initialY-height;
}
else
{
height=mouseY-initialY;
mainY=initialY;
}
}
else
{
height=mouseY-initialY;
width=mouseX-initialX;
mainX=initialX;
mainY=initialY;
}
}
public void drawoval(MouseEvent me){
Graphics2D g=(Graphics2D)getGraphics();
if(initialoval)
{
refreshValues(me);
initialoval=false;
}
g.setColor(maincolor);
g.setXORMode(Color.white);
g.drawOval(mainX,mainY,width,height);
setBoundary(me);
g.drawOval(mainX,mainY,width,height);
}
public void update(Graphics gm)
{
Graphics2D g=(Graphics2D)gm;
t=true;
g.setColor(maincolor);
if(!initialoval)g.drawOval(mainX,mainY,width,height);initialoval=true;
showStatus("Shape : "+shape+" Color : "+color+" Cursor: X:"+getx+" Y:"+gety);
}
public void paint(Graphics gm)
{
update(gm);
}
}
Here is the small part of that code in which i have problem.
It is an edited code so please ignore additional things. and focus on oval.
In this code you can draw Oval on the screen.
But all the Oval that you drawn are disable if windows is resized.
So please my problem in that applet.
I will solve it in my main application
Re: How to retain Graphics after resizing java applet
You should get rid of your update method, get rid of your drawOval method and do all drawing in the paint(...) method using the Graphics object provided. Be sure to call the super.paint(gm) method first, and be sure to remove the call to update(gm) from within paint(...).
After you've done this, then read through some of the Swing tutorial to learn how to do this in Swing.
Re: How to retain Graphics after resizing java applet
Your code doesn't draw the ovals when the paint method is called because the window has been resized or because of a call to repaint.
The paint method needs to control the drawing of the ovals on the screen because it is called when the window is resized or the repaint() is called.
Re: How to retain Graphics after resizing java applet
Quote:
Originally Posted by
curmudgeon
You should get rid of your update method, get rid of your drawOval method and do all drawing in the paint(...) method using the Graphics object provided. Be sure to call the super.paint(gm) method first, and be sure to remove the call to update(gm) from within paint(...).
After you've done this, then read through some of the Swing tutorial to learn how to do this in Swing.
I have done this that you told me
Here is the code
Code java:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="drawit" width=1000 height=600></applet>*/
public class dr extends Applet implements ActionListener,MouseListener,MouseMotionListener {
Button oval;
Panel buttonPanel,toolPanel;
Color xorcolor,maincolor;
String shape,color,getx,gety;
Choice brushchoice,brushsize;
int mouseX,mouseY,initialX,initialY,height,width,i,j,mainX,mainY,prevX,prevY;
boolean t=true;
boolean initialoval=true;
public void init() {
setLayout(new BorderLayout());
oval=new Button("OVAL");
buttonPanel=new Panel();
toolPanel=new Panel();
buttonPanel.setLayout(new GridLayout(12,2,5,5));
buttonPanel.setBackground(Color.blue);
toolPanel.setBackground(Color.gray);
buttonPanel.add(oval);
this.add(buttonPanel,BorderLayout.WEST);
this.add(toolPanel,BorderLayout.NORTH);
//********************LISTENER FOR ALL OBJECTS****************************
addMouseListener(this);
addMouseMotionListener(this);
oval.addActionListener(this);
}
public void mouseClicked(MouseEvent me){
}
public void mouseEntered(MouseEvent me){
}
public void mouseReleased(MouseEvent me){
}
public void mouseDragged(MouseEvent me){
drawoval(me);
}
public void mouseExited(MouseEvent me){
}
public void mouseMoved(MouseEvent me){
}
public void mousePressed(MouseEvent me){
refreshValues(me);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==oval)
{
//here shape will be defined
shape="OVAL";
}
}
public void refreshValues(MouseEvent me)
{
mainX=me.getX();
mainY=me.getY();
initialX=me.getX();
initialY=me.getY();
height=0;width=0;
mouseX=me.getX();
mouseY=me.getY();
}
public void setBoundary(MouseEvent me)
{
if(t)
{
initialX=me.getX();
initialY=me.getY();
height=0;width=0;
t=false;
}
mouseX=me.getX();
mouseY=me.getY();
if(mouseX<initialX || mouseY<initialY)
{
if(mouseX<initialX)
{
width=initialX-mouseX;
mainX=initialX-width;
}
else
{
width=mouseX-initialX;
mainX=initialX;
}
if(mouseY<initialY)
{
height=initialY-mouseY;
mainY=initialY-height;
}
else
{
height=mouseY-initialY;
mainY=initialY;
}
}
else
{
height=mouseY-initialY;
width=mouseX-initialX;
mainX=initialX;
mainY=initialY;
}
}
public void drawoval(MouseEvent me){
Graphics2D g=(Graphics2D)getGraphics();
if(initialoval)
{
refreshValues(me);
initialoval=false;
}
g.setColor(maincolor);
g.setXORMode(Color.white);
g.drawOval(mainX,mainY,width,height);
setBoundary(me);
g.drawOval(mainX,mainY,width,height);
}
public void paint(Graphics gm)
{
super.paint(gm);
t=true;
gm.setColor(maincolor);
if(!initialoval)gm.drawOval(mainX,mainY,width,height);initialoval=true;
showStatus("Shape : "+shape+" Color : "+color+" Cursor: X:"+getx+" Y:"+gety);
}
}
Still problem persists.
Is this task cannot be done without using swing?
Re: How to retain Graphics after resizing java applet
What is this line of code supposed to do?
Code :
if(!initialoval)gm.drawOval(mainX,mainY,width,height);initialoval=true;
Why hide the statements like that?
Try debugging the code by adding a println statement to the paint method to see when it is called.
The drawing code needs to be called from the paint() method.
Another test. Change the color used in the paint() method:
Code :
gm.setColor(Color.magenta); //<<<<<<<<<maincolor);
Then draw an oval and slowly drag the side to change the size. Notice the color of oval changes to magenta and then the oval disappears.