2010-08-04 9 views
11

मेरे पास एक छवि है जब मैं बटन पर क्लिक करता हूं तो मैं घुमा रहा हूं। लेकिन यह काम नहीं कर रहा है।स्विंग में धीरे-धीरे एक छवि को घुमाने के लिए कैसे?

मैं छवि को बंद होने तक धीरे-धीरे 90 डिग्री तक घूर्णन छवि देखना चाहता हूं लेकिन ऐसा नहीं होता है। जब बटन क्लिक किया जाता है तो छवि को धीरे-धीरे 90 डिग्री घुमाने चाहिए।

मैंने समस्या का प्रदर्शन करने के लिए एक एसएससीसीई बनाया है। कृपया अपनी पसंद की किसी भी छवि के साथ CrossingPanelSSCE कक्षा में छवि को प्रतिस्थापित करें। बस छवि को अपने images फ़ोल्डर में रखें और इसे images/railCrossing.JPG नाम दें।

RotateButtonSSCE

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.Action; 
import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JPanel; 

public class RotateButtonSSCE extends JPanel implements ActionListener{ 
     private JButton rotate = new JButton("Rotate"); 
     private VisualizationPanelSSCE vis = new VisualizationPanelSSCE(); 

    public RotateButtonSSCE() { 
     this.setBorder(BorderFactory.createTitledBorder("Rotate Button ")); 
     this.rotate.addActionListener(this); 
     this.add(rotate); 
    } 

    public void actionPerformed(ActionEvent ev) { 
     vis.rotatetheCrossing(); 
    } 

} 

CrossingPanelSSCE

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Image; 
import java.awt.Rectangle; 
import java.awt.Toolkit; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.geom.AffineTransform; 

import javax.swing.BorderFactory; 
import javax.swing.JPanel; 
import javax.swing.border.TitledBorder; 

public class CrossingPanelSSCE extends JPanel{ 

    private static final long serialVersionUID = 1L; 

    // private data members 
    private Image crossingImage; 
    private int currentRotationAngle; 
    private int imageWidth; 
    private int imageHeight; 
    private AffineTransform affineTransform; 
    private boolean clockwise; 
    private static int ROTATE_ANGLE_OFFSET = 2; 

    private int xCoordinate; 
    private int yCoordinate; 

    private static javax.swing.Timer timer; 

    private void initialize(){ 
     this.crossingImage = Toolkit.getDefaultToolkit().getImage("images/railCrossing.JPG"); 
     this.imageWidth = this.getCrossingImage().getWidth(this); 
     this.imageHeight = this.getCrossingImage().getHeight(this); 
     this.affineTransform = new AffineTransform(); 
     currentRotationAngle = 90; 
     timer = new javax.swing.Timer(20, new MoveListener()); 
    } 

    public CrossingPanelSSCE(int x, int y) { 
     this.setxCoordinate(x); 
     this.setyCoordinate(y); 
     this.setPreferredSize(new Dimension(50, 50)); 
     this.setBackground(Color.red); 
     TitledBorder border = BorderFactory.createTitledBorder("image"); 
     this.setLayout(new FlowLayout()); 
     this.initialize(); 

    } 


    public void paintComponent(Graphics grp){ 

     Rectangle rect = this.getBounds(); 
     Graphics2D g2d = (Graphics2D)grp; 
     g2d.setColor(Color.BLACK); 
     this.getAffineTransform().setToTranslation(this.getxCoordinate(), this.getyCoordinate()); 

      //rotate with the rotation point as the mid of the image 
     this.getAffineTransform().rotate(Math.toRadians(this.getCurrentRotationAngle()), this.getCrossingImage().getWidth(this) /2, 
             this.getCrossingImage().getHeight(this)/2); 

     //draw the image using the AffineTransform 
     g2d.drawImage(this.getCrossingImage(), this.getAffineTransform(), this); 
    } 


