ScrollBar Java Swing Image - java

I'm trying to add scrollbar to image in java swing, there is also function to read RGB from image. There are 2 problems:
Scrollbar doesn't work - i want to put image to container...some kind of ''frame'' with constant size and zoom image. After zoom - picture is larger - there are also scrollbars...but it's not working .
There is function to read RGB color pixel in from mouse position , but when i add scrollbar program think that image is in (0,0) position , and there colors are reading , not from real image in frame.
Code:
Window.class:
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
public class Window extends JFrame implements ActionListener{
JButton Bzoom ;
ButtonGroup BGzoom, BGMzoom;
JRadioButton RB1,RB2,RB4,RB8,RB16;
JRadioButton RBM1, RBM2, RBM4, RBM8, RBM16;
MyImage myImage;
public Window()
{
super("Image_Processing");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800 , 600);
setLayout(new FlowLayout());
setVisible(true);
setLocation(50,50);
setResizable(true);
// RadioButtons for X 1,2,4,8,16
BGzoom = new ButtonGroup();
RB1 = new JRadioButton("1x",true);
RB2 = new JRadioButton("2X",false);
RB4 = new JRadioButton("4X",false);
RB8 = new JRadioButton("8X",false);
RB16 = new JRadioButton("16X",false);
BGzoom.add(RB1);
BGzoom.add(RB2);
BGzoom.add(RB4);
BGzoom.add(RB8);
BGzoom.add(RB16);
add(RB1);
add(RB2);
add(RB4);
add(RB8);
add(RB16);
RB1.addActionListener(this);
RB2.addActionListener(this);
RB4.addActionListener(this);
RB8.addActionListener(this);
RB16.addActionListener(this);
// RadioButtons for MINUS X 1,2,4,8,16
BGMzoom = new ButtonGroup();
RBM1 = new JRadioButton("-1x",true);
RBM2 = new JRadioButton("-2X",false);
RBM4 = new JRadioButton("-4X",false);
RBM8 = new JRadioButton("-8X",false);
RBM16 = new JRadioButton("-16X",false);
BGzoom.add(RBM1);
BGzoom.add(RBM2);
BGzoom.add(RBM4);
BGzoom.add(RBM8);
BGzoom.add(RBM16);
add(RBM1);
add(RBM2);
add(RBM4);
add(RBM8);
add(RBM16);
RBM1.addActionListener(this);
RBM2.addActionListener(this);
RBM4.addActionListener(this);
RBM8.addActionListener(this);
RBM16.addActionListener(this);
Bzoom = new JButton("WYKONAJ");
add(Bzoom);
Bzoom.addActionListener(this);
// RGB for mouse click
getContentPane().addMouseMotionListener(new MouseMotionListener()
{
public void mouseMoved(MouseEvent e)
{
int x = (int) ((getContentPane().getMousePosition().x - (myImage.getBounds().x + 1))/myImage.getZoom());
int y = (int) ((getContentPane().getMousePosition().y - (myImage.getBounds().y + 1))/myImage.getZoom());
int color = myImage.getColorAt(x, y);
Color thisColor = new Color(color);
System.out.println("R: " + thisColor.getRed() + " G: " + thisColor.getGreen() + " B: " + thisColor.getBlue());
}
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
public void LoadImage(String filename)
{
myImage = new MyImage(filename);
JScrollPane scroll = new JScrollPane(myImage,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
add(scroll);
pack();
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == Bzoom)
{
if(RB1.isSelected())
{
myImage.zoomIn(1);
repaint();
}
else if(RB2.isSelected())
{
myImage.zoomIn(2);
repaint();
}
else if(RB4.isSelected())
{
myImage.zoomIn(4);
repaint();
}
else if(RB8.isSelected())
{
myImage.zoomIn(8);
repaint();
}
else if(RB16.isSelected())
{
myImage.zoomIn(16);
repaint();
}
else if(RBM1.isSelected())
{
myImage.zoomOut(1);
repaint();
}
else if(RBM2.isSelected())
{
myImage.zoomOut(2);
repaint();
}
else if(RBM4.isSelected())
{
myImage.zoomOut(4);
repaint();
}
else if(RBM8.isSelected())
{
myImage.zoomOut(8);
repaint();
}
else if(RBM16.isSelected())
{
myImage.zoomOut(16);
repaint();
}
}
}
}
MyImage.class:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class MyImage extends JPanel{
protected BufferedImage image;
private float zoom = (float) 1.0;
public MyImage() {}
public MyImage(String filename)
{
super();
File imageFile = new File(filename);
try
{
image = ImageIO.read(imageFile);
}
catch (IOException e)
{
System.err.println("Blad odczytu obrazka");
e.printStackTrace();
}
Dimension dimension = new Dimension(image.getWidth(), image.getHeight()); //wymiar
setPreferredSize(dimension);
}
public Dimension getPreferredSize()
{
int w = (int)(zoom * (image.getWidth()+20));
int h = (int)(zoom * (image.getHeight()+20));
return new Dimension(w, h);
}
#Override
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.scale(zoom, zoom);
g2d.drawImage(image, 0 , 0, this);
}
public float getZoom()
{
return zoom;
}
public int getColorAt(int x, int y)
{
return image.getRGB(x, y);
}
public void zoomIn(int scale)
{
zoom = zoom*scale;
}
public void zoomOut(int scale)
{
zoom = zoom/scale;
}
}
Main.class:
import java.awt.BorderLayout;
import java.awt.Container;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
//import org.opencv.core.Core;
//import org.opencv.core.CvType;
//import org.opencv.core.Mat;
public class Main {
public static void main( String[] args ) throws IOException
{
Window m = new Window();
m.LoadImage("file.jpg");
}
}
Thanks for all help , and sorry for my English ;)

