2016-01-28 3 views
-1

Мне поручено рисовать массивList фигур в java.Как рисовать массивList форм Java

Я чувствую, у меня есть большая частью его прав, есть два метода я запутался однако

метод в ShapeChooserPanel называется public void setPoints(int x, int y)

Поскольку Xs and Ys являются массивами, как бы я установить й и у значения ?

А другой - это последний метод в ShapeChooserPanel. Я не могу узнать, как печатать фигуры в массиве. Он должен нарисовать текущую фигуру в месте щелчка мыши.

Мой код ниже

Основной класс:

import javax.swing.JFrame; 

public class Lab2 { 

    public static void main (String[] args) { 


    JFrame myFrame = new JFrame("Lab 2"); 

    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    myFrame.add(new ShapeChooserPanel()); 
    myFrame.pack(); 
    myFrame.setVisible(true); 








    } 
} 

класс ShapeChooserPanel

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.ArrayList; 

public class ShapeChooserPanel extends JPanel { 

private int currentX; 
private int currentY; 
private Color currentColor; 
private int currentShape; 
private JButton clearBtn; 
private JRadioButton circle, square, triangle, box; 
private DrawingPanel drawingPanel; 
private JPanel controlsPanel; 
//constants representing shape choice 
private final int CIRCLE = 0; 
private final int SQUARE = 1; 
private final int TRIANGLE = 2; 
private final int BOX = 3; 
//constant delta used for setting distance between points 
private final int DELTA = 25; 
private int[] Xs; 
private int[] Ys; 
//store all the shapes to be painted UNCOMMENT when you have Shape.java defined 
ArrayList<Shape> shapes; 

public ShapeChooserPanel(){ 
    //provide some default values paints a circle at (10,10) in blue 
    currentX = 10; 
    currentY = 10; 
    Xs = new int[4];//we will use all 4 points for the square, but only the first 3 for the triangle 
    Ys = new int[4]; 
    setPoints(currentX,currentY); 
    currentShape = CIRCLE; 
    currentColor = Color.red; 
    shapes = new ArrayList<Shape>(); 
    //instantiate the controls panel and set its layout to display everything in a single column 
    controlsPanel = new JPanel(); 
    controlsPanel.setLayout(new BoxLayout(controlsPanel, BoxLayout.Y_AXIS)); 


    //TODO: add clear button * 
    // TODO: define radio buttons * 

    clearBtn = new JButton("Clear"); 
    clearBtn.addActionListener(new ClearListener()); 

    circle = new JRadioButton("Red Circle"); 
    circle.addActionListener(new ShapeListener()); 

    square = new JRadioButton("Cyan Square"); 
    square.addActionListener(new ShapeListener()); 

    triangle = new JRadioButton("Green Triangle"); 
    triangle.addActionListener(new ShapeListener()); 

    box = new JRadioButton("Blue Box"); 
    box.addActionListener(new ShapeListener()); 

    ButtonGroup group = new ButtonGroup(); 
    group.add(clearBtn); 
    group.add(circle); 
    group.add(square); 
    group.add(triangle); 
    group.add(box); 

    controlsPanel.add(clearBtn); 
    controlsPanel.add(circle); 
    controlsPanel.add(square); 
    controlsPanel.add(triangle); 
    controlsPanel.add(box); 



    //TODO: add radio buttons to group * 
    //TODO add listeners to radio buttons * 
    //TODO: add radio buttons to controls panel * 

    drawingPanel = new DrawingPanel(); 

    drawingPanel.setBorder(BorderFactory.createLineBorder(Color.black)); 
    //TODO: set a border around the drawing panel * 
    drawingPanel.setPreferredSize(new Dimension(200,200)); 
    drawingPanel.addMouseListener(new PanelListener()); 

    add(drawingPanel); 
    add(controlsPanel); 
    setPreferredSize(new Dimension (300,400)); 
}//end constructor 

public void setPoints(int x, int y) { 
    //TODO: set Xs and Ys * 

    Xs[0] = x; 
    Ys[0] = y; 

} 

private class ClearListener implements ActionListener{ 
    public void actionPerformed(ActionEvent ae){ 
     shapes.removeAll(shapes); 
     drawingPanel.repaint(); 
    } 
} 
private class PanelListener implements MouseListener { 
    public void mouseClicked(MouseEvent me) { 

     currentX = me.getX(); 
     currentY = me.getY(); 
     //TODO: find coordinates of this mouse click * 

     //TODO: add a new shape to the shapes list* 

     shapes.addAll(shapes); 

     setPoints(currentX, currentY); 
     //TODO: call setPoints with current x and y values * 
     drawingPanel.repaint(); 
    } 
    public void mouseExited(MouseEvent me){} 
    public void mouseEntered(MouseEvent me){} 
    public void mouseReleased(MouseEvent me){} 
    public void mousePressed(MouseEvent me){} 
} 
//Class to listen for radio button changes 
private class ShapeListener implements ActionListener{ 
    public void actionPerformed(ActionEvent me){ 
     //TODO: determine which radio button was clicked * 

     if(me.getSource() == circle){ 
      currentShape = CIRCLE; 
      currentColor = Color.red; 

      } 

      if(me.getSource() == square){ 
       currentShape = SQUARE; 
       currentColor = Color.cyan; 

      } 

      if(me.getSource() == triangle){ 
       currentShape = TRIANGLE; 
       currentColor = Color.green; 

      } 

      if(me.getSource() == box){ 
       currentShape = BOX; 
       currentColor = Color.blue; 

      } 


     //TODO: set current shape and color * 
     drawingPanel.repaint(); 
    } 
} 
private class DrawingPanel extends JPanel { 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 



