-
Java Paint Method?
Hi, my code works fine but I want to put some graphics in my code and I already wrote a method and with joption it works fine but I want to use this code in jcombo box and I can't figure it out where to put because if I put under action performed it give me errors take a look at this code
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.event.*;
// using JPanel as a drawing panel
public class JPanelDemo1 extends JFrame implements ActionListener, ItemListener
{
JPanel southPanel;
JPanel centerPanel;
JComboBox myBox;
public final int CIRCLE = 0, SQUARE = 1, RECT = 2, OVAL = 3;
int red, green, blue;
int x, y, width, height;
int shape;
Random rand = new Random();
Container container;
public JPanelDemo1()
{
container = getContentPane();
String shapesList[] = {"CIRCLE","SQUARE","OVAL","RECTANGLES"};
southPanel = new JPanel();
myBox = new JComboBox(shapesList);
myBox.addActionListener(this);
container.add( southPanel, BorderLayout.SOUTH );
container.add(myBox,BorderLayout.SOUTH);
setSize( 600, 500 );
setVisible( true );
setLocation( 100, 100 );
}
public void actionPerformed( ActionEvent ae )
{
if (myBox.getSelectedItem().equals("CIRCLE"))
{
System.out.println("Circles");
}
if (myBox.getSelectedItem().equals("SQUARE"))
{
System.out.println("Squares");
}
if (myBox.getSelectedItem().equals("OVAL"))
{
System.out.println("OVAL");
}
if (myBox.getSelectedItem().equals("RECTANGLES"))
{
System.out.println("Rectangles");
}
}
public void itemStateChanged( ItemEvent ie )
{
// actionPerformed(e);
}
public static void main( String[] arg )
{
JPanelDemo1 demo = new JPanelDemo1();
demo.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
and the code I want to replace where it says console commands SYSTEM.OUT.PRINTLN
Code:
import javax.swing.*;
import java.awt.*;
import java.util.*;
// more efficient version of RandomShapes2
public class RandomShapes3 extends JApplet
{
public final int CIRCLE = 0, SQUARE = 1, RECT = 2, OVAL = 3;
public void paint( Graphics g )
{
int red, green, blue;
int x, y, width, height;
int shape;
Random rand = new Random();
super.paint( g );
shape = Integer.parseInt( JOptionPane.showInputDialog(
"Please choose the shape: \n" +
"0 - Circles\n" +
"1 - Squares\n" +
"2 - Rectangles\n" +
"3 - Ovals\n" +
"-1 - Quit" ) );
while ( shape != -1 )
{
super.paint( g );
switch( shape )
{
case CIRCLE:
for( int i = 0; i < 10; i ++ )
{
//super.paint( g );
// set the values for the 7 variables
red = rand.nextInt( 256 ); // 0 - 255
green = rand.nextInt( 256 );
blue = rand.nextInt( 256 );
x = 20 + rand.nextInt( 181 ); // 20 - 200
y = 20 + rand.nextInt( 181 ); // 20 - 200
width = 20 + rand.nextInt( 181 ); // 20 - 200
height = 20 + rand.nextInt( 181 ); // 20 - 200
// set the color
g.setColor( new Color( red, green, blue ) );
g.fillOval( x, y, width, width );
// put it to sleep for 100 ms
try
{
Thread.sleep( 100 );
}
catch ( Exception e ) {}
} // for
break;
case OVAL:
for( int i = 0; i < 10; i ++ )
{
//super.paint( g );
// set the values for the 7 variables
red = rand.nextInt( 256 ); // 0 - 255
green = rand.nextInt( 256 );
blue = rand.nextInt( 256 );
x = 20 + rand.nextInt( 181 ); // 20 - 200
y = 20 + rand.nextInt( 181 ); // 20 - 200
width = 20 + rand.nextInt( 181 ); // 20 - 200
height = 20 + rand.nextInt( 181 ); // 20 - 200
// set the color
g.setColor( new Color( red, green, blue ) );
g.fillOval( x, y, width, height );
try
{
Thread.sleep( 100 );
}
catch ( Exception e ) {}
}
break;
case SQUARE:
for( int i = 0; i < 10; i ++ )
{
//super.paint( g );
// set the values for the 7 variables
red = rand.nextInt( 256 ); // 0 - 255
green = rand.nextInt( 256 );
blue = rand.nextInt( 256 );
x = 20 + rand.nextInt( 181 ); // 20 - 200
y = 20 + rand.nextInt( 181 ); // 20 - 200
width = 20 + rand.nextInt( 181 ); // 20 - 200
height = 20 + rand.nextInt( 181 ); // 20 - 200
// set the color
g.setColor( new Color( red, green, blue ) );
g.fillRect( x, y, width, width );
try
{
Thread.sleep( 100 );
}
catch ( Exception e ) {}
}
break;
case RECT:
for( int i = 0; i < 10; i ++ )
{
//super.paint( g );
// set the values for the 7 variables
red = rand.nextInt( 256 ); // 0 - 255
green = rand.nextInt( 256 );
blue = rand.nextInt( 256 );
x = 20 + rand.nextInt( 181 ); // 20 - 200
y = 20 + rand.nextInt( 181 ); // 20 - 200
width = 20 + rand.nextInt( 181 ); // 20 - 200
height = 20 + rand.nextInt( 181 ); // 20 - 200
// set the color
g.setColor( new Color( red, green, blue ) );
g.fillRect( x, y, width, height );
try
{
Thread.sleep( 100 );
}
catch ( Exception e ) {}
}
break;
//default:
} // switch
shape = Integer.parseInt( JOptionPane.showInputDialog(
"Please choose the shape: \n" +
"0 - Circles\n" +
"1 - Squares\n" +
"2 - Rectangles\n" +
"3 - Ovals\n" +
"-1 - Quit" ) );
} // while
}
}
so basically I want this joption version replace with Jcombo Box any inputs will be helpful thanks.
-
Now you've got a whole program's worth of logic in the paint method. Again, don't do this unless you want your GUI's graphics to be as slow as molasses. Also, you cannot fully control when paint/paintComponent gets called and so this logic will recur erratically.
-
Also read the Swing Tutorials on painting: a Swing JComponent should call paintComponent( ... ), not paint( ... ), that's for AWT Components. The previous advice is very valid: don't put any program logic in that method because you don't know when it's called.
kind regards,
Jos