Displaying the same image in a JPanel and using scroll - HELP

Displaying the same image in a JPanel and using scroll - HELP

View Answers

August 26, 2008 at 7:36 PM

Hi friend,


use this code.

import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class ImageDisplay extends JPanel implements MouseListener {
private Dimension area; //indicates area taken up by graphics
private Vector<Rectangle> circles;
private JPanel drawingPane;

private final Color colors[] = {
Color.red, Color.blue, Color.green, Color.orange,
Color.cyan, Color.magenta, Color.darkGray, Color.yellow};
private final int color_n = colors.length;

public ImageDisplay() {
super(new BorderLayout());
area = new Dimension(0,0);
circles = new Vector<Rectangle>();

//Set up the instructions.
JLabel jlableLeft = new JLabel("Click left mouse button to place a circle.");
JLabel jlableRight = new JLabel("Click right mouse button to clear drawing area.");
JPanel jpane = new JPanel(new GridLayout(0,1));
jpane.setFocusable(true);
jpane.add(jlableLeft);
jpane.add(jlableRight);

//Set up the drawing area.
drawingPane = new DrawingPane();
drawingPane.setBackground(Color.white);
drawingPane.addMouseListener(this);

//Put the drawing area in a scroll pane.
JScrollPane scroller = new JScrollPane(drawingPane);
scroller.setPreferredSize(new Dimension(200,200));

//Lay out this demo.
add(jpane, BorderLayout.PAGE_START);
add(scroller, BorderLayout.CENTER);
}

/** The component inside the scroll pane. */
public class DrawingPane extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);

Rectangle rect;
for (int i = 0; i < circles.size(); i++) {
System.out.println("circle size: " + circles.size());
rect = circles.elementAt(i);
g.setColor(colors[(i % color_n)]);
g.fillOval(rect.x, rect.y, rect.width, rect.height);
}
}
}

August 26, 2008 at 7:37 PM

//Handle mouse events.
public void mouseReleased(MouseEvent e) {
final int W = 100;
final int H = 100;
boolean changed = false;
if (SwingUtilities.isRightMouseButton(e)) {
//This will clear the graphic objects.
circles.removeAllElements();
area.width=0;
area.height=0;
changed = true;
} else {
int x = e.getX() - W/2;
int y = e.getY() - H/2;
if (x < 0) x = 0;
if (y < 0) y = 0;
Rectangle rect = new Rectangle(x, y, W, H);
circles.addElement(rect);
drawingPane.scrollRectToVisible(rect);

int this_width = (x + W + 2);
if (this_width > area.width) {
area.width = this_width; changed=true;
}

int this_height = (y + H + 2);
if (this_height > area.height) {
area.height = this_height; changed=true;
}
}
if (changed) {
drawingPane.setPreferredSize(area);
//Let the scroll pane know to update itself
//and its scrollbars.
drawingPane.revalidate();
}
drawingPane.repaint();
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}

private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Scroll example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
JComponent newContentPane = new ImageDisplay();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

//Display the window.
frame.setSize(300,300);
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
------------------------------

Thanks

Amardeep









