我正在制作一个简单的绘图程序,并且不会显示绘图面板。我不知道Circle类或drawingPanel类是否有问题。 colorButtons和sizeButtons确实显示,但drawingPanel没有。能帮帮我吗,请您花时间和提前谢谢!附:我在drawingFrame中添加了框架。
public class Assign72 {
public static void main(String[] args) {
DrawingFrame f= new DrawingFrame();
f.setTitle("Drawing Program");
f.setSize(462,312);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public class DrawingFrame extends JFrame implements ActionListener {
private DrawingPanel drawPanel;
private JPanel panel;
private JPanel colorPanel;
private JPanel sizePanel;
private JRadioButton redRadioButton;
private JRadioButton blueRadioButton;
private JRadioButton greenRadioButton;
private JRadioButton blackRadioButton;
private JRadioButton smallRadioButton;
private JRadioButton mediumRadioButton;
private JRadioButton largeRadioButton;
private JButton eraseButton;
private ButtonGroup colorButtonGroup;
private ButtonGroup sizeButtonGroup;
final int SMALL = 4;
final int MEDIUM = 8;
final int LARGE = 10;
public DrawingFrame() {
colorPanel = new JPanel();
sizePanel = new JPanel();
redRadioButton = new JRadioButton();
redRadioButton.setText("Red");
colorPanel.add(redRadioButton);
redRadioButton.addActionListener(this);
blueRadioButton = new JRadioButton();
blueRadioButton.setText("Blue");
colorPanel.add(blueRadioButton);
blueRadioButton.addActionListener(this);
greenRadioButton = new JRadioButton();
greenRadioButton.setText("Green");
colorPanel.add(greenRadioButton);
greenRadioButton.addActionListener(this);
blackRadioButton = new JRadioButton();
blackRadioButton.setText("Black");
colorPanel.add(blackRadioButton);
blackRadioButton.addActionListener(this);
smallRadioButton = new JRadioButton();
smallRadioButton.setText("Small");
sizePanel.add(smallRadioButton);
smallRadioButton.addActionListener(this);
mediumRadioButton = new JRadioButton();
mediumRadioButton.setText("Medium");
sizePanel.add(mediumRadioButton);
mediumRadioButton.addActionListener(this);
largeRadioButton = new JRadioButton();
largeRadioButton.setText("Large");
sizePanel.add(largeRadioButton);
largeRadioButton.addActionListener(this);
colorButtonGroup = new ButtonGroup();
sizeButtonGroup = new ButtonGroup();
sizeButtonGroup.add(smallRadioButton);
sizeButtonGroup.add(mediumRadioButton);
sizeButtonGroup.add(largeRadioButton);
colorButtonGroup.add(redRadioButton);
colorButtonGroup.add(blueRadioButton);
colorButtonGroup.add(greenRadioButton);
colorButtonGroup.add(blackRadioButton);
redRadioButton.setSelected(true);
largeRadioButton.setSelected(true);
JPanel configurePanel = new JPanel();
configurePanel.add(new JButton("Configure"));
// Will be right-aligned.
// The full panel.
panel = new JPanel();
panel.setBackground(Color.YELLOW);
drawPanel = new DrawingPanel(Color.RED, LARGE);
drawPanel.setBackground( Color.WHITE );
this.add(sizePanel, BorderLayout.PAGE_START);
this.add(colorPanel, BorderLayout.PAGE_END);
this.add(drawPanel,BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (redRadioButton.isSelected())
drawPanel.setCircleColor(Color.RED);
if (greenRadioButton.isSelected())
drawPanel.setCircleColor(Color.GREEN);
if (blueRadioButton.isSelected())
drawPanel.setCircleColor(Color.BLUE);
if (blackRadioButton.isSelected())
drawPanel.setCircleColor(Color.BLACK);
if (smallRadioButton.isSelected())
drawPanel.setCircleSize(SMALL);
if (mediumRadioButton.isSelected())
drawPanel.setCircleSize(MEDIUM);
if (largeRadioButton.isSelected())
drawPanel.setCircleSize(LARGE);
}
}
public class DrawingPanel extends JPanel implements MouseMotionListener {
private int circleSize;
private Color circleColor;
private Circle newCircle;
private Circle drawingCircle;
private ArrayList<Circle> circleArrayList = new ArrayList<Circle>();
DrawingPanel(Color colorValue, int size) {
addMouseMotionListener(this);
}
public void setCircleColor(Color choice) {
}
public Color getCircleColor() {
return circleColor;
}
public void setCircleDiameter(int diameter) {
}
public int getCircleSize() {
return circleSize;
}
public void paintComponent(Graphics g) {
super.paintComponents(g);
Iterator<Circle> circleIterator = circleArrayList.iterator();
Circle drawCircle;
while (circleIterator.hasNext()) {
drawCircle = (Circle) circleIterator.next();
drawCircle.draw(g);
}
}
public void mouseDragged(MouseEvent event) {
if (event.isMetaDown()) {
newCircle = new Circle(getCircleSize(), event.getPoint(),
this.getBackground());
newCircle = new Circle(getCircleSize(), event.getPoint(),
getCircleColor());
circleArrayList.add(newCircle);
repaint();
}
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
public class Circle {
private int size;
private Point point;
private Color color;
public Circle(int size, Point point, Color color) {
setSize(size);
setLocation(point);
setColor(color);
}
public int getSize() {
return size;
}
public void setSize(int size) {
}
public Point getLocation() {
return point;
}
public void setLocation(Point point) {
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
}
public void draw(Graphics g) {
getLocation();
setColor(color);
setSize(size);
setColor(color);
g.fillOval(point.x, point.y, size, size);
}
}
答案 0 :(得分:1)
问题是您的代码是不成文的:
public void setCircleColor(Color choice) {
}
显然setCircleColor
什么都不做。所以你需要做的第一件事是完成代码编写。填写所有这些空方法:
public void setCircleColor(Color choice) {
circleColor = choice;
}
然后你的draw方法没有设置Graphics上下文的颜色,所以你需要这样做:
public void draw(Graphics g) {
//getLocation();
//setColor(color);
//setSize(size);
//setColor(color);
g.setColor(color);
g.fillOval(point.x, point.y, size, size);
}
(那些其他方法调用不起作用。)
然后在某处您需要为这些值设置默认值,否则它们从0开始为空。
DrawingPanel(Color colorValue, int size) {
setCircleColor(colorValue);
setCircleSize(size);
addMouseMotionListener(this);
}
最后一件事是你的mouseDragged
事件有一些问题。
public void mouseDragged(MouseEvent event) {
//if (event.isMetaDown()) {
//newCircle = new Circle(getCircleSize(), event.getPoint(),
// this.getBackground());
newCircle = new Circle(getCircleSize(), event.getPoint(),
getCircleColor());
circleArrayList.add(newCircle);
repaint();
//}
}
isMetaDown
可能没有按照您的想法行事。元键对应于Mac'Command'键和Windows'Windows'键。newCircle
两次。我不确定哪一个应该是'正确的'。我假设不是第一个,因为第一个将Circle的颜色设置为面板的背景颜色(在这种情况下它将不可见)。在这些变化之后,似乎或多或少有效:
更多说明:
main
将代码包装在invokeLater
中。drawPanel.setCircleSize
似乎应该是drawPanel.setCircleDiameter
)。当您发布完整的代码示例时,除非问题是关于编译错误,否则它们应该是可编译的。以下是包含所有这些更改的功能代码以及一些次要的更改:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Assign72 {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
DrawingFrame f= new DrawingFrame();
f.setTitle("Drawing Program");
f.setSize(462,312);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
});
}
public static class DrawingFrame
extends JFrame
implements ActionListener {
private DrawingPanel drawPanel;
private JPanel panel;
private JPanel colorPanel;
private JPanel sizePanel;
private JRadioButton redRadioButton;
private JRadioButton blueRadioButton;
private JRadioButton greenRadioButton;
private JRadioButton blackRadioButton;
private JRadioButton smallRadioButton;
private JRadioButton mediumRadioButton;
private JRadioButton largeRadioButton;
private JButton eraseButton;
private ButtonGroup colorButtonGroup;
private ButtonGroup sizeButtonGroup;
static final int SMALL = 4; // constants are static
static final int MEDIUM = 8;
static final int LARGE = 10;
public DrawingFrame() {
colorPanel = new JPanel();
sizePanel = new JPanel();
redRadioButton = new JRadioButton();
redRadioButton.setText("Red");
colorPanel.add(redRadioButton);
redRadioButton.addActionListener(this);
blueRadioButton = new JRadioButton();
blueRadioButton.setText("Blue");
colorPanel.add(blueRadioButton);
blueRadioButton.addActionListener(this);
greenRadioButton = new JRadioButton();
greenRadioButton.setText("Green");
colorPanel.add(greenRadioButton);
greenRadioButton.addActionListener(this);
blackRadioButton = new JRadioButton();
blackRadioButton.setText("Black");
colorPanel.add(blackRadioButton);
blackRadioButton.addActionListener(this);
smallRadioButton = new JRadioButton();
smallRadioButton.setText("Small");
sizePanel.add(smallRadioButton);
smallRadioButton.addActionListener(this);
mediumRadioButton = new JRadioButton();
mediumRadioButton.setText("Medium");
sizePanel.add(mediumRadioButton);
mediumRadioButton.addActionListener(this);
largeRadioButton = new JRadioButton();
largeRadioButton.setText("Large");
sizePanel.add(largeRadioButton);
largeRadioButton.addActionListener(this);
colorButtonGroup = new ButtonGroup();
sizeButtonGroup = new ButtonGroup();
sizeButtonGroup.add(smallRadioButton);
sizeButtonGroup.add(mediumRadioButton);
sizeButtonGroup.add(largeRadioButton);
colorButtonGroup.add(redRadioButton);
colorButtonGroup.add(blueRadioButton);
colorButtonGroup.add(greenRadioButton);
colorButtonGroup.add(blackRadioButton);
redRadioButton.setSelected(true);
largeRadioButton.setSelected(true);
JPanel configurePanel = new JPanel();
configurePanel.add(new JButton("Configure"));
// Will be right-aligned.
// The full panel.
panel = new JPanel();
panel.setBackground(Color.YELLOW);
drawPanel = new DrawingPanel(Color.RED, LARGE);
drawPanel.setBackground( Color.WHITE );
this.add(sizePanel, BorderLayout.PAGE_START);
this.add(colorPanel, BorderLayout.PAGE_END);
this.add(drawPanel,BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
if (redRadioButton.isSelected())
drawPanel.setCircleColor(Color.RED);
if (greenRadioButton.isSelected())
drawPanel.setCircleColor(Color.GREEN);
if (blueRadioButton.isSelected())
drawPanel.setCircleColor(Color.BLUE);
if (blackRadioButton.isSelected())
drawPanel.setCircleColor(Color.BLACK);
if (smallRadioButton.isSelected())
drawPanel.setCircleDiameter(SMALL);
if (mediumRadioButton.isSelected())
drawPanel.setCircleDiameter(MEDIUM);
if (largeRadioButton.isSelected())
drawPanel.setCircleDiameter(LARGE);
}
}
public static class DrawingPanel
extends JPanel
implements MouseMotionListener {
private int circleSize;
private Color circleColor;
private Circle newCircle;
private Circle drawingCircle;
private ArrayList<Circle> circleArrayList = new ArrayList<Circle>();
DrawingPanel(Color colorValue, int size) {
setCircleColor(colorValue);
setCircleDiameter(size);
addMouseMotionListener(this);
}
public void setCircleColor(Color choice) {
circleColor = choice;
}
public Color getCircleColor() {
return circleColor;
}
public void setCircleDiameter(int diameter) {
circleSize = diameter;
}
public int getCircleSize() {
return circleSize;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponents(g);
// use regular for loop
for(Circle c : circleArrayList) {
c.draw(g);
}
}
@Override
public void mouseDragged(MouseEvent event) {
newCircle = new Circle(getCircleSize(), event.getPoint(),
getCircleColor());
circleArrayList.add(newCircle);
repaint();
}
@Override
public void mouseMoved(MouseEvent arg0) {}
}
public static class Circle {
private int size;
private Point point;
private Color color;
public Circle(int size, Point point, Color color) {
setSize(size);
setLocation(point);
setColor(color);
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public Point getLocation() {
return point;
}
public void setLocation(Point point) {
this.point = point;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public void draw(Graphics g) {
g.setColor(color);
g.fillOval(point.x, point.y, size, size);
}
}
}
还有很多其他方法可以改进,但有太多建议要做。那将编译并执行。
答案 1 :(得分:0)
很可能你的绘图板存在但不涂漆。
可能是因为它可能不透明。
尝试在绘图面板上使用setOpaque(true);
。