私は今かなりの時間学校の課題に取り組んできました。しかし、私は本当に何をすべきか理解できません。明日の予定ですが、私はかなりストレスを感じています。画像をドラッグして回転させるには、paintComponentにどのようなことが必要ですか?
タスクは、私はいくつかの画像を取得し、それらをウィンドウで持っているし、それらを回って回ることができるようになります。
大きな問題は、私がどのように管理するのか分かりません。paintComponent()
です。 私が読んだのは、「必要なときに」自動的に呼び出され、repaint()
に電話するときです。私はそれを働かせるのが難しいと思う。
メインクラス
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class JFrameC extends JFrame{
JPanel panel;
ArrayList <ThePhoto> oneArray = new <ThePhoto> ArrayList();
public JFrameC(){
super("This window");
setLayout(new BorderLayout());
panel = new JPanel();
panel.setBackground(Color.GREEN);
panel.setLayout(null);
add(panel);
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void addPicture(String name){
oneArray.add(new ThePhoto(name, this));
panel.add(oneArray.get(oneArray.size()-1).getJPanel());
}
public void draw(JPanel p){
//One of the tasks is that the image is pressed to end up on top.
//I thought that if I sort of an ArrayList so I can keep track of which one
//is on top. Then be able to paint them in order.
for(ThePhoto x : oneArray){
if(x.getJPanel() == p && oneArray.indexOf(x) != 0){
int i = oneArray.indexOf(x);
for(;i > 0; i--){
ThePhoto temp = oneArray.get(i);
oneArray.set(i, oneArray.get(i-1));
oneArray.set(i-1, temp);
}
break;
}
}
panel.validate();//I can not get this to work
panel.repaint();
getContentPane().validate();//Or this.
getContentPane().repaint();
}
public void paintComponent(Graphics g){
//Is this right?
//What should I write here?
}
public static void main(String[] args) {
JFrameC j = new JFrameC();
j.addPicture("one.gif");
j.addPicture("two.gif");
j.addPicture("three.gif");
j.addPicture("four.gif");
}
}
クラス
import javax.swing.*;
import java.awt.*;
public class ThePhoto{
ImageIcon onePicture;
JLabel l;
JPanel p;
JFrameC k;
int posX = 10;
int posY = 10;
public ThePhoto(String name, JFrameC k){
this.k = k;
onePicture = new ImageIcon(name);
l = new JLabel(onePicture);
p = new JPanel();
p.setLayout(new CardLayout());
p.setBorder(null);
p.setBackground(null);
p.add(l);
p.setBounds(posX, posY, 100, 100);
p.addMouseListener(new HandleMouse(k, this));
p.addMouseMotionListener(new HandleMouse(k, this));
}
public void setX(int x){posX = x;}
public void setY(int y){posY = y;}
public JPanel getJPanel(){return p;}
public void paintComponent(Graphics g){
//Is this right?
//What should I write here?
}
}
のMouseEventクラス
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.*;
import javax.swing.*;
public class HandleMouse extends MouseAdapter implements MouseMotionListener{
JFrame k;
ThePhoto b;
public HandleMouse(JFrameC k, ThePhoto b){
this.k = k;
this.b = b;
}
public void mouseClicked (MouseEvent e) {
k.draw((JPanel)e.getComponent());
}
public void mouseDragged (MouseEvent e) {
e.translatePoint(e.getComponent().getLocation().x, e.getComponent().getLocation().y);
e.getComponent().setLocation(e.getX(), e.getY());
b.setX(e.getX());
b.setY(e.getY());
}
public void mouseReleased(MouseEvent e) {
k.draw((JPanel)e.getComponent());
}
}
明確な問題を要約すると:
はを呼び出すために最善のことを1.Is〜Frame
またはPanel
?として、私はそれが両方の場合にすべての '再描画されるコンテナ'であることを理解しています。もしそうなら、JFrameが望ましいでしょうか?
2. paintComponent()
に何が含まれるべきかに関するルーチン/通常/ルールがありますか?
3.何かアドバイスや批判は大歓迎です。しかし、初心者が理解し、不必要な侮辱をしないように書いてください。
私は宿題をやりたいとは思っていません。しかし、私はより良いものになるためにいくつかアドバイスを求めるだけです。私は初心者であり、初心者が書いたコードに似ていると書いておきたい。
あなたは 'paintComponent'メソッド内で' 'super.paintComponent(g)を必ず呼び出す必要があります。パネル上にペイントしているので、パネル自体で 'repaint()'を呼び出す必要があります。 [カスタム・ペイントの実行](https://docs.oracle.com/javase/tutorial/uiswing/painting/)を参照してください。 –
[カスタム・ペイントの実行](http://docs.oracle.com/javase)から始めてください。/tutorial/uiswing/painting /)と[Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html)を参照して、絵画の仕組みを理解してください – MadProgrammer