Scrollbar doesn't work - i want to put image to container...some kind of ''frame'' with constant size and zoom image. After zoom - picture is larger - there are also scrollbars...but it's not working .
You need to inform the container that there has been change to the state of the component which might affect it's size...
public void zoomIn(int scale) {
zoom = zoom * scale;
revalidate();
repaint();
}
public void zoomOut(int scale) {
zoom = zoom / scale;
revalidate();
repaint();
}
The revalidate call will cause the container hierarchy to revalidate the layout information and update the layout
There is function to read RGB color pixel in from mouse position , but when i add scrollbar program think that image is in (0,0) position , and there colors are reading , not from real image in frame.
Add the MouseListener to the MyImage pane. MouseEvents are contextual to the component to which they are generated from
You will need a way to scale the mouse coordinates back to the "unscaled" version of the image so you can get the pixel represented by the unscaled image

Related

Zooming in and out relative to position of the mouse is working, but why does part of the drawn text go out of bounds when zooming in?

Below is a minimal reproducible code example where you can use mouse wheel to zoom in and out relative to the position of the mouse. The JScrollPane also auto-adjusts its size as you zoom in and out.
package testpane;
import java.awt.event.MouseAdapter;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
public class TestPane
{
public static Drawing d;
public static double zoomFactor = 1;
public static void main(String[] args)
{
JFrame f = new JFrame("Tree Diagram");
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new TestPane().makeDiagram());
f.setSize(1600, 800);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public JComponent makeDiagram()
{
d = new Drawing();
MouseAdapter mouseAdapter = new TestPaneMouseListener();
d.addMouseListener(mouseAdapter);
d.addMouseMotionListener(mouseAdapter);
d.addMouseWheelListener(mouseAdapter);
return new JScrollPane(d);
}
}
package testpane;
import java.awt.Component;
import java.awt.Container;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import javax.swing.JComponent;
import javax.swing.JViewport;
import javax.swing.SwingUtilities;
import static testpane.TestPane.d;
import static testpane.TestPane.zoomFactor;
public class TestPaneMouseListener extends MouseAdapter
{
private final Point origin = new Point();
#Override
public void mouseDragged(MouseEvent e)
{
Component c = e.getComponent();
Container p = SwingUtilities.getUnwrappedParent(c);
if (p instanceof JViewport)
{
JViewport viewport = (JViewport) p;
Point cp = SwingUtilities.convertPoint(c, e.getPoint(), viewport);
Point vp = viewport.getViewPosition();
vp.translate(origin.x - cp.x, origin.y - cp.y);
((JComponent) c).scrollRectToVisible(new Rectangle(vp, viewport.getSize()));
origin.setLocation(cp);
}
}
#Override
public void mousePressed(MouseEvent e)
{
Component c = e.getComponent();
Container p = SwingUtilities.getUnwrappedParent(c);
if(p instanceof JViewport)
{
JViewport viewport = (JViewport) p;
Point cp = SwingUtilities.convertPoint(c, e.getPoint(), viewport);
origin.setLocation(cp);
}
}
#Override
public void mouseWheelMoved(MouseWheelEvent e)
{
if(e.getWheelRotation()<0)
{
zoomFactor*=1.05;
d.setZoomFactor(1.05);
}
if(e.getWheelRotation()>0)
{
zoomFactor/=1.05;
d.setZoomFactor(1/1.05);
}
}
}
package testpane;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import javax.swing.JPanel;
public class Drawing extends JPanel
{
private final AffineTransform zoomTransform = new AffineTransform();
private final Rectangle rect = new Rectangle(1600, 800);
private double xOffset = 0;
private double yOffset = 0;
private double prevZoomFactor = 1;
public Drawing()
{
Font currentFont = getFont();
Font newFont = currentFont.deriveFont(currentFont.getSize() * 15F);
setFont(newFont);
setBackground(Color.WHITE);
}
public void setZoomFactor(double zoomFactor)
{
zoomTransform.scale(zoomFactor, zoomFactor);
revalidate();
repaint();
}
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
AffineTransform scrollTransform = g2d.getTransform();
double xRel = MouseInfo.getPointerInfo().getLocation().getX() - getLocationOnScreen().getX();
double yRel = MouseInfo.getPointerInfo().getLocation().getY() - getLocationOnScreen().getY();
double zoomDiv = TestPane.zoomFactor / prevZoomFactor;
xOffset = (zoomDiv) * (xOffset) + (1 - zoomDiv) * xRel;
yOffset = (zoomDiv) * (yOffset) + (1 - zoomDiv) * yRel;
prevZoomFactor = TestPane.zoomFactor;
scrollTransform.translate(xOffset, yOffset);
scrollTransform.concatenate(zoomTransform);
g2d.setTransform(scrollTransform);
g2d.drawString("Example", 400, 400);
g2d.dispose();
}
#Override
public Dimension getPreferredSize()
{
Rectangle r = zoomTransform.createTransformedShape(rect).getBounds();
return new Dimension(r.width, r.height);
}
}
Here is my problem: the zooming works just fine, but when I zoom too close, part of the "Example" written on the JScrollPane goes out of bounds. I believe that the problem comes from the translation I've made inside of the "Drawing" java class in the "paintComponent" method. While this translation does help me zoom in properly, it also causes my text to go out of bounds if I zoom too close. Is there any way for me to fix this?