    public void rotateCrossing(){ 
     System.out.println("CurrentRotationAngle: " + currentRotationAngle); 
     this.currentRotationAngle += ROTATE_ANGLE_OFFSET; 
     //int test = currentRotationAngle % 90; 
     if(currentRotationAngle % 90 == 0){ 
     setCurrentRotationAngle(currentRotationAngle); 
     timer.stop();   
     } 

     //repaint the image panel 
     repaint(); 
    } 


    void start() { 
     if (timer != null) { 
      timer.start(); 
     } 
    } 


    private class MoveListener implements ActionListener { 

      public void actionPerformed(ActionEvent e) { 
       rotateCrossing(); 
     } 

    } 

    public Image getCrossingImage() { 
     return crossingImage; 
    } 
    public void setCrossingImage(Image crossingImage) { 
     this.crossingImage = crossingImage; 
    } 

    public int getCurrentRotationAngle() { 
     return currentRotationAngle; 
    } 
    public void setCurrentRotationAngle(int currentRotationAngle) { 
     this.currentRotationAngle = currentRotationAngle; 
    } 

    public int getImageWidth() { 
     return imageWidth; 
    } 
    public void setImageWidth(int imageWidth) { 
     this.imageWidth = imageWidth; 
    } 

    public int getImageHeight() { 
     return imageHeight; 
    } 
    public void setImageHeight(int imageHeight) { 
     this.imageHeight = imageHeight; 
    } 

    public AffineTransform getAffineTransform() { 
     return affineTransform; 
    } 
    public void setAffineTransform(AffineTransform affineTransform) { 
     this.affineTransform = affineTransform; 
    } 

    public boolean isClockwise() { 
     return clockwise; 
    } 
    public void setClockwise(boolean clockwise) { 
     this.clockwise = clockwise; 
    } 

    public int getxCoordinate() { 
     return xCoordinate; 
    } 
    public void setxCoordinate(int xCoordinate) { 
     this.xCoordinate = xCoordinate; 
    } 

    public int getyCoordinate() { 
     return yCoordinate; 
    } 
    public void setyCoordinate(int yCoordinate) { 
     this.yCoordinate = yCoordinate; 
    } 

    public javax.swing.Timer getTimer() { 
     return timer; 
    } 
    public void setTimer(javax.swing.Timer timer) { 
     this.timer = timer; 
    } 



} 

VisualizationPanelSSCE

import gui.CrossingPanel; 
import java.awt.BasicStroke; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.Shape; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.geom.GeneralPath; 

import javax.swing.BorderFactory; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.border.TitledBorder; 

import application.Robot2; 

public class VisualizationPanelSSCE extends JPanel{ 


     //private data members 
     private GeneralPath path; 
     private Shape horizontalRail; 
     private Shape verticalRail; 
     private static int LENGTH = 350; 
     private CrossingPanelSSCE crossingP; 



     private void initializeComponents(){ 
      this.path = new GeneralPath(); 
      this.horizontalRail = this.createHorizontalRail(); 
      this.verticalRail = this.createVerticalRail(); 
      this.crossingP = new CrossingPanelSSCE(328,334); 
     } 

     public VisualizationPanelSSCE(){ 
      this.initializeComponents(); 
      this.setPreferredSize(new Dimension(400,400)); 
      TitledBorder border = BorderFactory.createTitledBorder("Rotation"); 
      this.setBorder(border); 

     } 

     public GeneralPath getPath() { 
      return path; 
     } 
     public void setPath(GeneralPath path) { 
      this.path = path; 
     } 


     private Shape createHorizontalRail(){ 
      this.getPath().moveTo(5, LENGTH); 
      this.getPath().lineTo(330, 350); 
      this.getPath().closePath(); 
      return this.getPath(); 
     } 

     private Shape createVerticalRail(){ 
      this.getPath().moveTo(350, 330); 
      this.getPath().lineTo(350,10); 
      this.getPath().closePath(); 
      return this.getPath(); 
     } 


