Reflecting text in java

2019-09-02 18:00发布

I am trying to make a digital clock in this way:

As shown in the image 1 i want the clock to be reflected.

enter image description here

What i have tried: I tried using java (Graphics2D)g by rotating the string and the using substring but i got this problem:

enter image description here

My code for this:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Calendar;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


  @SuppressWarnings("serial")
public class DigitalClock extends JPanel {

JLabel label = new JLabel();
int c = 0;
Font font = null;
JFrame frame = new JFrame();

//Constructor
public DigitalClock(){

    frame.setSize(700,500);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    frame.add(this);
    frame.setVisible(true);

    DigitalThread();

}


//Paint Method
public void paintComponent(Graphics g){
     Graphics2D g2d = (Graphics2D)g;

     //The background
     g2d.setColor(Color.BLACK);
     g2d.fillRect(0,0,500,100);

     //Show Time
     g2d.setColor(Color.WHITE);
     g2d.setFont(new Font(g.getFont().toString(),10,15));
     g2d.drawString(timeNow(),100, 25);

     //Show time Reflected
     g2d.rotate(Math.PI,100,25);
     g2d.drawString(timeNowRot(timeNow()),45, 20);


}

//Change time Value with this Thread
 public void DigitalThread(){

      new Thread(new Runnable(){
          public void run(){

         boolean flag=true;
        while(flag==true){  
        try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
        repaint();
        }

      }}).start();

  }  


 //Return time
 public String timeNow(){

   return  zero(Calendar.getInstance().get(Calendar.HOUR_OF_DAY))+":"+
           zero(Calendar.getInstance().get(Calendar.MINUTE))+":"+zero(Calendar.getInstance().get(Calendar.SECOND));
 }

 //Return time reflected
 public String timeNowRot(String time){

     return   time.substring(time.length()-1,time.length())+time.substring(time.length()-2,time.length()-1)+":";
 }


 //Add Zero if value<10
 public String zero(int num){        
     if(num<10)  return "0"+num;    
     else  return ""+num;                
 }

}

I can achieve this by using java 2d? is there a method to rotate a string again vertically so i have not this problem thanks..

标签: java graphics
3条回答
迷人小祖宗
2楼-- · 2019-09-02 18:31

Use scale(x, y) which multiplies the x and y coordinates.

g2d.scale(1.0, -1.0);
g2.drawString(....);
g2d.scale(1.0, -1.0); // Undo

This does a transformation where the y axis is reversed. You could also use shear for a paralellogram look.

A g2d.scale(-1.0, 1.0); would draw the string backwards. Can be used in combination with the rotation.

It allt depends on the usage of any rotation and the order, how to scale. Without rotation: scale y by -1:

good
ƃooq
查看更多
走好不送
3楼-- · 2019-09-02 18:36

Actually i found the answer based n your answers thanks all:

It is and irony but the next day after the question i had a lesson at university about that...

Info about scaling:

 g2d.scale(1,-1);  /*scale  by y axes(Flipping vertical
                                                             |
                                    flip here (scale(-1,1) ) |   here it was
                                                    _ _ _ _ _|_ _ _ _ _
                                                             |                                                                
                                   flip here (scale(-1,-1) ) |   fliped here( scale(1,-1) )
                                                             |  
                       */   
g2d.drawString(timeNow(),10,-27);

I also added some more effect so the clock be more realistic like:

 g2d.setPaint(new GradientPaint(0,-15,color,0,-50,Color.BLACK));
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-09-02 18:51

Your link shows a clock face AND its reflection. If that's what you want to do then this is the way.

Draw the clock face into an Image, instead of direct to the Component. Then you render the resulting image into the component twice using drawImage - once the normal way, and once transformed so it looks reflected.

This uses slightly more memory, but only temporarily, and it saves you having to do the paint twice.

If you only want to draw the reflected clock face, then set the image transformation properties in the Graphics to get the reflection.

I'll leave the details as an exercise to the reader.

查看更多
登录 后发表回答