0

我正在做一个简单的喘气项目,允许用户从中选择一个形状,ComboBox然后在面板上绘制它。问题是当用户选择任何类型的形状并尝试在面板上绘制它时,不会出现任何形状。

这是我的小组课

import java.awt.Color;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;

public class Shapes extends JPanel {
    private MyLine[] lines = new MyLine[100];
    private MyRectangle[] recs;
    private MyOval[] ovals;
    int ShapeType = 0;

    int LC = 0;// conter for number of lines
    int RC = 0;// conter for number of rectable
    int OC = 0;// conter for number of ovals

    int x1;
    int y1;
    int x2;
    int y2;

    Shapes() {

        this.setBackground(Color.WHITE);
        this.setBounds(0, 100, 300, 300);
        MouseHandler handler = new MouseHandler();
        this.addMouseListener(handler);
        this.addMouseMotionListener(handler);

    }

    public int getShapeType() {
        return ShapeType;
    }

    public void setShapeType(int ShapeType) {
        this.ShapeType = ShapeType;
    }

    private class MouseHandler extends MouseAdapter implements
            MouseMotionListener {

        @Override
        public void mouseClicked(MouseEvent e) {
            // throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void mousePressed(MouseEvent e) {
            x1 = e.getX();
            y1 = e.getX();
            repaint();
            System.out.println(x1 + " " + y1);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            x2 = e.getX();
            y2 = e.getX();
            lines[LC] = new MyLine(x1, y1, x2, y2);
            LC++;
            System.out.println(x2 + " " + y2);
            repaint();

        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            x2 = e.getX();
            y2 = e.getX();
            System.out.println(x2 + " dragged " + y2);
            repaint();
        }

    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.red);
        if (ShapeType == 0) {
            for (int i = 0; i < LC; i++) {

                lines[i].draw(g);
            }
        }

        if (ShapeType == 1) {
            for (int i = 0; i < LC; i++) {
                lines[i].draw(g);
            }
        }

        if (ShapeType == 1) {
            for (int i = 0; i < LC; i++) {
                lines[i].draw(g);
            }
        }

    }
}

我的线类

package painter;

import java.awt.Graphics;
import java.awt.Color;

public class MyLine extends MyShape {
    MyLine(int x1, int y1, int x2, int y2) {
        this.x1 = x1;
        this.x2 = x2;
        this.y1 = y1;
        this.y2 = y2;
        this.c = c;

    }

    @Override
    public void draw(Graphics g) {
        g.setColor(Color.RED);
        g.drawLine(500, 200, 300, 400);
    }

}
4

1 回答 1

0

你的面板太小了:它是 300 x 300 像素,但是当你画线时,你从 (500,200) 开始,这超出了面板的范围:

g.drawLine(500, 200, 300, 400);

尝试将其拉近左上角:

 g.drawLine(0, 0, 300, 400);
于 2013-05-06T20:24:41.943 回答