Hi I am trying to get the Java Graphics to work but the paint function is messing up the GUI.
following is my code:
I apologize if the code is not very pretty.Also I can send the images for the labels if anyone wants it.Code:/*
Sample Program:Displaying a window
File:Sample.java
Author:Shweta Gupte
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.border.*;
import java.awt.geom.*;
import java.applet.*;
//import java.awt.Graphics;
class Sample extends JFrame implements ActionListener {
private static final int FRAME_WIDTH =700;
private static final int FRAME_HEIGHT=700;
private static final int FRAME_X_ORIGIN=0;
private static final int FRAME_Y_ORIGIN=0;
private JRadioButton [] radioButton;
private final String CIRCLE="Circle";
private JLabel image1,image2,image3,image4;
private JButton circleBtn,RectBtn,SquareBtn,ovalBtn;
private TitledBorder title;
private int oval=0,rect=0;
public static void main ( String [] args){
Sample frame = new Sample();
frame.setVisible(true);
}
public Sample(){
Container contentPane;
JPanel drawPanel,manipulatePanel,buttonPanel,colorPanel,RadioPanel;
String [] btnText= {"Translate","Resize"};
//Set Frame properties
setTitle("Shape Shifter");
setSize( FRAME_WIDTH, FRAME_HEIGHT);
setLocation( FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//changeBkColor();
contentPane = getContentPane( );
contentPane.setLayout(new BorderLayout());
buttonPanel =new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.blue);
drawPanel = new JPanel();
drawPanel.setBorder(BorderFactory.createTitledBorder("Draw"));
drawPanel.setBackground(Color.blue);
//Fill and Coordinates Panel
colorPanel = new JPanel();
colorPanel.setBorder(BorderFactory.createRaisedBevelBorder());
colorPanel.setLayout(new GridLayout(0,1));
JCheckBox fillBtn =new JCheckBox("Fill");
fillBtn.setBackground(Color.blue);
JCheckBox coordBtn =new JCheckBox("Coordinates");
coordBtn.setBackground(Color.blue);
colorPanel.add(fillBtn);
colorPanel.add(coordBtn);
colorPanel.setBackground(Color.blue);
manipulatePanel = new JPanel();
manipulatePanel.setLayout(new FlowLayout());
title= BorderFactory.createTitledBorder("Manipulate");
title.setTitleJustification(TitledBorder.RIGHT);
manipulatePanel.setBorder(title);
manipulatePanel.setBackground(Color.blue);
RadioPanel =new JPanel();
RadioPanel.setLayout(new GridLayout(0,1));
RadioPanel.setBorder(BorderFactory.createLoweredBevelBorder());
radioButton =new JRadioButton[btnText.length];
for (int i=0; i<radioButton.length;i++){
radioButton[i]= new JRadioButton(btnText[i]);
//radioButton[i].addItemListener(this);
radioButton[i].setBackground(Color.blue);
RadioPanel.add(radioButton[i]);
}
manipulatePanel.add(RadioPanel);
//Oval button
ovalBtn =new JButton();
//Setimage gif
image3=new JLabel (new ImageIcon( "myOval.gif"));
ovalBtn.add(image3);
drawPanel.add(ovalBtn);
ovalBtn.addActionListener(this);
//Circle button
circleBtn =new JButton();
//Setimage gif
image1=new JLabel (new ImageIcon( "myCircle.gif"));
circleBtn.add(image1);
drawPanel.add(circleBtn);
circleBtn.addActionListener(this);
//rectangle Button
RectBtn =new JButton();
//setimage gif
image2=new JLabel (new ImageIcon( "myRectangle.gif"));
RectBtn.add(image2);
drawPanel.add(RectBtn);
RectBtn.addActionListener(this);
//Square button
SquareBtn =new JButton();
//Setimage gif
image4=new JLabel (new ImageIcon( "mySquare.gif"));
SquareBtn.add(image4);
drawPanel.add(SquareBtn);
SquareBtn.addActionListener(this);
buttonPanel.add(drawPanel);
buttonPanel.add(colorPanel);
buttonPanel.add(manipulatePanel);
contentPane.add(buttonPanel,BorderLayout.NORTH);
//setDefaultCloseOperation(EXIT_ON_CLOSE);
changeBkColor();
}
/*public void paint(Graphics g){
//g.setColor(Color.red);
if(oval==1){
g.drawOval(200,200,220,220);
//g.setColor(Color.green);
}
if(rect==1)
g.fillRect(230,230,250,250);
}*/
public void actionPerformed(ActionEvent event){
if(event.getSource() instanceof JButton){
JButton clickedButton =(JButton) event.getSource();
repaint();
//System.out.println(clickedButton);
if(clickedButton== ovalBtn){
oval=1;
rect=0;
System.out.println("here");
clickedButton=null;
}
if(clickedButton== circleBtn){
//draw Circle
}
if(clickedButton ==RectBtn){
oval=0;
rect=1;
System.out.println("here1");
clickedButton=null;
//Draw Rect
}
if(clickedButton== SquareBtn){
//Draw Square
}
}
System.out.println("here2");
//repaint();
}
private void changeBkColor(){
Container contentPane = getContentPane();
contentPane.setBackground(Color.BLACK);
}
}
I have commented out the paint function.but I need it to draw the circle ,oval etc.
Any alternatives or suggestions appreciated.
Thanks
svaidya

