Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

graphics question 1

Status
Not open for further replies.

room24

Programmer
Dec 28, 2003
83
JM
if i change the value of xvalue and yvalue in the code below it changes everything on the screen even the things already painted how do i work around that?? i want to leave things already drawn untouched and just change the size of the oval on the next draw. looking for some ideas here plz.

code:

// Fig. 13.19: Painter2.java
// Using class MouseMotionAdapter.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Painter2 extends JFrame {
private int pointCount = 0;

// array of 1000 java.awt.Point references
private Point points[] = new Point[ 1000 ];

// set up GUI and register mouse event handler
public Painter2()
{
super( "A simple paint program" );




// create a label and place it in SOUTH of BorderLayout
getContentPane().add( new JLabel( "Drag the mouse to draw" ),
BorderLayout.SOUTH );

addMouseMotionListener(

new MouseMotionAdapter() { // anonymous inner class

// store drag coordinates and repaint
public void mouseDragged( MouseEvent event )
{
if ( pointCount < points.length ) {
points[ pointCount ] = event.getPoint();
++pointCount;
repaint();
}
}

} // end anonymous inner class

); // end call to addMouseMotionListener

setSize( 300, 150 );
setVisible( true );

} // end Painter2 constructor

// draw oval in a 4-by-4 bounding box at specified location on window
public void paint( Graphics g )
{
super.paint( g ); // clears drawing area

for ( int i = 0; i < points.length && points[ i ] != null; i++ )
g.fillOval( points[ i ].x, points[ i ].y, xvalue, yvalue );

}

public static void main( String args[] )
{
Painter2 application = new Painter2();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

} // end class Painter2
 
// Fig. 13.19: Painter2.java
// Using class MouseMotionAdapter.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Painter2 extends JFrame {
private int pointCount = 0;

// array of 1000 java.awt.Point references
private Point points[] = new Point[ 1000 ];
private int myProperties[][] = new int[1000][2];
private int xvalue = 100, yvalue = 100;

// set up GUI and register mouse event handler
public Painter2()
{
super( "A simple paint program" );




// create a label and place it in SOUTH of BorderLayout
getContentPane().add( new JLabel( "Drag the mouse to draw" ),
BorderLayout.SOUTH );

addMouseMotionListener(

new MouseMotionAdapter() { // anonymous inner class

// store drag coordinates and repaint
public void mouseDragged( MouseEvent event )
{
if ( pointCount < points.length ) {
points[ pointCount ] = event.getPoint();
myProperties[pointCount][0] = xvalue;
myProperties[pointCount][1] = yvalue;
if (pointCount>30)
setXY(30,30);
++pointCount;
repaint();
}
}

} // end anonymous inner class

); // end call to addMouseMotionListener

setSize( 300, 150 );
setVisible( true );
} // end Painter2 constructor

// draw oval in a 4-by-4 bounding box at specified location on window
public void paint( Graphics g )
{
super.paint( g ); // clears drawing area

for ( int i = 0; i < points.length && points[ i ] != null; i++ )
g.fillOval( points[ i ].x, points[ i ].y, myProperties[0], myProperties[1]);

}
public void setXY(int x, int y)
{
xvalue=x;
yvalue=y;
}

public static void main( String args[] )
{
Painter2 application = new Painter2();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

} // end class Painter2
 
// use double buffer
// Fig. 13.19: Painter2.java
// Using class MouseMotionAdapter.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Painter2 extends JFrame {
private int pointCount = 0;

// array of 1000 java.awt.Point references
private Point points[] = new Point[ 1000 ];
private int myProperties[][] = new int[1000][2];
private int xvalue = 100, yvalue = 100;