     public void paintComponent(Graphics comp){ 
      super.paintComponent(comp); 
      Graphics2D comp2D = (Graphics2D)comp; 
      BasicStroke pen = new BasicStroke(15.0F, BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND); 

      comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
            RenderingHints.VALUE_ANTIALIAS_ON); 
      comp2D.setPaint(Color.black); 
      comp2D.setBackground(Color.WHITE); 
      comp2D.draw(this.horizontalRail); 
      this.crossingP.paintComponent(comp2D); 
     } 


     public CrossingPanelSSCE getCrossingP() { 
      return crossingP; 
     } 
     public void setCrossingP(CrossingPanelSSCE crossingP) { 
      this.crossingP = crossingP; 
     } 

     public void rotatetheCrossing(){ 

      Runnable rotateCrossing1 = new Runnable(){ 
       public void run() { 
        crossingP.start(); 
       } 
      }; 
      SwingUtilities.invokeLater(rotateCrossing1); 
     } 


    } 

TestGUISSCE यह मुख्य विधि में शामिल है।

import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.util.Random; 

import javax.swing.*; 

public class TestGUISSCE{ 
    private RotateButtonSSCE rotate = new RotateButtonSSCE(); 
    private VisualizationPanelSSCE vision = new VisualizationPanelSSCE(); 

    public void createGui(){ 

     JFrame frame = new JFrame("Example"); 
     frame.setSize(new Dimension(500, 500)); 


     JPanel pane = new JPanel(); 
     pane.add(this.vision); 
     pane.add(rotate); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(pane); 
     frame.setVisible(true); 

    } 

    public static void main(String[] args) { 
     new TestGUISSCE().createGui(); 
    } 
} 
+0

