JPanel がある JFrame があり、paintComponent() を使用してその上にフリーハンドで描画しようとしています。ここで、xPOS および yPOS 変数を mouseDragged メソッド内から変更し、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プログラムからそのJavaプログラムに座標を送ります。
// s.SendData(xpos,ypos);
そして、スレッド内から repaint() メソッドを呼び出すことができます。しかし、なぜこれが起こっていないのか、理解できないようです。どんな助けでも感謝します、ありがとう。
SSCCE PaintBrushServer
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());
}
}
}