Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Displaying the same image in a JPanel and using scroll - HELP
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Lucas
Displaying the same image in a JPanel and using scroll - HELP
2 Answer(s)      6 years and 3 months ago
Posted in : Java Beginners



Advertisement
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
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
image displaying in java
image displaying in java  how to display an image by using load image button in applet viewer
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
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
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
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 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
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
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
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
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
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
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
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
Scroll ImagesUIScrollView
Scroll ImagesUIScrollView  Hi, Can anyone please suggest me how to scroll images in UIScrollView in iPad? THanks
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
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
Image Movement using Swings
Image Movement using Swings  How to move image using Swings
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
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
maximize an image using javaScript
maximize an image using javaScript  Write the code to maximize an image using javaScript
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 message on scroll Event
Display message on scroll Event In this tutorial, we will discuss about how to display message on scrolling of text area using jQuery scroll event. In this example, a text area with scroll bars are given , when we scroll any one
The scroll browser event of jQuery
The scroll browser event of jQuery In this tutorial ,we will print message on browser's window scrolling using scroll browser event of jQuery. In this example, some paragraphs are written , when we scroll down, a message
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
 

 

 

DMCA.com