Moving an image to the position of the cursor where is clicked on a JFrame

i need some help here with an exercise of my Java class. What i'm trying to do is that when i click on any part of the JFrame, an image moves closer to the pointer. Right now i have done the part that once i click on the JFrame the image is in the same position as the pointer but it does like it's "teleporting" and i'm trying to make it more like a constant movement to the pointer position.
So far this is my code i have atm:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ControlaRaton extends MouseAdapter {
JLabel label;
public ControlaRaton(JLabel label){
this.label = label;
}
public void mouseClicked(MouseEvent evt){
Point pos = evt.getPoint();
System.out.println(pos.x+" "+pos.y);
//System.out.println("Boton: "+evt.getButton());
label.setLocation(pos.x-20,pos.y-50);
}
}
Any ideas on how to do it that way? I was thinking maybe using a Thread but i don't know exactly how to implement it here :s
This is pretty simple approach, basically all this does is uses a Swing Timer to move an entity towards the last known click point
Have a look at
Concurrency in Swing
How to use Swing Timers
How to Write a Mouse Listener
for more details
Swing is NOT thread safe, when performing these types of operations, it's important to take this into consideration. The Swing API provides several ways to work threads, in this case, I've used a simple Timer as it generates updates at a regular interval and triggers updates from within the EDT, making it safe to update the UI from within
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MoveTowards {
public static void main(String[] args) {
new MoveTowards();
}
public MoveTowards() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MoveTowardsPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
class MoveTowardsPane extends JPanel {
private final BufferedImage image;
private Point imagePosition = new Point(150, 150);
private Point mousePoint;
private double imageAngleRad = 0;
public MoveTowardsPane() {
BufferedImage i = null;
try {
i = ImageIO.read(getClass().getResource("/sprite.png"));
} catch (IOException e) {
e.printStackTrace();
}
image = i;
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
mousePoint = e.getPoint();
double dx = e.getX() - imagePosition.getX();
double dy = e.getY() - imagePosition.getY();
imageAngleRad = Math.atan2(dy, dx);
repaint();
}
});
Timer timer = new Timer(40, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (mousePoint != null) {
int centerX = imagePosition.x + (image.getWidth() / 2);
int centerY = imagePosition.y + (image.getHeight() / 2);
if (mousePoint.x != centerX) {
imagePosition.x += mousePoint.x < centerX ? -1 : 1;
}
if (mousePoint.y != centerY) {
imagePosition.y += mousePoint.y < centerY ? -1 : 1;
}
repaint();
}
}
});
timer.start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
#Override
protected void paintComponent(Graphics gr) {
super.paintComponent(gr);
Graphics2D g = (Graphics2D) gr.create();
g.setRenderingHint(
RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
int cx = image.getWidth() / 2;
int cy = image.getHeight() / 2;
AffineTransform oldAT = g.getTransform();
g.translate(cx + imagePosition.x, cy + imagePosition.y);
g.rotate(imageAngleRad);
g.translate(-cx, -cy);
g.drawImage(image, 0, 0, null);
g.setTransform(oldAT);
g.dispose();
}
}
}
Why doesn't this use a JLabel? A lot of reasons, JLabel isn't really well fitted for the task, as it needs to take into account a lot of other information. This example also "turns" the sprite towards the click point, something which isn't easily achieved with a JLabel.
In principle, the theory is still the same for moving a component.
See
Painting in AWT and Swing
Performing Custom Painting
for more details about how this approach works
Redraw your circle on mouse move
void mouseMoved(MouseEvent evt){
Point pos = evt.getPoint();
System.out.println(pos.x+" "+pos.y);
//System.out.println("Boton: "+evt.getButton());
label.setLocation(pos.x-20,pos.y-50);
}
});
If you want to move the label just a bit with each click you could do the following:
public void mouseClicked(MouseEvent evt){
Point clickedPos = evt.getPoint();
Point newPosForLabel = calculateNewPos(clickedPos, labelPos);
label.setLocation(newPosForLabel);
}
private Point calculateNewPos(Point clickedPos, Point labelPos) {
// calculate newPos based on labelPos and clickedPos
return newPos;
}
else use the timer from Hannes or from the link given by MadProgrammer:

Java - paintComponent() drastically slowing down the program when repainting an image of high resolution

Recently I've been working on a small project until I've encountered something rather annoying.
What the program currently does is read certain folders which are generated by the program in which files with the file extension ".jpg" and ".png" are displayed (for this to work, the user the user has to manually insert the images in these folders).
That part works, until we get to the animation part that I've created.This is a rectangle with a certain RGBA value which changes its alpha value once the mouse hovers over it and exits it. With low resolution images, this animation works smoothly, but with high resolution images, its very laggy and the animation doesn't look so smooth.
mainFrame Class
package seriesorganiser;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* #author Luca Muscat
*/
public class mainFrame {
private static boolean check;
private static final String DOCUMENTS_LOCATION = System.getProperty("user.home") + "\\Documents";
public static void main(String[] args) throws IOException {
check = new File(DOCUMENTS_LOCATION + "\\SeriesOrganiser").exists();
if (!check) {
new File(DOCUMENTS_LOCATION + "\\SeriesOrganiser").mkdir();
new File(DOCUMENTS_LOCATION + "\\SeriesOrganiser\\Shows").mkdir();
new File(DOCUMENTS_LOCATION + "\\SeriesOrganiser\\Movies").mkdir();
}
JFrame frame = new JFrame("Series Organiser");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//Layout Handler
JPanel container = new JPanel();
//Movies=0, Shows=1.
JPanel moviesPanel = new moviesShowsPanel(0);
JPanel showsPanel = new moviesShowsPanel(1);
container.setLayout(new GridLayout(1, 2));
container.add(moviesPanel);
container.add(showsPanel);
frame.add(container);
frame.pack();
}
}
moviesShowsPanel
package seriesorganiser;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
/**
*
* #author Luca Muscat
*/
public class moviesShowsPanel extends JPanel {
int alpha = 255 / 2;
boolean isMouseOver = false;
boolean isMouseHover = false;
public final static int SHOWS_GENRE = 1;
public final static int MOVIES_GENRE = 0;
private int genreHolder;
public moviesShowsPanel(int genre) {
genreHolder=genre;
setPreferredSize(new Dimension(600, 600));
setBorder(BorderFactory.createLineBorder(Color.black));
setBackground(Color.LIGHT_GRAY);
addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent me) {
if(!isMouseOver)repaint();
isMouseOver = true;
}
#Override
public void mouseExited(MouseEvent me) {
if(isMouseOver)repaint();
isMouseOver = false;
}
});
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
scanForImage findImage = new scanForImage();
if (isMouseOver && alpha>10) {
alpha-=10;
}
if(!isMouseOver && alpha<145){
alpha+=10;
}
try {
if(genreHolder==MOVIES_GENRE){
g.drawImage(findImage.findMoviesImage(), 0, 0, getWidth(), getHeight(), null);
}
if(genreHolder==SHOWS_GENRE){
g.drawImage(findImage.findShowsImage(),0,0,getWidth(),getHeight(),null);
}
} catch (IOException ex) {
Logger.getLogger(moviesShowsPanel.class.getName()).log(Level.SEVERE, null, ex);
}
g.setColor(new Color(214, 229, 255, alpha));
g.fillRect(0, 0, getWidth(), getHeight());
repaint();
}
}
scanForImage
package seriesorganiser;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
*
* #author Luca Muscat
*/
public class scanForImage {
private static final String DOCUMENTS_LOCATION = System.getProperty("user.home")+"\\Documents";
BufferedImage findMoviesImage() throws IOException{
BufferedImage img = null;
File folderMovies = new File(DOCUMENTS_LOCATION+"\\SeriesOrganiser\\Movies");
img=DRY(folderMovies);
return img;
}
BufferedImage findShowsImage()throws IOException{
BufferedImage img=null;
File folderShows = new File(DOCUMENTS_LOCATION+"\\SeriesOrganiser\\Shows");
img = DRY(folderShows);
return img;
}
private BufferedImage DRY(File f)throws IOException{
BufferedImage img=null;
String filepath=String.valueOf(f);
File[] listOfFiles=f.listFiles();
for(File c:listOfFiles){
if(c.isFile()){
if(c.getName().contains(".jpg") || c.getName().contains(".png")){
img=ImageIO.read(new File(filepath+"\\"+c.getName()));
break;
}
}
}
return img;
}
}
To test it out all you guys need to do is put an image in both the Movies and Shows files which is found in a folder called SeriesOrganiser in the "Documents" folder library. (Windows 10).
Wouldn't mind some criticism on the code since I know that I have a lot to improve on.
A couple of comments from the paintComponent() method:
scanForImage findImage = new scanForImage();
Don't do I/O in a painting method. A painting method is for painting only
repaint();
Don't invoke repaint(). This will cause a painting request to be continually added to the event queue.
If you need animation then use a Swing Timer to schedule the animation.
g.dispose();
Don't dispose of the Graphics object that was passed to the method. Other components on the frame may use this Graphics.
You only use the dispose() method on a Graphic object that you create within the paintComponent() method.