// set up GUI and register mouse event handler
public Painter2()
{
super( "A simple paint program" );




// create a label and place it in SOUTH of BorderLayout
getContentPane().add( new JLabel( "Drag the mouse to draw" ),
BorderLayout.SOUTH );

addMouseMotionListener(

new MouseMotionAdapter() { // anonymous inner class

// store drag coordinates and repaint
public void mouseDragged( MouseEvent event )
{
if ( pointCount < points.length ) {
points[ pointCount ] = event.getPoint();
myProperties[pointCount][0] = xvalue;
myProperties[pointCount][1] = yvalue;
if (pointCount>30)
setXY(30,30);
++pointCount;
repaint();
}
}

} // end anonymous inner class

); // end call to addMouseMotionListener

setSize( 300, 150 );
setVisible( true );
} // end Painter2 constructor

// draw oval in a 4-by-4 bounding box at specified location on window
public void paint( Graphics g )
{
// super.paint( g ); // clears drawing area
Image backbuffer = createImage( 300, 150 );
Graphics backg = backbuffer.getGraphics();

for ( int i = 0; i < points.length && points[ i ] != null; i++ )
backg.fillOval( points[ i ].x, points[ i ].y, myProperties[0], myProperties[1]);

g.drawImage( backbuffer, 0, 0, this );
}
public void setXY(int x, int y)
{
xvalue=x;
yvalue=y;
}

public static void main( String args[] )
{
Painter2 application = new Painter2();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

} // end class Painter2
 
how would i change color because i added

public void paint( Graphics g )
{
super.paint( g ); // clears drawing area


g.setColor( pcolor );
for ( int i = 0; i < points.length && points[ i ] != null; i++ )
g.fillOval( points[ i ].x, points[ i ].y, myProperties[0], myProperties[1] );
//g.setPaintMode();




}
and it does not work?? i plan to use jcolorchooser to assign a color to pcolor but where would i put this line "g.setColor( pcolor );" in order to change the color before a paint.
 
// Fig. 13.19: Painter2.java
// Using class MouseMotionAdapter.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Painter2 extends JFrame {
private int pointCount = 0;

// array of 1000 java.awt.Point references
private Point points[] = new Point[ 1000 ];
private int myProperties[][] = new int[1000][2];
private int xvalue = 100, yvalue = 100;
private Color myColor[] = new Color[1000];
private Color currentColor;
// set up GUI and register mouse event handler
public Painter2()
{
super( "A simple paint program" );




// create a label and place it in SOUTH of BorderLayout
getContentPane().add( new JLabel( "Drag the mouse to draw" ),
BorderLayout.SOUTH );

addMouseMotionListener(

new MouseMotionAdapter() { // anonymous inner class

// store drag coordinates and repaint
public void mouseDragged( MouseEvent event )
{
if ( pointCount < points.length ) {
points[ pointCount ] = event.getPoint();
myProperties[pointCount][0] = xvalue;
myProperties[pointCount][1] = yvalue;
if (pointCount>30)
{
setXY(30,30);
setColor(Color.RED);
}
else
setColor(Color.BLACK);
myColor[pointCount] = currentColor;
++pointCount;
repaint();
}
}

} // end anonymous inner class

); // end call to addMouseMotionListener

setSize( 300, 150 );
setVisible( true );
} // end Painter2 constructor

// draw oval in a 4-by-4 bounding box at specified location on window
public void paint( Graphics g )
{
// super.paint( g ); // clears drawing area
Image backbuffer = createImage( 300, 150 );
Graphics backg = backbuffer.getGraphics();

for ( int i = 0; i < points.length && points[ i ] != null; i++ )
{
backg.setColor(myColor);
backg.fillOval( points[ i ].x, points[ i ].y, myProperties[0], myProperties[1]);
}

g.drawImage( backbuffer, 0, 0, this );
}
public void setXY(int x, int y)
{
xvalue=x;
yvalue=y;
}

public void setColor(Color tempColor)
{
currentColor = tempColor;
}

public static void main( String args[] )
{
Painter2 application = new Painter2();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

} // end class Painter2
 
this is what i did but the problem is backgroung color changes everything works except changing the color before a paint to the selected color i use the same principle as i saw u did but its not working why?? here is the code below


