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"