Swing Graphics object not drawing over image in JLabel

I am creating a Java program that receives and plots GPS coordinates over a TCP socket connection on a 2D image of a map. The constructor creates the JFrame and graphical components and then starts off a SwingWorker thread to handle getting coordinates from the socket and then drawing an oval to represent the fix on the map.
I am able to always receive data over the connection but the program does not reliably draw points over the image.
Any suggestions and thank you!
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import connectivity.TelitSocketServer;
import calculations.PlottingMath;
import objects.ElephantFix;
#SuppressWarnings("serial")
public class Gui
{
protected JFrame mainWindow;
protected JPanel mapArea = new JPanel();
private ReceiveFix rfix;
public Gui()
{
// TODO Auto-generated constructor stub
mainWindow = new JFrame();
// Load map image
Image map = null;
try
{
File mapImage = new File("map_images/AWE_PLOT.JPG");
map = ImageIO.read(mapImage);
} catch (Exception e)
{
System.out.println(e.toString());
}
JLabel label = new JLabel(new ImageIcon(map));
mapArea.add(label);
// Map Image Dimensions
mainWindow.getContentPane().add(mapArea, "Center");
mainWindow.setSize(471, 670);
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setTitle("ElePlotter");
mainWindow.setVisible(true);
rfix = new ReceiveFix();
rfix.execute();
}
public static void main(String args[])
{
new Gui();
}
private class ReceiveFix extends SwingWorker<Void, ElephantFix>
{
#Override
protected Void doInBackground()
{
// Start the server
String fix = "";
TelitSocketServer currentConnection = new TelitSocketServer();
try
{
// Wait for client to connect
currentConnection.intializeConnection();
while (true)
{
// Parse and convert received GPS fix into arc radians
fix = currentConnection.readLine();
String[] split = fix.split(" ");
double latWholeDegrees = Double.parseDouble(split[0]
.substring(0, 3));
double longWholeDegrees = Double.parseDouble(split[1]
.substring(0, 3));
double latMinutes = Double.parseDouble(split[0]
.substring(3)) * .166667;
double longMinutes = Double.parseDouble(split[1]
.substring(3)) * .166667;
double lat = latWholeDegrees - latMinutes / 10;
double lon = longWholeDegrees + longMinutes / 10;
publish(new ElephantFix(lat, lon));
}
} catch (Exception e)
{
e.printStackTrace();
}
// Return null if somehow unable to publish node data
return null;
}
#Override
protected void process(List<ElephantFix> fixes)
{
int x, y;
// Get the most recently published node
ElephantFix aFix = fixes.get(fixes.size() - 1);
// Translate lat/long into map X/Y pixel
x = PlottingMath.getCurrentPixelX(aFix.getLatitude(),
aFix.getLongitude());
y = PlottingMath.getCurrentPixelY(aFix.getLatitude(),
aFix.getLongitude());
// Plot on image
Graphics g = mapArea.getGraphics();
g.setColor(Color.RED);
g.fillOval(x, y, 15, 15);
// mapArea.validate();
//
// mapArea.repaint();
//
// Gui.this.repaint();
}
}
}
mapArea.getGraphics(); is not how custom painting in Swing is done.
Start by taking a look at Performing Custom Painting for more details.
Adding a component to a container ensures that the child component will always be painted over the container...
JLabel label = new JLabel(new ImageIcon(map));
mapArea.add(label);
You have that backwards. It might be better to add both the label and mapArea to the same container
The next problem you will need to overcome is the fact that you will need to get your painting component to reside over the top of your label.
To this end you could...
Use a glass pane
Use a OverlayLayout
Use a GridBagLayout
Update with example
There are any number of ways to achieve this, you could use multiple components which act as layers onto which you could add other components or perform custom painting.
This is not as easy it as it sounds. Most layout managers don't consider two components sharing the same space. You also have to manage the connection between the layers, so if the map is centered within the lowest layer, but the available space is larger or smaller than the map, offsets begin to drift...messy...
A simpler solution would be to use a single component and render all the content onto it, for example...
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestMap {
public static void main(String[] args) {
new TestMap();
}
public TestMap() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
MapPane pane = new MapPane();
pane.dropPinAt(174, 147);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(pane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MapPane extends JPanel {
private BufferedImage img;
private BufferedImage pin;
private List<Point> pins;
public MapPane() {
pins = new ArrayList<>(25);
try {
img = ImageIO.read(getClass().getResource("/Map.jpg"));
pin = ImageIO.read(getClass().getResource("/Pin.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
#Override
public Dimension getPreferredSize() {
return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(), img.getHeight());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - img.getWidth()) / 2;
int y = (getHeight() - img.getHeight()) / 2;
g2d.drawImage(img, x, y, this);
for (Point p : pins) {
// Offset the pin x/y
// to allow the point to land on the desired location
p.x -= 6;
p.y -= 64;
System.out.println(p);
g2d.drawImage(pin, p.x, p.y, this);
}
g2d.dispose();
}
}
protected void dropPinAt(int x, int y) {
pins.add(new Point(x, y));
}
}
}
You should not be drawing with a Graphics object obtained by calling getGraphics() on a Component. Doing this will give you a Graphics object that doesn't persist and an image that doesn't persist. Instead either draw on a BufferedImage with its Graphics object and display it in the GUI, or iterate through the data collection in the JPanel's paintComponent method and draw with the data obtained.

Big image on smaller JFrame/Applet

I have a pretty big image that I want to display on my JFrame and applet, the JFrame/Applet is 765x503 in size but the image is way larger. I need a way to display the image in real size. How can I make it so I can move the screen around to display some of the image and be able to move around the image?
I want to be able to drag the screen around, not use Scrollbars.
JScrollPane might be the one you need.. add both vertical and horizontal scrolls
The basic idea is you need to provide some kind of offset that should be applied to the image to be drawn.
When the user presses the mouse button at a point on the image, you record the click point. When the drag the mouse, you delta the difference. This gives you the amount the offset is to be moved. Based on the original position, you simply add this difference to the and generate a new offset.
There are other ways to do it, but generally, this is the method I use.
I've also added code to check to see if the drag would put the image out of bounds of the viewable area, you'll have to decide if you want to use it...
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class BigImage {
public static void main(String[] args) {
new BigImage();
}
public BigImage() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private BufferedImage bg;
private Point offset = new Point(0, 0);
public TestPane() {
try {
bg = ImageIO.read(new File("/path/to/image"));
} catch (IOException ex) {
ex.printStackTrace();
}
MouseAdapter ma = new MouseAdapter() {
private Point clickPoint;
private Point origin;
#Override
public void mousePressed(MouseEvent e) {
setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
clickPoint = e.getPoint();
origin = new Point(offset);
}
#Override
public void mouseReleased(MouseEvent e) {
setCursor(Cursor.getDefaultCursor());
clickPoint = null;
}
#Override
public void mouseDragged(MouseEvent e) {
int x = e.getPoint().x - clickPoint.x;
int y = e.getPoint().y - clickPoint.y;
offset.x = origin.x + x;
offset.y = origin.y + y;
if (offset.x > 0) {
offset.x = 0;
}
if (offset.x + bg.getWidth() < getWidth()) {
offset.x = -bg.getWidth() + getWidth();
}
if (offset.y > 0) {
offset.y = 0;
}
if (offset.y + bg.getHeight() < getHeight()) {
offset.y = -bg.getHeight() + getHeight();
}
repaint();
}
};
addMouseListener(ma);
addMouseMotionListener(ma);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(800, 600);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (bg != null) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(bg, offset.x, offset.y, this);
g2d.dispose();
}
}
}
}

Categories

Resources