我创建了这个类,它创建了一个带有背景图片的JFrame。我想在那张照片上画一个圆圈。但我只能显示图片或图片,图片上不会显示圆圈。我从我的主要叫这个班级。
对不起,如果这是一个新手问题:)
package worldofzuul;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
/**
*
* @author JesperJørgensen
*/
public class GraphicsFrame extends JFrame {
private JPanel man = new JPanel();
void setupframe() {
// Here we create the Frame
JFrame frame = new JFrame(); // create the frame
frame.setLayout(new BorderLayout());
frame.setResizable(false);
frame.setTitle("Zuul the ultimate fridaybar game"); // sets title in top bar of frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // what will happens when the frame close (exit)
//Here we set the background image (the map which we walk in)
ImageIcon icon = new ImageIcon("src/Image/Kort.png");
frame.add(new JLabel(icon));
frame.setContentPane(new DrawPane());
frame.pack(); // sets the size of the frame to fit all objects inside.
frame.setVisible(true); // show the frame
}
class DrawPane extends JPanel {
@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.red);
g.fillRect(20, 20, 100, 200);
}
}
}
答案 0 :(得分:0)
setContentPane
删除了ImageIcon
只有DrawPane
答案 1 :(得分:0)
见评论:
//always post an MCVE
//see http://stackoverflow.com/help/mcve
//include imports
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author JesperJørgensen
*/
public class GraphicsFrame extends JFrame {/*JFrame subclassing is never used*/
//This JPanel is never used
private JPanel man = new JPanel();
Image image;
void setupframe() {
// Here we create the Frame
JFrame frame = new JFrame(); // create the frame
frame.setSize(500,500);
frame.setLayout(new BorderLayout());
frame.setResizable(false);
frame.setTitle("Zuul the ultimate fridaybar game"); // sets title in top bar of frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // what will happens when the frame close (exit)
//initialize image
image = new ImageIcon("src/Image/Kort.png").getImage();
//frame.add(new JLabel(image));
frame.setContentPane(new DrawPane());
//if you don't use preferred sizes pack() will set frame to size 0.
//frame.pack(); // sets the size of the frame to fit all objects inside.
frame.setVisible(true); // show the frame
}
class DrawPane extends JPanel {
@Override
protected void paintComponent(Graphics g) {
//add draw image to paint
g.drawImage(image,0, 0, null);
//this draws a rectangle. change to circle if desired
g.setColor(Color.red);
g.fillRect(20, 20, 100, 200);
}
}
//include main to make it an MCVE
public static void main(String args[]) {
new GraphicsFrame().setupframe();
}
}
要使用此类正在扩展JFrame
的事实,您可能希望像这样实现它:
public class GraphicsFrame extends JFrame {
Image image;
//introduce constructor
public GraphicsFrame() {
setupframe();
}
void setupframe() {
// no need to create a frame. This class is a JFrame
//JFrame frame = new JFrame(); // create the frame
setSize(500,500);
setLayout(new BorderLayout());
setResizable(false);
setTitle("Zuul the ultimate fridaybar game"); // sets title in top bar of frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // what will happens when the frame close (exit)
//initialize image
image = new ImageIcon("src/Image/Kort.png").getImage();
setContentPane(new DrawPane());
setVisible(true); // show the frame
}
class DrawPane extends JPanel {
@Override
protected void paintComponent(Graphics g) {
//add draw image to paint
g.drawImage(image,0, 0, null);
//this draws a circle
g.setColor(Color.red);
g.drawOval(100, 100, 40, 40);
}
}
public static void main(String args[]) {
new GraphicsFrame();
}
}
根据需要,请不要犹豫要求澄清。