Adding java 2d graphices to a JScrollPane

How can I use the graphics 2d in a Java Applet JScrollPane?

here is me code. I have a a ImageOps.java file that does a bunch of Java 2d, but I need it to fit into my applet's JScrollPane. Can anyone help me?

package louis.tutorial;

import java.awt.BorderLayout;

import javax.swing.JPanel;

import javax.swing.JApplet;

import java.awt.Dimension;

import javax.swing.JInternalFrame;

import javax.swing.JButton;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.GridBagLayout;

import java.awt.GridBagConstraints;

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.awt.Image;

import java.awt.MediaTracker;

import java.awt.Rectangle;

import java.awt.Point;

import javax.swing.JComboBox;

import javax.swing.JList;

import javax.swing.JTextArea;

import javax.imageio.ImageIO;

import javax.swing.Icon;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JLabel;

import javax.swing.JMenuItem;

import javax.swing.ImageIcon;

import javax.swing.KeyStroke;

import java.awt.FileDialog;

import java.awt.Toolkit;

import java.awt.Color;

import java.awt.image.BufferedImage;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.event.KeyEvent;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.File;

import java.io.IOException;

import java.net.URL;

import javax.swing.JScrollPane;

/**

* Place class description here

*

* @version 1.0

* @author llakser

*

*/

public class ImageApplet extends JApplet {

private JPanel jContentPane = null;

private JPanel jpLower = null;

private JButton JBFirst = null;

private JButton JBPrevious = null;

private JButton JBNext = null;

private JButton JBLast = null;

private JButton JBZoonIn = null;

private JButton JBZoomOut = null;

private JButton JBAnimate = null;

private JButton JBPause = null;

private JButton JBAutoRepeat = null;

private JComboBox jcbFrameDelay = null;

private BufferedImage image;// the rasterized image

private Image[]numbers = new Image[10];

private Threadanimate;

private MediaTracker tracker;

private intframe = 0;

private ImageIcon[] icon = null;

private JFrame f = new JFrame("ImageOps");

/**

* This is the xxx default constructor

*/

public ImageApplet() {

super();

}

/**

* This method initializes this

*

* @return void

*/

public void init() {

this.setSize(600, 600);

this.setContentPane(getJContentPane());

this.setName("");

this.setMinimumSize(new Dimension(600, 600));

this.setPreferredSize(new Dimension(600, 600));

// f.addWindowListener(new WindowAdapter() {

//public void windowClosing(WindowEvent e) {System.exit(0);}

// });

ImageOps applet = new ImageOps();

//getJContentPane().add("Center", f);

getJContentPane().add("Center", applet);

//applet.add(f);

applet.init();

f.pack();

f.setSize(new Dimension(550,550));

f.setVisible(true);

}

public void load() {

tracker = new MediaTracker(this);

for (int i = 1; i < 10; i++) {

numbers = getImage (getCodeBase (), i+".jpeg");//getImage(getDocumentBase(), "Res/" + i + ".gif");

//Image img;

//ico = new ImageIcon(getCodeBase (), i+".jpeg");

/*if (fInBrowser)

img = getImage (getCodeBase (), "1.jpeg");

else

img = Toolkit.getDefaultToolkit ().getImage (

"1.jpeg");*/

tracker.addImage(numbers, i);

try {

tracker.waitForAll();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

int i = 1;

// for (int i = 1; i < 10; i++)

icon[0] = new ImageIcon ("C:/images2/1.jpeg");//getImage(getDocumentBase(), "Res/" + i + ".gif");

// }

//icon[0] = new ImageIcon("C:/images2/1.jpg");

//System.out.println("Made it after icon " + ico.toString());

//ImageIcon ii = new ImageIcon("1.jpg");

//jLabel = new JLabel(icon);

//jLabel.setText("JLabel");

//jLabel.setIcon(icon[0]);

Graphics g = null;

//g.drawImage(numbers[1],0,0,1500,1400,jScrollPane);

//ImageIcon ii = new ImageIcon(numbers[2]);

//this.jScrollPane.add(new JLabel(ii));// = new JScrollPane(new JLabel(ii));

// System.out.println("Gee I made it..");

//scroll.paintComponents(g);

//paintComponents(g);

}

public void paintComponents(Graphics g) {

super.paintComponents(g);

// Retrieve the graphics context; this object is used to paint shapes

Graphics2D g2d = (Graphics2D)g;

// Draw an oval that fills the window

int width = this.getBounds().width;

int height = this.getBounds().height;

Icon icon = new ImageIcon("C:/images2/1.jpg");

//jLabel.setIcon(icon);

//g2d.drawImage(0,0,1500,1400);

g2d.drawOval(10,20, 300,400);

// and Draw a diagonal line that fills MyPanel

g2d.drawLine(0,0,width,height);

}

/**

* This method initializes jContentPane

*

* @return javax.swing.JPanel

*/

private JPanel getJContentPane() {

if (jContentPane == null) {

jContentPane = new JPanel();

jContentPane.setLayout(new BorderLayout());

jContentPane.setPreferredSize(new Dimension(768, 576));

jContentPane.setMinimumSize(new Dimension(768, 576));

jContentPane.setName("");

jContentPane.add(getJpLower(), BorderLayout.SOUTH);

}

return jContentPane;

}

/**

* This method initializes jpLower

*

* @return javax.swing.JPanel

*/

private JPanel getJpLower() {

if (jpLower == null) {

try {

jpLower = new JPanel();

jpLower.setLayout(null);

jpLower.setPreferredSize(new Dimension(500, 100));

jpLower.setMinimumSize(new Dimension(500, 160));

jpLower.add(getJBFirst(), null);

jpLower.add(getJBPrevious(), null);

jpLower.add(getJBNext(), null);

jpLower.add(getJBLast(), null);

jpLower.add(getJBZoonIn(), null);

jpLower.add(getJBZoomOut(), null);

jpLower.add(getJBAnimate(), null);

jpLower.add(getJBPause(), null);

jpLower.add(getJBAutoRepeat(), null);

jpLower.add(getJcbFrameDelay(), null);

} catch (java.lang.Throwable e) {

// TODO: Something

}

}

return jpLower;

}

/**

* This method initializes JBFirst

*

* @return javax.swing.JButton

*/

private JButton getJBFirst() {

if (JBFirst == null) {

try {

JBFirst = new JButton();

JBFirst.setText("First");

JBFirst.setLocation(new Point(7, 7));

JBFirst.setSize(new Dimension(59, 26));

JBFirst.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

System.out.println("First Button Clicked()");

load();

// TODO Auto-generated Event stub actionPerformed()

}

});

} catch (java.lang.Throwable e) {

// TODO: Something

}

}

return JBFirst;

}

