老实说,我不知道自己做错了什么。 (虽然我确定它有点相似。)我一直试图调整我的代码数小时无济于事。
我正在尝试剪切图片并显示它们。之后,我会随机定位它们所在的位置并尝试将它们放置在正确的位置。但是,现在,我的任何问题都出现在我的JPanel上。有一次,我的测试代码显示了图像。但是,现在,相同的测试代码不起作用。
如果你能看到我做错了什么/问题出在哪里,请告诉我。希望这是简单而愚蠢的事情。
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;
public class Lab10 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
MyFrame frame = new MyFrame();
MyPanel panel = new MyPanel();
frame.add(panel);
panel.setFocusable(true);
}
}
class MyFrame extends JFrame{
MyFrame(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setSize(800,800);
setResizable(false);
}
}
class MyPanel extends JPanel{
public static final int SIZE = 4;
private int oldx = -1;
private int oldy = -1;
private final String fileName = "houseTyrell.png";
public ImageArray imageArray = new ImageArray(fileName, SIZE);
MyPanel(){
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
if(e.getButton()==1){
if(oldx == -1){
oldx = e.getX();
oldy = e.getY();
}else{
//imageArray.swapPoints(oldx, oldy, e.getX(), e.getY());
oldx = -1;
oldy = -1;
repaint();
}
}
}
});
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
imageArray.draw(g2);
/*Image image2;
try {
image2 = ImageIO.read(new File("houseTyrell.png"));
System.out.println("I should print");
try{
g2.drawImage(image2, 0, 0, null);
} catch(Exception my){
System.out.println("Drawing issue");
}
} catch (IOException ex) {
System.out.println("Reading issue");
}*/
}
}
class ImageArray{
private Square[][] squares;
private String fileName;
private int size;
private int w;
private int h;
public ImageArray(String fileName, int size) {
this.size = size;
this.fileName= fileName;
squares = new Square[size][size];
try {
BufferedImage image = ImageIO.read(new File(fileName));
w = image.getWidth() / size;
h = image.getHeight() / size;
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
squares[row][col] = new Square(
image.getSubimage(row * w , col * h , w, h), row, col, w, h);
}
}
} catch (Exception e) {
System.out.println("Can't open file!");
}
shuffle();
}
//TODO
public void shuffle(){
for(int i = 0; i < size * size; i++){
}
}
//TODO
public void swapPoints(int oldx, int oldy, int newx, int newy){
}
public void draw(Graphics2D g2){
for(int i = 0; i < squares.length; i++){
for(int j = 0; j < squares[0].length; j++){
Square square = squares[i][j];
square.draw(g2);
}
}
}
}
class Square{
private BufferedImage image;
private int row;
private int col;
private int x,y;
private int w;
private int h;
Square(BufferedImage image, int row, int col, int w, int h){
this.image = image;
this.row = row;
this.col = col;
this.x = row * w;
this.y = col * h;
this.w = w;
this.h = h;
}
public void draw(Graphics2D g2){
try{
g2.drawImage(image, x, y, null);
} catch (Exception my){
System.out.println("Square issue");
}
}
public BufferedImage getImage(){
return image;
}
public int getRow(){
return row;
}
public int getCol(){
return col;
}
}
答案 0 :(得分:3)
您在建立用户界面之前可以看到框架,在您添加invalidate
之后尝试在框架上调用validate
,repaint
和panel
...
public static void main(String[] args) {
MyFrame frame = new MyFrame();
MyPanel panel = new MyPanel();
frame.add(panel);
panel.setFocusable(true);
frame.invalidate();
frame.validate();
frame.repaint();
}
坦率地说,你的MyFrame
课程做得很少(除了让你的生活变得更加艰难),我会考虑摆脱它,也许用一个生成器方法替换它,它会返回一个看不见的框架尚未...
您还应该在Event Dispatching Thread的上下文中创建UI,这有助于解决其他“奇怪”问题。
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
MyFrame frame = new MyFrame();
MyPanel panel = new MyPanel();
frame.add(panel);
panel.setFocusable(true);
frame.invalidate();
frame.validate();
frame.repaint();
}
});
}
有关详细信息,请参阅Initial Threads
答案 1 :(得分:1)
在Class Lab10的主要方法中,在JFrame之前初始化JPanel,它应该可以工作。
PS:确保引用的图像&#34; houseTyrell.png&#34;临时存在或使用完全限定的路径进行测试(即c://test//houseTyrell.png)。