我正在做一项家庭作业,我应该制作一个程序,使您可以绘制自定义形状和线条,并在屏幕上移动它们。
最初,我使用公共空隙paint(g)
进行绘画,但是当我调用重绘时,形状会闪烁。
因此,我切换到paintComponent(g)
。但是,当我尝试绘制形状时,没有任何显示。我相信这是因为它不在顶部绘画JPanel
。
框架在3行中有3个面板BorderLayout
。
[Button JPanel]
[Draw Box JPanel]
[Coordinate Jpanel]
我想绘制的面板自然是Draw Box面板。
这是我目前拥有的代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
public class Main extends JPanel implements ActionListener, MouseListener, MouseMotionListener {
private String colors[] = { "Black", "Red", "Orange", "Yellow", "Green", "Blue" };
private String shapes[] = { "Ellipse", "Line", "Rectangle" };
private JMenuBar toolBar; // flow layout? OR box layout x-Axis
private JPanel drawBox;
private JPanel coordBox;
private JLabel coords;
private JButton undo = new JButton("Undo");
private JButton clear = new JButton("Clear");
private JComboBox color = new JComboBox(colors);
private JComboBox shape = new JComboBox(shapes);
private JCheckBox fill = new JCheckBox("Filled");
public Main() {
JFrame frame = new JFrame("Paint");
frame.setSize(400, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setLayout(new BorderLayout());
/*
* SETUP TOOLBAR
*/
toolBar = new JMenuBar();
toolBar.setLayout(new GridLayout(1, 5));
toolBar.add(undo);
toolBar.add(clear);
toolBar.add(color);
toolBar.add(shape);
toolBar.add(fill);
/*
* ADD ACTION LISTENERS TO BUTTONS
*/
undo.addActionListener(this);
clear.addActionListener(this);
color.addActionListener(this);
shape.addActionListener(this);
fill.addActionListener(this);
/*
* SETUP DRAW BOX
*/
drawBox = new JPanel();
drawBox.setOpaque(true);
drawBox.setPreferredSize(new Dimension(600, 400));
drawBox.addMouseListener(this);
drawBox.addMouseMotionListener(this);
/*
* SETUP COORDINATES
*/
coords = new JLabel();
coordBox = new JPanel();
coordBox.setBackground(new Color(211, 211, 211));
coordBox.setPreferredSize(new Dimension(drawBox.getWidth(), 25));
coords.setText("Coords: [0,0]");
coords.addMouseMotionListener(this);
coordBox.add(coords);
/*
* ADD TO FRAME
*/
frame.add(toolBar, BorderLayout.NORTH);
frame.add(drawBox, BorderLayout.CENTER);
frame.add(coordBox, BorderLayout.SOUTH);
frame.pack();
}
public static void main(String[] args) {
Main m = new Main();
}
public void paintComponent(Graphics g) {
// super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(300, 300, 100, 100);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == undo) {
System.out.println("Undo Last");
}
if (e.getSource() == clear) {
System.out.println("clearing screen");
}
// COLOR DROP DOWN BOX
if (e.getSource() == color) {
JComboBox cb = (JComboBox) e.getSource();
String selected = (String) cb.getSelectedItem();
switch (selected) {
case "Red":
System.out.println("switching to color red");
//ellipse.setColor(Color.RED);
break;
case "Orange":
System.out.println("switch to coor orange");
//ellipse.setColor(Color.ORANGE);
break;
case "Yellow":
System.out.println("switching to color yellow");
break;
case "Green":
System.out.println("Switching to green");
//ellipse.setColor(Color.GREEN);
break;
case "Blue":
System.out.println("Switching to blue");
break;
default:
System.out.println("NOthing selected");
break;
}
}
// SHAPE DROP DOWN BOX
if (e.getSource() == shape) {
JComboBox cb = (JComboBox) e.getSource();
String selected = (String) cb.getSelectedItem();
switch (selected) {
case "Ellipse":
System.out.println("switching to ellipse");
break;
case "Line":
System.out.println("switch to line");
break;
case "Rectangle":
System.out.println("switching to rectangle");
break;
default:
System.out.println("NOthing selected");
break;
}
}
if (e.getSource() == fill) {
JCheckBox cb = (JCheckBox) e.getSource();
if (cb.isSelected()) {
System.out.println("Fill shape");
//ellipse.setFilled(true);
} else {
System.out.println("Empty shape");
//ellipse.setFilled(false);
}
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
// System.out.println("Mouse clicked");
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse entered");
}
@Override
public void mouseExited(MouseEvent arg0) {
coords.setText("Coords: N/A");
}
@Override
public void mousePressed(MouseEvent arg0) {
System.out.println("Mouse pressed");
}
@Override
public void mouseReleased(MouseEvent arg0) {
System.out.println("Mouse Released");
}
@Override
public void mouseDragged(MouseEvent e) {
coords.setText("Coords: [" + e.getX() + "," + e.getY() + "]");
}
@Override
public void mouseMoved(MouseEvent e) {
coords.setText("Coords: [" + e.getX() + "," + e.getY() + "]");
// repaint();
// revalidate();
}
}
您的Main类扩展了JPanel,具有paintComponent方法-但 您从未在GUI上添加Main的实例 ,而是 在GUI上
添加了普通香草的JPanel,drawBox,因此,paintComponent永远不会被调用。
解决方案:添加Main或this
GUI,而不是普通的JPanel。
将显示JFrame和JPanel,但paintComponent方法不在JPanel上绘制。我只看到我添加的JLabel、JTextField和JButton,而没有看到应该在JPanel上绘制的内容。
我正在制作一个简单的2D游戏。每次滴答,我都想检查一个效果队列,该队列将启动一个线程以获得特定效果(淡入淡出过渡、音频淡入淡出等)。例如,在菜单屏幕上按“播放”将向该队列添加“淡出”消息,该消息将被处理并启动一个线程,以在我的游戏面板上绘制一个黑色矩形,并增加阿尔法值。 我重写了绘制组件(),并将我的图形对象发送到我的游戏状态管理器,它将图形对象传递到当前状态的绘制()。我目前没有一个效果状态(也
我在artPanel JPanel类中实现了MouseListener和MouseMotionListener。看来这解决了我的一些问题。但有几个问题: 您可以在这里下载我的java文件,以便更容易地识别问题。 我想不通的问题: “行”按钮无法正确创建行。我一直在玩mouseListener方法,但我无法使其工作。我希望它在单击和拖动鼠标的同时只画一条线。 其他按钮可以正确绘制形状。然而...当我
DrawImage类进行实际绘制
我对用Java制作GUI是新手。据我所知,有一个名为Graphics的类负责在JPanel中绘制形状。当我的应用程序启动时,我调用paintComponent方法,该方法绘制我正在编程的游戏的棋盘,paintComponent方法接受图形g作为输入。然而,稍后,我想更新棋盘,那么当用户做点击之类的事情时,我如何告诉在游戏开始时绘制棋盘的同一个g绘制其他东西呢? 我相信这应该有一个很简单的答案。