/**

* This method initializes JBPrevious

*

* @return javax.swing.JButton

*/

private JButton getJBPrevious() {

if (JBPrevious == null) {

try {

JBPrevious = new JButton();

JBPrevious.setText("Previous");

JBPrevious.setLocation(new Point(69, 7));

JBPrevious.setSize(new Dimension(94, 26));

} catch (java.lang.Throwable e) {

// TODO: Something

}

}

return JBPrevious;

}

/**

* This method initializes JBNext

*

* @return javax.swing.JButton

*/

private JButton getJBNext() {

if (JBNext == null) {

try {

JBNext = new JButton();

JBNext.setText("Next");

JBNext.setLocation(new Point(166, 7));

JBNext.setSize(new Dimension(66, 26));

} catch (java.lang.Throwable e) {

// TODO: Something

}

}

return JBNext;

}

/**

* This method initializes JBLast

*

* @return javax.swing.JButton

*/

private JButton getJBLast() {

if (JBLast == null) {

try {

JBLast = new JButton();

JBLast.setText("Last");

JBLast.setLocation(new Point(235, 7));

JBLast.setSize(new Dimension(59, 26));

} catch (java.lang.Throwable e) {

// TODO: Something

}

}

return JBLast;

}

/**

* This method initializes JBZoonIn

*

* @return javax.swing.JButton

*/

private JButton getJBZoonIn() {

if (JBZoonIn == null) {

try {

JBZoonIn = new JButton();

JBZoonIn.setText("Zoom In");

JBZoonIn.setLocation(new Point(408, 7));

JBZoonIn.setPreferredSize(new Dimension(90, 26));

JBZoonIn.setSize(new Dimension(90, 26));

} catch (java.lang.Throwable e) {

// TODO: Something

}

}

return JBZoonIn;

}

