javaのJPanel重ね時、repaint
JPanelの上にJPanelを重ね、上のJPanelを透過させました。
この状態で両方のパネルでstartを行います。
下記の結果では、両方が動いています。
目標として、片方のパネルが描写をのこし、片方のパネルが再描写するのが理想です。
誰かお教えください。
import javax.swing.JFrame;
import java.awt.*;
public class Test extends Object
{
public static void main(String[] arguments){
TestViewU Upanel = new TestViewU();//上のパネル
TestViewS Spanel = new TestViewS();//下のパネル
Upanel.setLocation(new Point(0, 0));
Upanel.setLayout(null);
Upanel.setOpaque(false);
Spanel.setLocation(new Point(0, 0));
Spanel.setLayout(null);
Spanel.add(Upanel);
Upanel.setSize(new Dimension(800, 600));
Spanel.setSize(new Dimension(800, 600));
JFrame aWindow;
aWindow = new JFrame("MVC-");
aWindow.getContentPane().add(Spanel);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aWindow.setSize(800, 600);
aWindow.setVisible(true);
Spanel.start();
Upanel.start();
}
}
import java.awt.*;
import javax.swing.*;
public class TestViewS extends JPanel implements Runnable{
static Thread thread;
private static int interval_time = 5;
int a = 100;
int b = 100;
public TestViewS(){
setBackground(Color.WHITE);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(a, b, 10, 10);
}
void start() {
thread = new Thread(this);
thread.start();
}
public void run() {
boolean checkA = true;
boolean checkB = true;
Thread thisThread = Thread.currentThread();
while (thisThread == thread) {
try {
if(checkA && checkB){
a++;}
else if(!checkA && checkB){
b++;}
else if(!checkA && !checkB){
a--;}
else if(checkA && !checkB){
b--;}
if(a == 701){ checkA = !checkA; a--;}
else if(a == 99){ checkA = !checkA; a++;}
if(b == 501){ checkB = !checkB; b--;}
else if(b == 99){ checkB = !checkB; b++;}
repaint();
Thread.sleep(interval_time);
} catch (InterruptedException ie) {
thread = null;
break;}}
}
}
import java.awt.*;
import javax.swing.*;
public class TestViewU extends JPanel implements Runnable{
static Thread thread;
private static int interval_time = 5;
int a = 200;
int b = 200;
public TestViewU(){
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(a, b, 10, 10);
}
void start() {
thread = new Thread(this);
thread.start();
}
public void run() {
boolean checkA = true;
boolean checkB = true;
Thread thisThread = Thread.currentThread();
while (thisThread == thread) {
try {
if(checkA && checkB){
a++;}
else if(!checkA && checkB){
b++;}
else if(!checkA && !checkB){
a--;}
else if(checkA && !checkB){
b--;}
if(a == 601){ checkA = !checkA; a--;}
else if(a == 199){ checkA = !checkA; a++;}
if(b == 401){ checkB = !checkB; b--;}
else if(b == 199){ checkB = !checkB; b++;}
repaint(a, b, 10, 10);
Thread.sleep(interval_time);
} catch (InterruptedException ie) {
thread = null;
break;}}
}
}