Hai all,
I have a program that to draw an image on JPanel. But in that program problem is that i cant draw image on first selecin of image. I want to select the picture twice .On second time it will draw on JPanel
Here i m attaching the program
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class pFrame extends JFrame implements ActionListener
{
JMenuBar mbar;
JMenuItem open;
JMenu file;
Container c;
JComboBox colorChoice, figureChoice;
Display canvas;
JScrollPane imageScroller;
pFrame()
{
mbar=new JMenuBar();
file=new JMenu("File");
open=new JMenuItem("Open");
file.add(open);
mbar.add(file);
c=getContentPane();
c.setLayout(new BorderLayout());
c.setBackground(Color.gray);
setJMenuBar(mbar);
JPanel buttonBar = new JPanel();
buttonBar.setBackground(Color.gray);
c.add(buttonBar, BorderLayout.SOUTH);
canvas = new Display();
imageScroller = new JScrollPane();
imageScroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
imageScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
c.add(imageScroller,BorderLayout.CENTER);
JPanel choiceBar = new JPanel();
choiceBar.setBackground(Color.gray);
//c.add(choiceBar, BorderLayout.NORTH);
JButton fill = new JButton("Set Background");
fill.addActionListener(canvas);
buttonBar.add(fill);
JButton clear = new JButton("Clear");
clear.addActionListener(canvas);
buttonBar.add(clear);
colorChoice = new JComboBox();
colorChoice.addItem("Black");
colorChoice.addItem("Red");
colorChoice.addItem("Green");
colorChoice.addItem("Blue");
colorChoice.addItem("Cyan");
colorChoice.addItem("Magenta");
colorChoice.addItem("Yellow");
colorChoice.addItem("White");
colorChoice.setBackground(Color.white);
buttonBar.add(colorChoice);
figureChoice = new JComboBox();
figureChoice.addItem("Curve");
figureChoice.addItem("Straight Line");
figureChoice.addItem("Rectangle");
figureChoice.addItem("Oval");
figureChoice.addItem("RoundRect");
figureChoice.addItem("Filled Rectangle");
figureChoice.addItem("Filled Oval");
figureChoice.addItem("Filled RoundRect");
figureChoice.setBackground(Color.white);
buttonBar.add(figureChoice);
open.addActionListener(this);
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
setSize(d);
setVisible(true);
setTitle("Rajeesh's");
setResizable(false);
} //constructor
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==open)
{
JFileChooser chooser=new JFileChooser();
chooser.showOpenDialog(this);
File f=chooser.getSelectedFile();
ImageIcon img=new ImageIcon(f.toString());
int WID=img.getIconWidth();
int HEI=img.getIconHeight();
canvas.setPreferredSize(new java.awt.Dimension(WID,HEI));
imageScroller.setViewportView(canvas);
Graphics g=canvas.getGraphics();
canvas.OSI=img.getImage();
g.drawImage(img.getImage(),0,0,WID,HEI,img.getImageObserver());
}
}//actionPerformed
public Insets getInsets()
{
return new Insets(10,10,10,10);
//return new Insets(0,0,0,0);
}
private class Display extends JPanel implements MouseListener, MouseMotionListener, ActionListener
{
private final static int BLACK = 0,RED = 1,GREEN = 2,BLUE = 3,CYAN = 4,MAGENTA = 5,YELLOW = 6,WHITE = 7;
private final static int CURVE = 0,LINE = 1,RECT = 2,OVAL = 3,ROUNDRECT = 4,FILLED_RECT = 5,FILLED_OVAL = 6,FILLED_ROUNDRECT = 7;
Image OSI; // The off-screen image (created in checkOSI()).
int widthOfOSI, heightOfOSI;
/* The following variables are used when the user is sketching a
curve while dragging a mouse. */
private int mouseX, mouseY;
private int prevX, prevY;
private int startX, startY;
private boolean dragging;
private int figure;
private Graphics dragGraphics; // A graphics context for the off-screen image,
// to be used while a drag is in progress.
private Color dragColor; // The color that is used for the figure that is
// being drawn.
Display()
{
addMouseListener(this);
addMouseMotionListener(this);
setBackground(Color.white);
}
private void drawFigure(Graphics g, int shape, int x1, int y1, int x2, int y2)
{
if (shape == LINE)
{
// For a line, just draw the line between the two points.
g.drawLine(x1,y1,x2,y2);
return;
}
int x, y; // Top left corner of rectangle that contains the figure.
int w, h; // Width and height of rectangle that contains the figure.
if (x1 >= x2)
{ // x2 is left edge
x = x2;
w = x1 - x2;
}
else
{ // x1 is left edge
x = x1;
w = x2 - x1;
}
if (y1 >= y2)
{ // y2 is top edge
y = y2;
h = y1 - y2;
}
else
{ // y1 is top edge.
y = y1;
h = y2 - y1;
}
switch (shape)
{ // Draw the appropriate figure.
case RECT:
g.drawRect(x, y, w, h);
break;
case OVAL:
g.drawOval(x, y, w, h);
break;
case ROUNDRECT:
g.drawRoundRect(x, y, w, h, 20, 20);
break;
case FILLED_RECT:
g.fillRect(x, y, w, h);
break;
case FILLED_OVAL:
g.fillOval(x, y, w, h);
break;
case FILLED_ROUNDRECT:
g.fillRoundRect(x, y, w, h, 20, 20);
break;
}//switch
}//drawFigure
private void repaintRect(int x1, int y1, int x2, int y2)
{
// Call repaint on a rectangle that contains the points (x1,y1)
// and (x2,y2). (Add a 1-pixel border along right and bottom
// edges to allow for the pen overhang when drawing a line.)
int x, y; // top left corner of rectangle that contains the figure
int w, h; // width and height of rectangle that contains the figure
if (x2 >= x1)
{ // x1 is left edge
x = x1;
w = x2 - x1;
}
else
{ // x2 is left edge
x = x2;
w = x1 - x2;
}
if (y2 >= y1)
{ // y1 is top edge
y = y1;
h = y2 - y1;
}
else
{ // y2 is top edge.
y = y2;
h = y1 - y2;
}
repaint(x,y,w+1,h+1);
}//repaintRect
void checkOSI()
{
// This method is responsible for creating the off-screen image.
// It should be called before using the OSI. It will make a new OSI if
// the size of the panel changes.
if (OSI == null || widthOfOSI != getSize().width || heightOfOSI != getSize().height)
{
// Create the OSI, or make a new one if panel size has changed.
OSI = null; // (If OSI already exists, this frees up the memory.)
OSI = createImage(getSize().width, getSize().height);
widthOfOSI = getSize().width;
heightOfOSI = getSize().height;
Graphics OSG = OSI.getGraphics(); // Graphics context for drawing to OSI.
OSG.setColor(getBackground());
OSG.fillRect(0, 0, widthOfOSI, heightOfOSI);
OSG.dispose();
}
}
public void paintComponent(Graphics g)
{
// Copy the off-screen image to the screen,
// after checking to make sure it exists. Then,
// if a shape other than CURVE is being drawn,
// draw it on top of the image from the OSI.
checkOSI();
g.drawImage(OSI, 0, 0, this);
if (dragging && figure != CURVE)
{
g.setColor(dragColor);
drawFigure(g,figure,startX,startY,mouseX,mouseY);
}
}//paintComponent
public void actionPerformed(ActionEvent evt)
{
String command = evt.getActionCommand();
checkOSI();
if (command.equals("Set Background"))
{
setBackground(getCurrentColor());
if (colorChoice.getSelectedIndex() == BLACK)
colorChoice.setSelectedIndex(WHITE);
else
colorChoice.setSelectedIndex(BLACK);
}//outer if
Graphics g = OSI.getGraphics();
g.setColor(getBackground());
g.fillRect(0,0,getSize().width,getSize().height);
g.dispose();
repaint();
}//actionPerformed
private Color getCurrentColor()
{
// Check the colorChoice menu to find the currently
// selected color, and return the appropriate color
// object.
int currentColor = colorChoice.getSelectedIndex();
switch (currentColor)
{
case BLACK:
return Color.black;
case RED:
return Color.red;
case GREEN:
return Color.green;
case BLUE:
return Color.blue;
case CYAN:
return Color.cyan;
case MAGENTA:
return Color.magenta;
case YELLOW:
return Color.yellow;
default:
return Color.white;
}
}
public void mousePressed(MouseEvent evt)
{
if (dragging == true)
return;
prevX = startX = evt.getX(); // Save mouse coordinates.
prevY = startY = evt.getY();
figure = figureChoice.getSelectedIndex();
dragColor = getCurrentColor();
dragGraphics = OSI.getGraphics();
dragGraphics.setColor(dragColor);
dragging = true; // Start drawing.
} // end mousePressed()
public void mouseReleased(MouseEvent evt)
{
if (dragging == false)
return; // Nothing to do because the user isn't drawing.
dragging = false;
mouseX = evt.getX();
mouseY = evt.getY();
if (figure == CURVE)
{
// A CURVE is drawn as a series of LINEs
drawFigure(dragGraphics,LINE,prevX,prevY,mouseX,mouseY);
repaintRect(prevX,prevY,mouseX,mouseY);
}
else if (figure == LINE)
{
repaintRect(startX,startY,prevX,prevY);
if (mouseX != startX || mouseY != startY)
{
// Draw the line only if it has non-zero length.
drawFigure(dragGraphics,figure,startX,startY,mouseX,mouseY);
repaintRect(startX,startY,mouseX,mouseY);
}
}
else
{
repaintRect(startX,startY,prevX,prevY);
if (mouseX != startX && mouseY != startY)
{
// Draw the shape only if both its height
// and width are both non-zero.
drawFigure(dragGraphics,figure,startX,startY,mouseX,mouseY);
repaintRect(startX,startY,mouseX,mouseY);
}
}
dragGraphics.dispose();
dragGraphics = null;
}
public void mouseDragged(MouseEvent evt)
{
// Called whenever the user moves the mouse while a mouse button
// is down. If the user is drawing a curve, draw a segment of
// the curve on the off-screen image, and repaint the part
// of the panel that contains the new line segment. Otherwise,
// just call repaint and let paintComponent() draw the shape on
// top of the picture in the off-screen image.
if (dragging == false)
return; // Nothing to do because the user isn't drawing.
mouseX = evt.getX(); // x-coordinate of mouse.
mouseY = evt.getY(); // y=coordinate of mouse.
if (figure == CURVE)
{
drawFigure(dragGraphics,LINE,prevX,prevY,mouseX,mouseY);
repaintRect(prevX,prevY,mouseX,mouseY);
}
else
{
// Repaint two rectangles: The one that contains the previous
// version of the figure, and the one that will contain the
// new version. The first repaint is necessary to restore
// the picture from the off-screen image in that rectangle.
repaintRect(startX,startY,prevX,prevY);
repaintRect(startX,startY,mouseX,mouseY);
}
prevX = mouseX; // Save coords for the next call to mouseDragged or mouseReleased.
prevY = mouseY;
} // end mouseDragged.
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseMoved(MouseEvent evt) { }
} // end Display
public static void main(String arg[])
{
pFrame si=new pFrame();
}
} // end class pFrame
Thanks in advance
Nikhil V G