     //TODO: paint all the shapes in our list 
    } 
} 
} 

и мой класс Shape

import java.awt.Color; 

public class Shape { 

private int x,y; 
private int type; 
private Color c; 

public Shape(int x, int y, int type, Color c) { 
    this.x = x; 
    this.y = y; 
    this.type = type; 
    this.c = c; 

} 

public int x(int x) { 

    return x; 
} 

public int y(int y) { 

    return y; 
} 

public int type(int type) { 

    return type; 
} 

public Color c(Color c) { 

    return c; 
} 
} 
+0

По существу, вы должны позволить ваш класс 'Shape', чтобы иметь возможность рисовать себя, что бы либо требуют, чтобы у вас были подклассы, которые реализуют каждый тип формы или имеют метод 'paint' в вашей форме, который может рисовать каждый отдельный тип. Затем вы зацикливаете свой список и вызываете метод 'paint' для каждого экземпляра' Shape' – MadProgrammer

ответ

1

super.paintComponent (г) запускает paintC omponent от суперкласса JPanel (класс JComponent), чтобы стереть все, что нарисовано на панели. Это полезно для анимации, но не для установки цвета.

Я думаю, вам нужно будет установить цвет фигур в вашем Arraylist<Shape> shapes, когда вы их создадите, тем не менее, было бы полезно увидеть класс Shape. Может быть, вы могли бы создать функцию changeColor(Color ColorToBeSet) в этом классе формы и проходных форм ArrayList назвать его в конце вашего ShapeListener

+0

У меня есть текущий цвет, установленный внутри ShapeListener, он изменяется в зависимости от того, на какой клик нажимается. –

+0

Yup, но вы вызываете перерисовку (); на вашей чертежной панели, но я не вижу нигде, где эта панель чертежа содержит что-либо. Опять же, даже если он будет содержать ваши фигуры, repaint(); заставит рисунокPanel перерисовать себя, а не «фигуры» внутри –

+0

. Так что, если утверждения не в том месте?Когда я получил незавершенный код, там была перекраска, однако я добавил инструкции if, чтобы увидеть, какая кнопка была нажата, и установить текущий цвет, форму. –

1

Вы могли бы ...

Определить «абстрактное» понятие формы, которая имеет основные свойства (расположение и размер), и который может рисовать ...

public abstract class Shape { 

    private int x, y; 
    private int width, height; 
    private Color c; 

    public Shape(int x, int y, int width, int height, Color c) { 
     this.x = x; 
     this.y = y; 
     this.c = c; 
     this.width = width; 
     this.height = height; 
    } 

    public int getHeight() { 
     return height; 
    } 

    public int getWidth() { 
     return width; 
    } 

    public int getX() { 
     return x; 
    } 

    public int getY() { 
     return y; 
    } 

    public Color getColor() { 
     return c; 
    } 

    public abstract void paint(Graphics2D g2d); 
} 

Затем вы можете реализовать отдельные формы ...

public class Rectangle extends Shape { 

    public Rectangle(int x, int y, int width, int height, Color c) { 
     super(x, y, width, height, c); 
    } 

    @Override 
    public void paint(Graphics2D g2d) { 
     g2d.setColor(getColor()); 
     g2d.drawRect(getX(), getY(), getWidth(), getHeight()); 
    } 
} 

public class Oval extends Shape { 

    public Oval(int x, int y, int width, int height, Color c) { 
     super(x, y, width, height, c); 
    } 

    @Override 
    public void paint(Graphics2D g2d) { 
     g2d.setColor(getColor()); 
     g2d.drawOval(getX(), getY(), getWidth(), getHeight()); 
    } 
} 

Затем, вы можете просто вызвать метод paint каждого экземпляра Shape в соответствии с требованиями ...

Shapes

public class TestPane extends JPanel { 

    private List<Shape> shapes; 

    public TestPane() { 
     shapes = new ArrayList<>(25); 
     shapes.add(new Rectangle(10, 10, 20, 20, Color.RED)); 
     shapes.add(new Oval(15, 15, 40, 20, Color.RED)); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(200, 200); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     for (Shape shape : shapes) { 
      Graphics2D g2d = (Graphics2D) g.create(); 
      shape.paint(g2d); 
      g2d.dispose(); 
     } 
    } 

} 
Смежные вопросы