/**

* This method initializes JBZoomOut

*

* @return javax.swing.JButton

*/

private JButton getJBZoomOut() {

if (JBZoomOut == null) {

try {

JBZoomOut = new JButton();

JBZoomOut.setBounds(new Rectangle(503, 7, 90, 26));

JBZoomOut.setPreferredSize(new Dimension(90, 26));

JBZoomOut.setText("Zoom Out");

} catch (java.lang.Throwable e) {

// TODO: Something

}

}

return JBZoomOut;

}

/**

* This method initializes JBAnimate

*

* @return javax.swing.JButton

*/

private JButton getJBAnimate() {

if (JBAnimate == null) {

try {

JBAnimate = new JButton();

JBAnimate.setText("Animate");

JBAnimate.setSize(new Dimension(90, 26));

JBAnimate.setPreferredSize(new Dimension(90, 26));

JBAnimate.setLocation(new Point(408, 36));

} catch (java.lang.Throwable e) {

// TODO: Something

}

}

return JBAnimate;

}

/**

* This method initializes JBPause

*

* @return javax.swing.JButton

*/

private JButton getJBPause() {

if (JBPause == null) {

try {

JBPause = new JButton();

JBPause.setPreferredSize(new Dimension(90, 26));

JBPause.setLocation(new Point(503, 36));

JBPause.setSize(new Dimension(90, 26));

JBPause.setText("Pause");

} catch (java.lang.Throwable e) {

// TODO: Something

}

}

return JBPause;

}

/**

* This method initializes JBAutoRepeat

*

* @return javax.swing.JButton

*/

private JButton getJBAutoRepeat() {

if (JBAutoRepeat == null) {

try {

JBAutoRepeat = new JButton();

JBAutoRepeat.setBounds(new Rectangle(446, 65, 106, 26));

JBAutoRepeat.setText("Auto Repeat");

} catch (java.lang.Throwable e) {

// TODO: Something

}

}

return JBAutoRepeat;

}

/**

* This method initializes jcbFrameDelay

*

* @return javax.swing.JComboBox

*/

private JComboBox getJcbFrameDelay() {

if (jcbFrameDelay == null) {

try {

jcbFrameDelay = new JComboBox();

jcbFrameDelay.setSize(new Dimension(156, 25));

jcbFrameDelay.setName("Frame Delay");

jcbFrameDelay.setLocation(new Point(7, 35));

} catch (java.lang.Throwable e) {

// TODO: Something

}

}

return jcbFrameDelay;

}

} // @jve:decl-index=0:visual-constraint="10,10"

[12002 byte] By [wwwlouiea] at [2007-11-27 5:57:14]
# 1
Use [code] tags and indent your code: [url= http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url]Also, you will more help if you post a short (<1 page) sample program thatdemonstrates your problem.
Hippolytea at 2007-7-12 16:29:55 > top of Java-index,Java Essentials,Java Programming...
# 2
generally you don't. But you can subclass any component to override the paintComponent(Graphics) method, and the graphics object you get is a Graphics2D object, so you cast it.
bsampieria at 2007-7-12 16:29:55 > top of Java-index,Java Essentials,Java Programming...
# 3
How do I override the JScollPane?
wwwlouiea at 2007-7-12 16:29:55 > top of Java-index,Java Essentials,Java Programming...
# 4
> How do I override the JScollPane?I think JScrollPane works fine as it is. What are you trying to accomplish? What is your goal?
Hippolytea at 2007-7-12 16:29:55 > top of Java-index,Java Essentials,Java Programming...
# 5

I am trying to create my own Image Applet. It will do animations. It will start out paused on the first image right after the images are loaded from the url folder. I could then select the animate button to continue the animation. I will be able to zoom in and out on the images when they are in paused mode. These will be Doppler Radar images.

wwwlouiea at 2007-7-12 16:29:55 > top of Java-index,Java Essentials,Java Programming...
# 6

What if you design your own ImageComponent and then:

ImageComponent comp = new ImageComponent ();

JScrollPane sp = new JScrollPane(comp);

...

What's wrong with JScrollPane as it stands that it needs to be changed?