संभावित डुप्लिकेट [रोटेशन छवि समस्या हल करने में सहायता की आवश्यकता] [http://stackoverflow.com/questions/3371227/need-help-in-solving-rotation-image-problem) – finnw

+0

सभी वर्गों में होना आवश्यक है अलग फाइलें मैंने एक उत्तर पोस्ट किया और उन्होंने इसे -1 दिया। मुझे समझ में नहीं आता कि -1 मतलब क्या है। –

+0

@ कैप: इसे पढ़ें: http://stackoverflow.com/faq – BalusC

उत्तर

4
this.crossingP.paintComponent(comp2D); 

कभी यह करते हैं! आपका क्रॉसिंगपैन किसी भी घटक में नहीं जोड़ा गया है, इसलिए repaint() का कोई प्रभाव नहीं पड़ता है। आप paintComponent() विधि में प्रिंट जोड़कर इसे देख सकते हैं। अतः आप VisualizationPane को CrossingPane जोड़ने की जरूरत:

setLayout(new BorderLayout()); 
add(crossingP, BorderLayout.CENTER); 

छवि केंद्रित के साथ कुछ मुद्दों कर रहे हैं, लेकिन इसे ठीक करने के लिए मुश्किल नहीं होना चाहिए।

पीएस। लेआउट और पेंटिंग के बारे में फिर से पढ़ें।

24

@ tulskiy के सहायक टिप्पणियों के अलावा, मैं दो अंक जोड़ना होगा:

  1. हमेशा के रूप में नीचे दिखाया गया है, event dispatch thread पर अपने जीयूआई का निर्माण।

  2. एक sscceछोटा, स्वयं युक्त, सही (compilable) होना चाहिए, उदाहरण। एक सुविधा के रूप में, दूसरों को कई सार्वजनिक कक्षाओं को फिर से बनाने की आवश्यकता नहीं है; शीर्ष-स्तर (पैकेज-निजी) या नेस्टेड कक्षाओं का उपयोग करें। चूंकि यह एक ग्राफिक्स समस्या है, एक सार्वजनिक या सिंथेटिक छवि का उपयोग करें जो आपकी समस्या को दर्शाता है।

नीचे दिए गए उदाहरण में, paintComponent() ग्राफिक्स संदर्भ के परिवर्तन को रोटेशन को प्रभावित करने के लिए बदल देता है। ध्यान दें कि संचालन घोषणा के आदेश (स्पष्ट) रिवर्स में किया जाता है: सबसे पहले, छवि का केंद्र मूल में अनुवादित होता है; दूसरा, छवि घुमाया गया है; तीसरा, छवि का केंद्र पैनल के केंद्र में अनुवादित है। आप पैनल का आकार बदलकर प्रभाव देख सकते हैं।

परिशिष्ट: AffineTransform का उपयोग करके इस विकल्प को approach भी देखें।

image

package overflow; 

import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.BufferedImage; 
import java.util.Random; 
import javax.swing.*; 

/** 
* @see https://stackoverflow.com/questions/3371227 
* @see https://stackoverflow.com/questions/3405799 
*/ 
public class RotateApp { 

    private static final int N = 3; 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       JFrame frame = new JFrame(); 
       frame.setLayout(new GridLayout(N, N, N, N)); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       for (int i = 0; i < N * N; i++) { 
        frame.add(new RotatePanel()); 
       } 
       frame.pack(); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 


class RotatePanel extends JPanel implements ActionListener { 

    private static final int SIZE = 256; 
    private static double DELTA_THETA = Math.PI/90; 
    private final Timer timer = new Timer(25, this); 
    private Image image = RotatableImage.getImage(SIZE); 
    private double dt = DELTA_THETA; 
    private double theta; 

    public RotatePanel() { 
     this.setBackground(Color.lightGray); 
     this.setPreferredSize(new Dimension(
      image.getWidth(null), image.getHeight(null))); 
     this.addMouseListener(new MouseAdapter() { 

      @Override 
      public void mousePressed(MouseEvent e) { 
       image = RotatableImage.getImage(SIZE); 
       dt = -dt; 
      } 
     }); 
     timer.start(); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2d = (Graphics2D) g; 
     g2d.translate(this.getWidth()/2, this.getHeight()/2); 
     g2d.rotate(theta); 
     g2d.translate(-image.getWidth(this)/2, -image.getHeight(this)/2); 
     g2d.drawImage(image, 0, 0, null); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     theta += dt; 
     repaint(); 
    } 

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

} 

class RotatableImage { 

    private static final Random r = new Random(); 

    static public Image getImage(int size) { 
     BufferedImage bi = new BufferedImage(
      size, size, BufferedImage.TYPE_INT_ARGB); 
     Graphics2D g2d = bi.createGraphics(); 
     g2d.setRenderingHint(
      RenderingHints.KEY_ANTIALIASING, 
      RenderingHints.VALUE_ANTIALIAS_ON); 
     g2d.setPaint(Color.getHSBColor(r.nextFloat(), 1, 1)); 
     g2d.setStroke(new BasicStroke(size/8)); 
     g2d.drawLine(0, size/2, size, size/2); 
     g2d.drawLine(size/2, 0, size/2, size); 
     g2d.dispose(); 
     return bi; 
    } 
} 
+0

आपकी पोस्ट के लिए धन्यवाद। मैं इसकी सराहना करता हूं। –

+0

+1 अच्छा:) ..... –

+0

@trashgod: बिल्कुल वाह! + 100! – ron

5

Rotated Icon के लिए कोड अपने केंद्र के बारे में बारी बारी से करने AffineTransform उपयोग करता है।

+0

+1 यह ' आइकन 'इंटरफ़ेस। – trashgod

+0

@trashgod, ओह, मैंने गलती से इस धागे में पोस्ट किया। मेरा मतलब इस पोस्टिंग में जवाब देना था (http://stackoverflow.com/questions/5722058/rotate-a-picture-around-its-center/5722166#5722166)। लेकिन मुझे लगता है कि आपके पास दो उदाहरणों के साथ उत्तर दिया गया है। – camickr

+1

सबसे सौभाग्यपूर्ण! यह मेरे जवाब को अच्छी तरह से पूरा करता है, और मैंने ऊपर एक लिंक जोड़ा है। सवाल छोड़ दिया गया हो सकता है, लेकिन मैं अभी तक एक समय के लिए जवाब फहराऊंगा। :-) – trashgod

संबंधित मुद्दे