在java中反映文本 [英] Reflecting text in java

查看:120
本文介绍了在java中反映文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如图1所示,我希望时钟为反映





我已经尝试过:
我尝试使用java(Graphics2D)g通过旋转字符串和使用子字符串,但我得到了这个问题:



我的代码为:

  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();

//构造函数
public DigitalClock(){

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

DigitalThread();



$ b //绘制方法
public void paintComponent(Graphics g){
Graphics2D g2d =(Graphics2D)g;

//背景
g2d.setColor(Color.BLACK);
g2d.fillRect(0,0,500,100);

//显示时间
g2d.setColor(Color.WHITE);
g2d.setFont(new Font(g.getFont()。toString(),10,15));
g2d.drawString(timeNow(),100,25);

//显示时间反映
g2d.rotate(Math.PI,100,25);
g2d.drawString(timeNowRot(timeNow()),45,20);




//更改此线程的值
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();
}

}})。




//返回时间
public String timeNow(){

return zero(Calendar.getInstance( ).get(Calendar.HOUR_OF_DAY))+:+
zero(Calendar.getInstance()。get(Calendar.MINUTE))+:+ zero(Calendar.getInstance()。get(Calendar。第二));
}

//返回时间反映
public String timeNowRot(String time){

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


//如果值<10
,则返回0如果(num <10)返回0,则返回0 + NUM;
else return+ num;
}

}

我可以通过使用java 2d来实现这一点?有没有一种方法来垂直再次旋转字符串,所以我没有这个问题,谢谢..

解决方案

其实我找到答案感谢所有人:

这是讽刺,但在问题的第二天,我在大学里就这个问题接受了一课...



有关缩放的信息

  g2d.scale(1,-1); / *按y轴缩放(翻转垂直
|
在这里翻转(缩放(-1,1))|这里是
_ _ _ _ _ _ _ _ _ _
|
在这里翻转(scale(-1,-1))| fliped here(scale(1,-1))
|
* /
g2d.drawString( timeNow(),10,-27);

我还增加了一些效果,所以时钟更多如下所示:

  g2d.setPaint(new GradientPaint(0,-15,color,0,-50,Color.BLACK) ); 


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

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

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

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..

解决方案

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));

这篇关于在java中反映文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