我有一个 JFrame,上面有一个 JPanel,我正在尝试使用paintComponent() 对其进行一些自由手绘。现在,我从 mouseDragged 方法内部更改 xPOS 和 yPOS 变量,然后调用 repaint(),所以它工作正常
//**PROGRAM 1**-My DrawPanel Class
public class DrawPanel extends JPanel implements Runnable, MouseMotionListener{
int xpos = 0;
int ypos = 0;
String message;
DrawPanel(){
//constructor ini
addMouseMotionListener(this);
setBackground(Color.green);
setSize(500, 400);
setBounds(10, 10, 500, 400);
}
public void paintComponent(Graphics g) {
g.setColor(Color.red);
g.fillOval(xpos, ypos, 5, 5);
}
@Override
public void mouseDragged(MouseEvent arg0) {
xpos= arg0.getX();
ypos= arg0.getY();
// s.SendData(xpos,ypos);
repaint();
}
public void run() {
try{
while((message = reader.readLine())!=null) {
int in = message.indexOf("#");
xpos = Integer.parseInt(message.substring(0, in));
ypos = Integer.parseInt(message.substring(in+1));
System.out.print(xpos+" "+ypos);
repaint();
}}catch(Exception e){e.printStackTrace();}
}
}
我有一个线程,它通过套接字从其他一些java程序接受一些新坐标,我能够接受它们,我的
System.out.print(xpos+" "+ypos);
工作正常,但之后的 repaint() 方法似乎不起作用,没有错误,没有任何反应。
仅供参考,我提到的另一个 java 程序具有相同的结构,我将坐标从这个 java 程序发送到那个,
// s.SendData(xpos,ypos);
并且可以从线程内部调用 repaint() 方法。但似乎无法理解,为什么它没有发生在这一点上。任何帮助将不胜感激,谢谢。
SSCCE画笔服务器
import java.awt.*;
import java.awt.event.*;
//import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class PaintBrush {
DrawPanel dr = new DrawPanel();
myFrame mf;
Graphics g1;
boolean flag=false;
server s;
BufferedReader reader;
PaintBrush(){
myFrame mf = new myFrame();
s = new server();
}
public static void main(String[] args) {
new PaintBrush();
}
public class ButtonPanel extends JPanel{
ButtonPanel(){
add(new myButton());
}
}
public class DrawPanel extends JPanel implements Runnable, MouseMotionListener{
int xpos = 0;
int ypos = 0;
String message;
DrawPanel(){
//constructor ini
addMouseMotionListener(this);
setBackground(Color.green);
setSize(500, 400);
setBounds(10, 10, 500, 400);
//s = new server();
}
public void paintComponent(Graphics g) {
g.setColor(Color.red);
g.fillOval(xpos, ypos, 5, 5);
}
@Override
public void mouseDragged(MouseEvent arg0) {
xpos= arg0.getX();
ypos= arg0.getY();
s.SendData(xpos,ypos);
repaint();
}
@Override
public void run() {
try{
while((message = reader.readLine())!=null) {
int in = message.indexOf("#");
xpos = Integer.parseInt(message.substring(0, in));
ypos = Integer.parseInt(message.substring(in+1));
// System.out.print(xpos+" "+ypos);
repaint();
}}catch(Exception e){e.printStackTrace();}
}
}
public class myFrame extends JFrame{
myFrame(){
DrawPanel dr = new DrawPanel();
//setBackground(Color.black);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(665,490);
getContentPane().add(BorderLayout.CENTER,dr);
getContentPane().add(BorderLayout.NORTH, new ButtonPanel());
}
}public class myButton extends JButton{
myButton(){
setText("PaintBrushServer");
// addActionListener(new listen());
}
}
public class server {
ServerSocket ssock;
Socket clientSocket;
PrintWriter writer;
DataOutputStream os = null;
server(){
//ini server
try {
ssock = new ServerSocket(42422);
System.out.println("waiting");
clientSocket = ssock.accept();
System.out.println(clientSocket.getRemoteSocketAddress()+" connected\n");
//writer = new PrintWriter(.getOutputStream());
InputStreamReader streamReader = new InputStreamReader(clientSocket.getInputStream());
reader = new BufferedReader(streamReader);
os = new DataOutputStream(clientSocket.getOutputStream());
new Thread(dr).start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void SendData(int x, int y){
try{
os.writeBytes(x+"#"+y+"\n");
}catch(Exception e){e.printStackTrace();}
}
}
}
SSCCE-PaintBrushClient
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Client {
int xpos = 0;
int ypos = 0;
Boolean flag = false;
ClientConnect cc ;
DataOutputStream os = null;
BufferedReader reader;
Client(){
//server connect
ClientConnect cc = new ClientConnect();
}
public static void main(String[] args) {
new Client();
}
public class ClientConnect {
private Socket sock;
String message;
ClientConnect(){
try {
sock = new Socket("127.0.0.1", 42422);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
os = new DataOutputStream(sock.getOutputStream());
//sr = new ServerReader();
//sr.start();
new myFrame();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
public void SendData(int x, int y){
try{
os.writeBytes(x+"#"+y+"\n");
}catch(Exception e){e.printStackTrace();}
}
public class DrawPanel extends JPanel implements Runnable, MouseMotionListener{
String message;
DrawPanel(){
//constructor ini
addMouseMotionListener(this);
setBackground(Color.green);
setSize(500, 400);
setBounds(10, 10, 500, 400);
}
public void paintComponent(Graphics g) {
g.setColor(Color.red);
g.fillOval(xpos, ypos, 5, 5);
}
@Override
public void run() {
// TODO Auto-generated method stub
try{
while((message = reader.readLine())!=null) {
int in = message.indexOf("#");
xpos = Integer.parseInt(message.substring(0, in));
ypos = Integer.parseInt(message.substring(in+1));
//System.out.println(message);
System.out.println(xpos+" "+ypos);
repaint();
}}catch(Exception e){e.printStackTrace();}
}
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
xpos= arg0.getX();
ypos= arg0.getY();
//flag = true;
//System.out.println(xpos);
//SendData(xpos,ypos);
repaint();
}
public void mouseMoved(MouseEvent arg0) {
}
}
public class myButton extends JButton{
myButton(){
setText("PaintBrushClient");
//addActionListener(new listen());
}
}
public class ButtonPanel extends JPanel{
ButtonPanel(){
add(new myButton());
}
}
public class myFrame extends JFrame{
myFrame(){
DrawPanel dr = new DrawPanel();
new Thread(dr).start();
setResizable(false);
setBackground(Color.black);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(665,490);
getContentPane().add(BorderLayout.CENTER,dr);
getContentPane().add(BorderLayout.NORTH, new ButtonPanel());
}
}
}