gpt4 book ai didi

java - 在java中加载的图像上绘制对象

转载 作者:行者123 更新时间:2023-11-30 05:54:07 25 4
gpt4 key购买 nike

我需要帮助加载 map 图像并在接收到的位置上绘制一个点。这是服务器的代码:

import java.awt.Dimension;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Server extends JFrame{

private static int PACKETSIZE = 100 ;
private static int WIDTH = 1340;
private static int HEIGHT = 613;
public DatagramSocket socket;
public DrawPoint drawPoint;

public Server() {

setDefaultCloseOperation(EXIT_ON_CLOSE);
drawPoint = new DrawPoint();
drawPoint.setPreferredSize(new Dimension(WIDTH, HEIGHT));
add(drawPoint);
pack();
setVisible(true);
}

//This method converts the lat,lon coordinates to the coordinates of the pixels in the image of the map (which is 1319 by 664)

public String GPStoCoord( double lat, double lon){

double latref = 30.631103;
double lonref = -96.358981;
double yref = 0.015128;
double xref = 0.035589;
int coordx = (int)((latref - lat)/yref*1319);
int coordy = (int)((lon - lonref)/xref*664);

return coordx + "," +coordy;
}

private void GPSlocdraw() {
try {
DatagramPacket packet_GPS1 = new DatagramPacket(new byte[PACKETSIZE], PACKETSIZE);
socket.receive(packet_GPS1);
String sGPS1 = new String(packet_GPS1.getData());
String latlonGPS1[] = sGPS1.split(",", 2);
double lat1 = Double.parseDouble(latlonGPS1[0]);
double lon1 = Double.parseDouble(latlonGPS1[1]);

//converting the GPS data to coordinates
String coord = GPStoCoord(lat1,lon1);
String latlon[] = coord.split(",");
int lat = Integer.parseInt(latlon[0]);
int lon = Integer.parseInt(latlon[1]);

drawPoint.setCoordx(lat);
drawPoint.setCoordy(lon);
drawPoint.repaint();
} catch( IOException e){}
}

public static void main( String args[] ) {

SwingUtilities.invokeLater(() -> new Server().GPSlocdraw());
}
}

这是 DRawPoint 类的代码,我试图在其中绘制加载的图像并在其顶部绘制椭圆形图形,看起来最后绘制的那个会删除另一个。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class DrawPoint extends JPanel {
private int coordx, coordy;

@Override
public void paintComponent( Graphics g){
try {
BufferedImage map = ImageIO.read(new File("CSmap.png"));

super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillOval(coordx,coordy,8,8);
g.drawImage(map,0,0,this);

} catch(IOException e){}
}

//use setters to change the state
void setCoordy( int coordy) {this.coordy = coordy;}
void setCoordx( int coordx) {this.coordx = coordx;}
}

此外,如何绘制一些东西以在整个运行过程中永久保留在图像上?

最佳答案

以下代码演示了在底层图像的不同位置重复绘制一个点。
可以将其复制粘贴到一个文件(Server.java)中并执行:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Server extends JFrame{

private DrawPoint drawPoint;

public Server() {

setDefaultCloseOperation(EXIT_ON_CLOSE);
drawPoint = new DrawPoint();
add(drawPoint);
pack();
setVisible(true);
}

//This conversion is not essential for the question asked. remove to make it Mcve
//public String GPStoCoord( double lat, double lon){}

private void GPSlocdraw() {
//generate random x,y within an arbitrary range
Random rnd = new Random(); int maxY = 350, maxX = 250;
Timer timer = new Timer(1000, e -> { //periodically change coordinates and repaint
drawPoint.setCoordx(rnd.nextInt(maxX));
drawPoint.setCoordy(rnd.nextInt(maxY));
drawPoint.repaint();
});
timer.start();
}

public static void main( String args[] ) {
SwingUtilities.invokeLater(() -> new Server().GPSlocdraw());
}
}

class DrawPoint extends JPanel {

private int coordx, coordy, width, height;
private Image map;

public DrawPoint() {
try {
//to make an mcVe always use publicly available resources
map = ImageIO.read(new URL("http://www.digitalphotoartistry.com/rose1.jpg"));
height = map.getHeight(this);
width = map.getWidth(this);
setPreferredSize(new Dimension(width, height)); //fit panel to image
} catch ( IOException ex) {ex.printStackTrace(); }
}

@Override
public void paintComponent(Graphics g){
//read from file once, not every method run BufferedImage map = ImageIO.read(new File("CSmap.png"));
super.paintComponent(g);
g.setColor(Color.YELLOW);
g.drawImage(map,0,0,this);//image first
g.fillOval(coordx,coordy,8,8); //point on top
}

//use setters to change the state
void setCoordy( int coordy) {this.coordy = coordy;}
void setCoordx( int coordx) {this.coordx = coordx;}
}

至于“如何在整个运行过程中绘制一些东西以永久保留在图像上?” :要获得一个想法,请不要更改椭圆的 x 和 y 坐标,然后看看会发生什么。

关于java - 在java中加载的图像上绘制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53462122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com