// Fig. 12.19: Painter.java
// Using class MouseMotionAdapter.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Painter extends JFrame
{

private String x[] = { "4", "8", "16", "32", "64" };
private String y[] = { "4", "8", "16", "32", "64" };
private int xValue = 4, yValue = 4;
private JButton changeBackground, changeColor, ChangePen;
private Color color = Color.lightGray;
//private Color pcolor = Color.BLACK;
private Color myColor[] = new Color[1000];
private Color currentColor;
private Container c;
private JPanel buttonPanel;
private int pointCount = 0;
private JComboBox penSizex, penSizey;
private int myProperties[][] = new int[1000][2];




// array of 1000 java.awt.Point references
private Point points[] = new Point[ 1000 ];




public Painter()
{
super( "A simple paint program" );


//System.out.println("in painter!!!");
setSize( 800, 600 );
show();




c = getContentPane();

c.add(
new Label( "Drag the mouse to draw" ),
BorderLayout.SOUTH );




//Add button here to change color
buttonPanel = new JPanel();
changeColor = new JButton( "Pen color" );
changeBackground = new JButton( "Change Background" );

buttonPanel.add( changeBackground );
buttonPanel.add( changeColor );

buttonPanel.add(new Label( "Click arrows to choose pensize" ));

penSizex = new JComboBox( x );
penSizex.setMaximumRowCount( 3 );

penSizey = new JComboBox( y );
penSizey.setMaximumRowCount( 3 );


buttonPanel.add( penSizex );
buttonPanel.add(new Label( "X" ));
buttonPanel.add( penSizey );
buttonPanel.add(new Label( "Y" ));



// listen to backgroud button events
changeBackground.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
color =
JColorChooser.showDialog( Painter.this,
"Choose a color", color );

if ( color == null )
color = Color.lightGray; // need to set screen to previous colour here

c.setBackground( color );
c.repaint();
}
}
);


// listen to pen color button events
changeColor.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
color =
JColorChooser.showDialog( Painter.this,
"Choose a color", currentColor );

if ( currentColor == null )
currentColor = Color.BLACK;


//c.repaint();
}
}
);




/** Listens to the combobox(penSizex). */
penSizex.addItemListener(

//.anonymous inner class to handle jcomboBox events
new ItemListener()
{

// handle JComboBox event
public void itemStateChanged( ItemEvent event )
{
if ( event.getStateChange() == ItemEvent.SELECTED )
{

//String petNamey = (String)penSizey.getSelectedItem();
String petNamex = (String)penSizex.getSelectedItem();

xValue = Integer.parseInt( petNamex );
//yValue = Integer.parseInt( petNamey );

}
}
}
);// end call to item listener



// Listens to the combobox(penSizey).
penSizey.addItemListener(

//.anonymous inner class to handle jcomboBox events
new ItemListener()
{

// handle JComboBox event
public void itemStateChanged( ItemEvent event )
{
if ( event.getStateChange() == ItemEvent.SELECTED )
{

String petNamey = (String)penSizey.getSelectedItem();



yValue = Integer.parseInt( petNamey );


}
}
}
);// end call to item listener





c.add( buttonPanel, BorderLayout.NORTH );
setVisible( true );






addMouseMotionListener(
new MouseMotionAdapter() {
public void mouseDragged( MouseEvent event )
{
if ( pointCount < points.length ) {
points[ pointCount ] = event.getPoint();
myProperties[pointCount][0] = xValue;
myProperties[pointCount][1] = yValue;
myColor[pointCount] = currentColor;
++pointCount;
repaint();

}
}
}
);// end call to addMouseMotionListener




}// end Painter2 constructor







// draw oval of dimensions determined from combobox
public void paint( Graphics g )
{
super.paint( g ); // clears drawing area



for ( int i = 0; i < points.length && points[ i ] != null; i++ )
{
g.setColor(myColor);
g.fillOval( points[ i ].x, points[ i ].y, myProperties[0], myProperties[1] );

}





}





// execute program
public static void main( String args[] )
{
Painter app = new Painter();
app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top