Hippolytea at 2007-7-12 16:29:55 > top of Java-index,Java Essentials,Java Programming...
# 7
I can't get the graphics to load in the current JScrollpane. I am not that good at java to know. All the Graphic samples I have seen show drawing on the this, Not in a jframe or JScrolPane.
wwwlouiea at 2007-7-12 16:29:55 > top of Java-index,Java Essentials,Java Programming...
# 8
The main problem I have is that I don't know how the Graphics reflect which container or components. You create a graphic g and then you write to it. How do you designate which container or components it applies too?
wwwlouiea at 2007-7-12 16:29:55 > top of Java-index,Java Essentials,Java Programming...
# 9

public void paintComponent(Graphics g) {

// Clear off-screen bitmap

super.paintComponent(g);

// Cast Graphics to Graphics2D

Graphics2D g2d = (Graphics2D)g;

// Set pen parameters

g2d.setPaint(fillColorOrPattern);

g2d.setStroke(penThicknessOrPattern);

g2d.setComposite(someAlphaComposite);

g2d.setFont(anyFont);

g2d.translate(...);

g2d.rotate(...);

g2d.scale(...);

g2d.shear(...);

g2d.setTransform(someAffineTransform);

// Allocate a shape

SomeShape s = new SomeShape(...);

// Draw shape

g2d.draw(s); // outline

g2d.fill(s); // solid

}

How do I designate this code to draw in a JScrollPane or something else?

wwwlouiea at 2007-7-12 16:29:55 > top of Java-index,Java Essentials,Java Programming...
# 10

I think you've got it:

public class ImageComponent extends JPanel { //or JComponent

protected void paintComponent(Graphics g) {

super.paintComponent(g);

//do your rendering here

}

...

}

The fact that ImageComponent sits inside JScrollPane is accidental, right?

It should be able to function on its own, without a JScrollPane present, just as JTextArea can.

So I'm not sure what your question is. Can you post a small (<1 page)

example program demonstrating your problem.

For example, here are a very few lines demonstrating what I am trying to say:

import java.awt.*;

import javax.swing.*;

class CustomExample extends JPanel {

public CustomExample() {

setPreferredSize(new Dimension(800,800));

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.fillOval(0, 0, getWidth(), getHeight());

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable(){

public void run() {

JFrame f = new JFrame("CustomExample");

f.getContentPane().add(new JScrollPane(new CustomExample()));

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setSize(400,400);

f.setLocationRelativeTo(null);

f.setVisible(true);

}

});

}

}

Now you post your problem in a program that is just as short.

Hippolytea at 2007-7-12 16:29:55 > top of Java-index,Java Essentials,Java Programming...
# 11

Hippolyte,

You think I got it. LOL.

I appreciate your help but I don't got it. I am using Eclipse and the Visual Editor. I create a Visual class and then extend it from JApplet. When I get some overridden class I can't put it where I want it. I don't know what else to say. Thanks anyways

wwwlouiea at 2007-7-12 16:29:55 > top of Java-index,Java Essentials,Java Programming...
# 12
Okay. I just use a text editor. It lets me write whatever code I want ;-)
Hippolytea at 2007-7-12 16:29:55 > top of Java-index,Java Essentials,Java Programming...
# 13

Hippolyte,

I am trying to incorporate the code snipplet you supplied. I ran it on its own and I changed it to a JLabel. It's very simple and I have learned from it. I will see if I can place it in my Applet in the place it needs to be. Things are always clearer the next day. I will let you know of my results.

wwwlouiea at 2007-7-12 16:29:55 > top of Java-index,Java Essentials,Java Programming...
# 14

public class ImageComponent extends JPanel { //or JComponent

protected void paintComponent(Graphics g) {

super.paintComponent(g);

//do your rendering here

}

...

}

public class ImageComponent extends JPanel { //or JComponent

protected void paintComponent(Graphics g) {

super.paintComponent(g);

//do your rendering here

}

...

}

format check

wwwlouiea at 2007-7-12 16:29:55 > top of Java-index,Java Essentials,Java Programming...
# 15
Do you see how the tags work?You just enclose code:[code]x = y;System.out.println("sample code");[/code]
Hippolytea at 2007-7-21 21:39:14 > top of Java-index,Java Essentials,Java Programming...
# 16
Yes I see. I tested it with and without in the above example.
wwwlouiea at 2007-7-21 21:39:14 > top of Java-index,Java Essentials,Java Programming...