Related Tutorials/Questions & Answers:
Scroll in JPanel
Scroll in JPanel  Hello sir,  Hi Friend, You can use the following code: import java.awt.*; import javax.swing.*; class scroll { public static void main(String[] args) { Frame f = new Frame ("Demo"); JPanel p = new
Displaying image using jsp and spring.
Displaying image using jsp and spring.  how to display an image stored in WEB-INF/images folder on the browser using jsp and spring
Advertisements
Press button and Scroll Image to Scrollpanel
Press button and Scroll Image to Scrollpanel  Insert image into the jpanel,inside the jscrollpane.If i press the button and scroll image
problen in displaying the text and image in gallery using Android
problen in displaying the text and image in gallery using Android   public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gallery = (Gallery
image displaying in java
image displaying in java  how to display an image by using load image button in applet viewer
displaying image
displaying image  how to upload image and retrieve it form database mysql in php?   Here is an example of uploading image using php. 1)form.html: <form method="post" enctype="multipart/form-data" action="upload.php
Displaying error on the Same form
Displaying error on the Same form  I want to display the errors on the same form(Top of the form) after validation. I am used Div element and Table.... There is a root Div element they are using for Window purpose. I tried to resize it using DOM
how to scroll multiple images using on Click in Android
how to scroll multiple images using on Click in Android  I am new... there is requirement like multiple image scrolling dynamically. But unfortunately i am unable to scroll multiple images. But i am able to scroll single image. plz let me know
displaying image in awt - Java Beginners
displaying image in awt  Hi All, I have downloaded the code to display image using awt from here and when I execute the code I am getting... ActionListener{ JFrame fr = new JFrame ("Image loading program Using awt"); Label
resize image with the change in size of jpanel.. - Java Beginners
resize image with the change in size of jpanel..  hi.. i am try to insert image in jpanel in jform to give attractive look to application,but the of image do not changes if the size of jframe or jpanel changes. if there is any
jpanel
jpanel  hello sir, i made one jpanel inside the frame and made... when click the button then open the second panel same place of first panel....?? thanku and second question how to attach calender jar file in jtextfield using
jpanel
jpanel  hello sir, i made one jpanel inside the frame and made... when click the button then open the second panel same place of first panel....?? thanku and second question how to attach calender jar file in jtextfield using
jpanel
jpanel  hello sir, i made one jpanel inside the frame and made... when click the button then open the second panel same place of first panel....?? thanku and second question how to attach calender jar file in jtextfield using
jpanel
jpanel  hello sir, i made one jpanel inside the frame and made... when click the button then open the second panel same place of first panel....?? thanku and second question how to attach calender jar file in jtextfield using
Displaying search results in same panel of gridview in same jframe window.
Displaying search results in same panel of gridview in same jframe window... search and see the results in the same panel of the Jframe in Gridview Jtable... should be displayed in the jpanel of jframe window.... import java.sql.*; import
Displaying search results in same panel of gridview in same jframe window.
Displaying search results in same panel of gridview in same jframe window... search and see the results in the same panel of the Jframe in Gridview Jtable... should be displayed in the jpanel of jframe window.... import java.sql.*; import
Display Multiple Images in jscrollpane using Java Jpanel
Display Multiple Images in jscrollpane using Java Jpanel  Browse and Display multiple images in vertical view of java jscrollpane using jpanel
Displaying image with byte array[] - Java Beginners
Displaying image with byte array[]  Hi Frndz.. As per my requirement i need to show an image by using an byte array object which have image data in binary format. The challenge here i have only byte array[] object ,by using
How to Set Transperent Background Image to Jpanel - Java Beginners
How to Set Transperent Background Image to Jpanel  Sir ,How to set Background Image to Jpanel.  Hi Friend, Try the following code...); } } ); JTextArea area=new JTextArea(15,4){ ImageIcon image = new
image displaying from database with some other information
image displaying from database with some other information  hi, in the following section of code when i am not displaying the image it is working properly but when i tried to display the image nothing is displayed on browser
insert image using hibernate
insert image using hibernate  Sir, Today I hadposted question regarding upload image, th answer you had given is using SQL, but I am using Hibernate to insert data in the same table where I want to insert image. Plz hlp me
Image is not displaying
Image is not displaying  Hii i am using spring javamail with html template to send mail,i am facing that image displaying prob when mail have to cm in gmail,in template just i am giving my current location of image which
Displaying image when clicked on URL in jsp - JSP-Servlet
Displaying image when clicked on URL in jsp  Hi, I am using... in displaying the image from the folder. Regards, Anish... For Screenshot : abc.jpg here abc.jpg is a url and when clicked would show me an image
Image Movement using Swings
Image Movement using Swings  How to move image using Swings
maximize an image using javaScript
maximize an image using javaScript  Write the code to maximize an image using javaScript
Displaying Hello World using GWT
Displaying Hello World using GWT       This example describes the process for displaying... involved in displaying the Hello World message using Net Beans IDE 6.1
Using insert update and delete in the same servlet
Using insert update and delete in the same servlet  How to write insert, update and delete coding in the same servlet
display image using jsp
display image using jsp  display image using jsp and phonegap on emulator of eclipse   Here is a simple jsp code that displays an image on browser. <%@ page import="java.io.*" %> <%@page contentType="image/gif
Image is in same folder where the java file is located - Swing AWT
Image is in same folder where the java file is located  dear sir It is there in the same folder..means the photograph.jpg file is created in the same folder where my java file is present... wat I should do sir
Image name,image path into database and image into folder using jsp
Image name,image path into database and image into folder using jsp  How to insert image path and image name into oracle database and image into folder using jsp
Create Scroll Bar in Java using SWT
Create Scroll Bar in Java using SWT       This section is all about creating scroll bar in Java SWT  The given example will show you how to create scroll bar in Java using
Create SpreadSheet same as excel using java applet
Create SpreadSheet same as excel using java applet  Hi Ji, May i know to create new excel sheet using java Applet
Create SpreadSheet same as excel using java applet
Create SpreadSheet same as excel using java applet  Hi Ji, May i know to create new excel sheet using java Applet
Uploading image using jsp
Uploading image using jsp  how to upload image using jsp. Already i tried, But that image file does not read. It returns only -1 without reading that image file ... I want know that solution using by u... Thanks, P.S.N.  
Draw bufferimage in jpanel using netbeans
Draw bufferimage in jpanel using netbeans  please i need urgent help. i have form which contains some fields generated in netbeans. how can i draw bufferimage in Jpanel that is inside the form. thanks
download image using url in jsp
download image using url in jsp  how to download image using url in jsp
Displaying images using Javascript
Displaying images using Javascript... to display images using javascript and css.For this we are going to make program named image-slideshow-5. css,image-slideshow-5.html,image-slideshow-5.js.The code
displaying a calendar of only current month in jsp on webpage using javascript
displaying a calendar of only current month in jsp on webpage using javascript  how can we display a calendar of only current month in jsp on webpage using javascript
upload image using JSP Hibernate
upload image using JSP Hibernate  sir, I want to take image from user and save to database(MYSQL) using Hibernate and JSP Thanks in advance
Replace broken Image using JQuery
Replace broken Image using JQuery  Sir My web page contains a lot of images. But when it will not loaded properly, it shows a broken image. How can I fix it so that broken image will be replaced by error image automatically
How to Store Image using JSF
How to Store Image using JSF  Hi How to upload images in db. using jsf. For jsf photo uploading .. I used this one code for upload image.. But this code haven't option to upload any images . i want to store image in db
displaying data based on criteria from 2 tables having same type of data - Java Beginners
displaying data based on criteria from 2 tables having same type of data  Dear Experts, First, thanks for replying my question regarding my servlet. My mind was very confused then. I realise that I can't use a servlet
How to retrieve image from database using jsp and servlet?
How to retrieve image from database using jsp and servlet?  Hi, I am trying to find code for displaying the image from database in a JSP page. How to retrieve image from database using jsp and servlet
Image Icon Using Canvas Example
Image Icon Using Canvas Example     ... location using Canvas class. In this example to create the image we are using following code: And for draw the image we are using following code of lines:ADS
Jpanel allignment
Jpanel allignment  i have two panels added one below anotehr. and i have a one button. when i clicked button. the first panel ara should... using gribaglayout or any layout. need quick reply. thanks in advance
how to fetch image from mysql using jsp
how to fetch image from mysql using jsp  how to fetch image from mysql using jsp
Scroll ImagesUIScrollView
Scroll ImagesUIScrollView  Hi, Can anyone please suggest me how to scroll images in UIScrollView in iPad? THanks
Display Image using Toolkit.getImage()
Display Image using Toolkit.getImage()   ... the specified image using Toolkit.getImage() method. To display the image, put an image...) of class ToolKit returns an image from the specified url. The method drawImage
How send different files to browser at same time ..example(pdf&html) want display in same request using servlet
How send different files to browser at same time ..example(pdf&html) want display in same request using servlet  package com.readfiles; import... at same time ..example(pdf&html) want display in same request using servlet
Storing Multiple image in sql using java
Storing Multiple image in sql using java  Hi, How to store and retrieve a multiple image in sql using java but already i have created sql table if i want to insert a image while runtime execution. Can anyone tell me solution

Ads