如何在paintComponent中翻转图形? java的

时间:2017-03-25 16:50:50

标签: java graphics rotation flip

//This takes all of my events from a google calendar and puts them in an array to be fed to the fullCalendar var eventArray = []; var request = window.gapi.client.calendar.events.list({ 'calendarId': 'primary', 'timeMin': (new Date()).toISOString(), 'showDeleted': false, 'singleEvents': true, 'orderBy': 'startTime' }).then(function(response) { var events = response.result.items; var j = 0; if (events.length > 0) { var eventsArr = []; for (var i = 0; i < events.length; i++) { var event = events[i]; var startDate = event.start.dateTime; var endDate = event.end.dateTime; if (!startDate) { startDate = event.start.date; } if (!endDate) { endDate = event.end.date; } var rowArray = {title: event.summary, start: startDate, end: endDate, id: event.id, allDay: false}; eventsArr.push(rowArray); this.setState({           eventArray: eventsArr         }) } } else { console.log('No upcoming events found.'); } //This creates my fullCalendar $('#calendar').fullCalendar({ header: { left: 'prev,next today', center: 'title', right: 'month,agendaWeek,agendaDay,listYear' }, views: { month: { // name of view titleFormat: 'MMMM' }, agenda: { titleFormat: 'MMM, DD' }, listYear: { titleFormat: ' ' } }, defaultDate: $('#calendar').fullCalendar('today'), allDayDefault: false, navLinks: true, editable: true, nowIndicator: true, eventLimit: true, events: this.state.eventArray }); 的{​​{1}}方法中,我使用paintComponent绘制图像,但我需要水平翻转,并且需要在该方法中进行翻转。我怎么能这样做?

RoosterB是Rooster的孩子,是JPanel的孩子。

JPanel

2 个答案:

答案 0 :(得分:0)

我认为这更接近MCVE:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class APanel extends JPanel{

    private Line2D line;

    APanel(){

        setPreferredSize(new Dimension(600,400));
        line = new Line2D.Double(50,100, 500,250);
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setPaint(Color.BLACK);
        g2d.draw(line);
    }

    public static void main(String[] args){

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        JPanel panel = new APanel();
        frame.getContentPane().add(panel);

        frame.pack();
        frame.setVisible(true);
    }
}

不是吗?
使用它简化了问题和答案:

public class APanel extends JPanel{

    double x1 = 50, y1 = 100, x2 = 500, y2 = 250; //line ends
    double fx1, fx2, fy1, fy2; // fliped line ends
    boolean flip = false;

    APanel(){
        setPreferredSize(new Dimension(600,400));
        JButton flipBtn = new JButton("Flip");
        flipBtn.setPreferredSize(new Dimension(100,30));
        flipBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                flip = ! flip;
                repaint();
            }
        });
        add(flipBtn);
        calculteFlipedLine();
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
        g2d.setPaint(Color.BLACK);

        if(flip) {
            g2d.draw(new Line2D.Double(x1, y1, x2, y2));
        } else {
            g2d.draw(new Line2D.Double(fx1, fy1, fx2, fy2));
        }
    }

    public static void main(String[] args){

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        JPanel panel = new APanel();
        frame.getContentPane().add(panel);

        frame.pack();
        frame.setVisible(true);
    }

    private void calculteFlipedLine() {
        //the calculation here is for simplified
        //horizontal flip. It may vary according to how you
        //define flip
        fx1 = x2; fy1 = y1;
        fx2 = x1; fy2 = y2;
    }
}

答案 1 :(得分:0)

对于水平翻转left <-> right,您需要修改x坐标。一般方法是

x_new = 2 * x_axis - x_old

此处x_axis是&#34;翻转&#34;的x坐标。应该是&#34;周围。&#34;例如,如果要绘制彼此面对的两个镜像,请向左或向右选择x_axis并绘制两次:一次翻转,一次不翻。

要在Java中实现这一点,您应该将所有坐标放在一个数组中。这称为显示列表。然后用循环绘制它们:

static struct MyLine {
  int x0, y0, x1, y1;
  Color color;
  MyLine(int x0, int y0, int x1, int y1, Color color) {
    this.x0 = x0; this.y0 = y0; this.x1 = x1; this.y1 = y1;
    this.color = color;
  }
}

static final Color MY_RED = new Color(56, 118, 29);
static final Color MY_BEAK_COLOR = ...

static final MyLine [] displayList = {
  new MyLine(6, 0, 11, 0, MY_RED);
  ...
};

现在在paintComponent,使用或不使用翻转渲染:

...
if (flipped) {
  int o = 2 * x_axis;
  for (MyLine line : displayList) {
    g.setColor(line.color);
    g.drawLine(o - line.x0, line.y0, o - line.x1, line.y1);
  }
} else {
  for (MyLine line : displayList) {
    g.setColor(line.color);
    g.drawLine(line.x0, line.y0, line.x1, line.y1);
  }
}

flip为真时,您将获得翻转图像,否则正常。如果您想要两者,请删除if。显示列表的一个优点是您可以在不同版本中多次绘制图像组件。