有没有办法在java中获得更高质量的小文本?

dgsult0t  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(126)

我想做一个基于元素和基本化学的无聊游戏,我想得到一个元素周期表。到目前为止,它还在工作,但是一些元素名称的文本太小了,以至于无法阅读。我用来确定文本大小的代码是这样的。

private void drawCenteredString(Graphics g, String text, int y) {
    //creates centered text for the element name
    int fontSize = (int)(size/5);
    String fontType = "Florett"; //font type
    FontMetrics metrics = g.getFontMetrics(new Font(fontType, Font.BOLD, fontSize));
    while(true){
        //tests if the width of the given text with the font type is larger than the
        //element square
        if(metrics.stringWidth(text) > (size-5)){
            //if it is, reduce the font size, and test again
            fontSize--;
            metrics = g.getFontMetrics(new Font(fontType, Font.BOLD, fontSize));
        } else{
            //if it's not, apply the font and break the loop
            g.setFont(new Font(fontType, Font.BOLD, fontSize));
            break;
        }
    }
    //this draws the text
    int x = ((int)(size/2)+this.x)-(metrics.stringWidth(text)/2);
    g.drawString(text, x, y);
}

这就是结果。
我一直在寻找我能搜索到的任何地方,但我没有找到任何关于如何提高java文本质量的信息。我见过有人和我一样的问题 java.awt.Graphics2D 文本,所以我不认为切换到它会比 java.awt.Graphics . 你们有什么想法吗?这不是一个太大的问题,只是有点恼人,你几乎看不懂任何一个大名字。
我还可以添加一个系统,当你将鼠标悬停在元素正方形上时,它允许元素正方形被放大,但是如果你只是想得到一个整体视图,那就有点不切实际了。感谢所有提供帮助的人。
编辑:我找到了一个方法来解决这个问题。当你点击其中一个方块时,它会放大以便看得更清楚。
正常的
放大
这不是一个非常实用的解决方案,但直到我可以找到一种方法来获得更高质量的文本显示,这将不得不工作。
如果有人想对我的代码进行编辑,请随意。非常感谢您的帮助。你也可以使用这个,如果你想实施到你的项目之一,不需要信用我。
运行程序.java

import javax.swing.JFrame;
public class Runner{
    public static void main(String[] args){
        JFrame fr = new JFrame("Periodic Journey");
        Screen s = new Screen();
        fr.add(s);
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fr.pack();
        fr.setVisible(true);
    }
}

屏幕.java

import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Screen extends JPanel implements MouseListener{
    Periodic_table[][] table;
    Periodic_table select;
    private int size, x, y;
    private boolean enlarge;
    public Screen(){
        addMouseListener(this);
        setFocusable(true);
        size = 64;
        x = 20;
        y = 20;
        int atomic_number = 1;
        table = new Periodic_table[9][18];
        //sets up periodic table (look at table for reference)
        for(int a = 0; a<table.length; a++){
            for(int b = 0; b<table[a].length; b++){
                //a is the y value, b is the x value
                int x = this.x+(size*b);
                int y = this.y+(size*a);
                if(a==0){
                    if(b>=1 && b<=16){
                        table[a][b] = new Periodic_table(x, y, size, 0);
                    } else if(b==0 || b==17){
                        table[a][b] = new Periodic_table(x, y, size, atomic_number);
                        atomic_number++;
                    }
                } else if(a==1 || a==2){
                    if(b>=2 && b<=11){
                        table[a][b] = new Periodic_table(x, y, size, 0);
                    } else{
                        table[a][b] = new Periodic_table(x, y, size, atomic_number);
                        atomic_number++;
                    }
                } else if(a==3 || a==4){
                    table[a][b] = new Periodic_table(x, y, size, atomic_number);
                    atomic_number++;
                }else if(a==5 || a==6){
                    if(b==2){
                        if(a==5){
                            table[a][b] = new Periodic_table(x, y, size, -1);
                            atomic_number = 72;
                        } else{
                            table[a][b] = new Periodic_table(x, y, size, -2);
                            atomic_number = 104;
                        }
                    } else{
                        table[a][b] = new Periodic_table(x, y, size, atomic_number);
                        atomic_number++;
                    }
                } else if(a==7 || a==8){
                    if((b>=0 && b<2) || b==17){
                        table[a][b] = new Periodic_table(x, y, size, 0);
                        if(a==7){
                            atomic_number = 57;
                        } else{
                            atomic_number = 89;
                        }
                    } else{
                        table[a][b] = new Periodic_table(x, y, size, atomic_number);
                        atomic_number++;
                    }
                } else{
                    table[a][b] = new Periodic_table(x, y, size, atomic_number);
                    atomic_number++;
                }
            }
        }
        select = new Periodic_table(0, 0, 0, 0);
        enlarge = false;
    }
    public Dimension getPreferredSize(){
        return new Dimension(1200, 700);
        //this is the size of the window
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g; //just here to set up for the future
        for(int a = 0; a<table.length; a++){
            for(int b = 0; b<table[a].length; b++){
                if(a>=0 && a<=8 && b>=0 && b<=17){
                    table[a][b].drawMe(g);
                }
            }
        }
        if(enlarge){
            select.drawMe(g);
        }
    }
    public void mousePressed(MouseEvent e){
        int mouseX = (int)((e.getX()-this.x)/size);
        int mouseY = (int)((e.getY()-this.y)/size);
        int mx = mouseX*size;
        int my = mouseY*size;
        if(mouseX>=0 && mouseX<=17 && mouseY>=0 && mouseY<=8){
            if(table[mouseY][mouseX].getAtomicNumber()!=0 && !enlarge && (mouseX!=2 || (mouseY!=5 && mouseY!=6))){
                int limit = 15;
                int otherLimit = 1200-limit;
                int otherLimitB = 700-limit;
                if(mx>=limit && (mx+(size*2))<=otherLimit && my>=limit && (my+(size*2)<=otherLimitB)){
                    select.enlarge(mx, my, (size*2), table[mouseY][mouseX].getAtomicNumber());
                } else if((mx+(size*2))>otherLimit && my<limit){
                    select.enlarge(otherLimit-(size*2), limit, (size*2), table[mouseY][mouseX].getAtomicNumber());
                } else if((mx+(size*2))>otherLimit && (my+(size*2))>otherLimitB){
                    select.enlarge(otherLimit-(size*2), otherLimitB-(size*2), (size*2), table[mouseY][mouseX].getAtomicNumber());
                } else if(mx<limit && (my+(size*2))>otherLimitB){
                    select.enlarge(limit, otherLimitB-(size*2), (size*2), table[mouseY][mouseX].getAtomicNumber());
                } else if(mx<limit && my<limit){
                    select.enlarge(limit, limit, (size*2), table[mouseY][mouseX].getAtomicNumber());
                } else if(mx<limit){
                    select.enlarge(limit, my, (size*2), table[mouseY][mouseX].getAtomicNumber());
                }  else if((mx+(size*2))>otherLimit){
                    select.enlarge(otherLimit-(size*2), my, (size*2), table[mouseY][mouseX].getAtomicNumber());
                } else if(my<limit){
                    select.enlarge(mx, limit, (size*2), table[mouseY][mouseX].getAtomicNumber());
                } else if((my+(size*2))>otherLimitB){
                    select.enlarge(mx, otherLimitB-(size*2), (size*2), table[mouseY][mouseX].getAtomicNumber());
                }
                enlarge = true;
            } else{
                enlarge = false;
            }
        }
        repaint();
    }
    public void mouseReleased(MouseEvent e){}
    public void mouseClicked(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
}

周期表.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Rectangle;
public class Periodic_table{
    private Color alkaline_metal, alkaline_earth, transition_metal, basic_metal;
    private Color semimetal, nonmetal, halogen, noble_gas, lanthanide, actinide;
    private Color black, white;
    private String[] atomicList, valueList;
    private int x, y, atomic_number, size;
    private Font a_num, element, element_name, element_name_long;
    public Periodic_table(int x, int y, int size, int atomic_num){
        atomicList = new String[118]; //list of element names
        valueList = new String[118]; //list of element abbreviations
        setList(); //creates both lists
        atomic_number = atomic_num;
        this.x = x;
        this.y = y;
        this.size = size;
        black = new Color(0, 0, 0);
        white = new Color(255, 255, 255);
        int maxColor = 255;
        //sets the color for the different types of elements
        alkaline_metal = new Color(maxColor, 0, 0);
        alkaline_earth = new Color(maxColor, (int)(maxColor/2), 0);
        transition_metal = new Color(maxColor, maxColor, 0);
        basic_metal = new Color(0, maxColor, 0);
        semimetal = new Color(0, maxColor, maxColor);
        nonmetal = new Color((int)(maxColor/4), (int)(maxColor/4), maxColor);
        halogen = new Color(maxColor, 0, maxColor);
        noble_gas = new Color((int)(maxColor/2), 0, maxColor);
        lanthanide = new Color(maxColor, (int)(maxColor/2), maxColor);
        actinide = new Color(maxColor, (int)(maxColor/4), (int)((maxColor/4)*3));
    }
    public void drawMe(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
        BasicStroke w1 = new BasicStroke(1);
        BasicStroke w2 = new BasicStroke(2);
        if(atomic_number!=0){ //tests if the square is empty (atomic number is 0 if it's empty)
            //draws the color of the element type
            if(atomic_number == 3 || atomic_number == 11 || atomic_number == 19 || atomic_number == 37 || atomic_number == 55 || atomic_number == 87){
                g.setColor(alkaline_metal);
            } else if(atomic_number == 4 || atomic_number == 12 || atomic_number == 20 || atomic_number == 38 || atomic_number == 56 || atomic_number == 88){
                g.setColor(alkaline_earth);
            } else if((atomic_number >= 21 && atomic_number <= 30) || (atomic_number >= 39 && atomic_number <=48) || (atomic_number >= 72 && atomic_number <=80) || (atomic_number >= 104 && atomic_number <= 112)){
                g.setColor(transition_metal);
            } else if(atomic_number == 5 || atomic_number == 14 || atomic_number == 32 || atomic_number == 33 || atomic_number == 51 || atomic_number == 52 || atomic_number == 84){
                g.setColor(semimetal);
            } else if(atomic_number == 1 || (atomic_number>=6 && atomic_number<=8) || atomic_number == 15 || atomic_number == 16 || atomic_number == 34){
                g.setColor(nonmetal);
            } else if(atomic_number == 13 || atomic_number == 31 || atomic_number == 49 || atomic_number == 50 || (atomic_number >= 81 && atomic_number <=83) || (atomic_number >= 113 && atomic_number <= 116)){
                g.setColor(basic_metal);
            } else if(atomic_number == 9 || atomic_number == 17 || atomic_number == 35 || atomic_number == 53 || atomic_number == 85 || atomic_number == 117){
                g.setColor(halogen);
            } else if(atomic_number == 2 || atomic_number == 10 || atomic_number == 18 || atomic_number == 36 || atomic_number == 54 || atomic_number == 86 || atomic_number == 118){
                g.setColor(noble_gas);
            } else if((atomic_number >= 57 && atomic_number <= 71) || atomic_number == -1){
                g.setColor(lanthanide);
            } else if((atomic_number >= 89 && atomic_number <= 103) || atomic_number == -2){
                g.setColor(actinide);
            }
            g.fillRect(x, y, size, size);
            g2.setStroke(w2);
            g.setColor(black);
            g.drawRect(x, y, size, size);
            g2.setStroke(w1);
            //draws the text
            if(atomic_number>0){
                g.setFont(new Font("Florett", Font.BOLD, (int)(size/5)));
                g.drawString(atomic_number + "", x+((int)(size/16)), y+((int)(size/4)));
                drawCenteredString(g, valueList[atomic_number-1]);
                drawCenteredString(g, atomicList[atomic_number-1], y+(size-((int)(size/8))));
                g.setColor(black);
            } else if(atomic_number==-1){
                drawCenteredString(g, "Lanthanides");
                g.setFont(new Font("Florett", Font.BOLD, (int)(size/5)));
                g.drawString("57-71", x+((int)(size/16)), y+((int)(size/4)));
            } else if(atomic_number==-2){
                drawCenteredString(g, "Actinides");
                g.setFont(new Font("Florett", Font.BOLD, (int)(size/5)));
                g.drawString("89-103", x+((int)(size/16)), y+((int)(size/4)));
            }
        }
    }
    private void drawCenteredString(Graphics g, String text) {
        //creates centered text for the element abbreviation
        int fontSize = (int)(size/3);
        String fontType = "Florett";
        FontMetrics metrics = g.getFontMetrics(new Font(fontType, Font.BOLD, fontSize));
        while(true){
            //tests if the width of the given text with the font type is larger than the
            //element square
            if(metrics.stringWidth(text) > (size-5)){
                //if it is, reduce the font size, and test again
                fontSize--;
                metrics = g.getFontMetrics(new Font(fontType, Font.BOLD, fontSize));
            } else{
                //if it's not, apply the font and break the loop
                g.setFont(new Font(fontType, Font.BOLD, fontSize));
                break;
            }
        }
        int x = ((int)(size/2)+this.x)-(metrics.stringWidth(text)/2);
        int y = (((int)(size/2)+this.y)-(metrics.getHeight()/2)) + metrics.getAscent();
        g.setFont(new Font(fontType, Font.BOLD, fontSize));
        g.drawString(text, x, y);
    }
    private void drawCenteredString(Graphics g, String text, int y) {
        //creates centered text for the element name
        int fontSize = (int)(size/5);
        String fontType = "Florett"; //font type
        FontMetrics metrics = g.getFontMetrics(new Font(fontType, Font.BOLD, fontSize));
        while(true){
            //tests if the width of the given text with the font type is larger than the
            //element square
            if(metrics.stringWidth(text) > (size-5)){
                //if it is, reduce the font size, and test again
                fontSize--;
                metrics = g.getFontMetrics(new Font(fontType, Font.BOLD, fontSize));
            } else{
                //if it's not, apply the font and break the loop
                g.setFont(new Font(fontType, Font.BOLD, fontSize));
                break;
            }
        }
        //this draws the text
        int x = ((int)(size/2)+this.x)-(metrics.stringWidth(text)/2);
        g.drawString(text, x, y);
    }
    public int getAtomicNumber(){
        return atomic_number;
    }
    public void enlarge(int x, int y, int size, int atomic_num){
        this.x = x;
        this.y = y;
        this.size = size;
        atomic_number = atomic_num;
    }
    private void setList(){
        //sets up both lists
        atomicList[0] = "Hydrogen";
        valueList[0] = "H";
        atomicList[1] = "Helium";
        valueList[1] = "He";
        atomicList[2] = "Lithium";
        valueList[2] = "Li";
        atomicList[3] = "Beryllium";
        valueList[3] = "Be";
        atomicList[4] = "Boron";
        valueList[4] = "B";
        atomicList[5] = "Carbon";
        valueList[5] = "C";
        atomicList[6] = "Nitrogen";
        valueList[6] = "N";
        atomicList[7] = "Oxygen";
        valueList[7] = "O";
        atomicList[8] = "Fluorine";
        valueList[8] = "F";
        atomicList[9] = "Neon";
        valueList[9] = "Ne";
        atomicList[10] = "Sodium";
        valueList[10] = "Na";
        atomicList[11] = "Magnesium";
        valueList[11] = "Mg";
        atomicList[12] = "Aluminum";
        valueList[12] = "Al";
        atomicList[13] = "Silicon";
        valueList[13] = "Si";
        atomicList[14] = "Phosphorus";
        valueList[14] = "P";
        atomicList[15] = "Sulfur";
        valueList[15] = "S";
        atomicList[16] = "Chlorine";
        valueList[16] = "Cl";
        atomicList[17] = "Argon";
        valueList[17] = "Ar";
        atomicList[18] = "Potassium";
        valueList[18] = "K";
        atomicList[19] = "Calcium";
        valueList[19] = "Ca";
        atomicList[20] = "Scandium";
        valueList[20] = "Sc";
        atomicList[21] = "Titanium";
        valueList[21] = "Ti";
        atomicList[22] = "Vanadium";
        valueList[22] = "V";
        atomicList[23] = "Chromium";
        valueList[23] = "Cr";
        atomicList[24] = "Manganese";
        valueList[24] = "Mn";
        atomicList[25] = "Iron";
        valueList[25] = "Fe";
        atomicList[26] = "Cobalt";
        valueList[26] = "Co";
        atomicList[27] = "Nickel";
        valueList[27] = "Ni";
        atomicList[28] = "Copper";
        valueList[28] = "Cu";
        atomicList[29] = "Zinc";
        valueList[29] = "Zn";
        atomicList[30] = "Gallium";
        valueList[30] = "Ga";
        atomicList[31] = "Germanium";
        valueList[31] = "Ge";
        atomicList[32] = "Arsenic";
        valueList[32] = "As";
        atomicList[33] = "Selenium";
        valueList[33] = "Se";
        atomicList[34] = "Bromine";
        valueList[34] = "Br";
        atomicList[35] = "Krypton";
        valueList[35] = "Kr";
        atomicList[36] = "Rubidium";
        valueList[36] = "Rb";
        atomicList[37] = "Strontium";
        valueList[37] = "Sr";
        atomicList[38] = "Yttrium";
        valueList[38] = "Y";
        atomicList[39] = "Zirconium";
        valueList[39] = "Zr";
        atomicList[40] = "Niobium";
        valueList[40] = "Nb";
        atomicList[41] = "Molybdenum";
        valueList[41] = "Mo";
        atomicList[42] = "Technetium";
        valueList[42] = "Tc";
        atomicList[43] = "Ruthenium";
        valueList[43] = "Ru";
        atomicList[44] = "Rhodium";
        valueList[44] = "Rh";
        atomicList[45] = "Palladium";
        valueList[45] = "Pd";
        atomicList[46] = "Silver";
        valueList[46] = "Ag";
        atomicList[47] = "Cadmium";
        valueList[47] = "Cd";
        atomicList[48] = "Indium";
        valueList[48] = "In";
        atomicList[49] = "Tin";
        valueList[49] = "Sn";
        atomicList[50] = "Antimony";
        valueList[50] = "Sb";
        atomicList[51] = "Tellurium";
        valueList[51] = "Te";
        atomicList[52] = "Iodine";
        valueList[52] = "I";
        atomicList[53] = "Xenon";
        valueList[53] = "Xe";
        atomicList[54] = "Cesium";
        valueList[54] = "Cs";
        atomicList[55] = "Barium";
        valueList[55] = "Ba";
        atomicList[56] = "Lanthanum";
        valueList[56] = "La";
        atomicList[57] = "Cerium";
        valueList[57] = "Ce";
        atomicList[58] = "Praseodymium";
        valueList[58] = "Pr";
        atomicList[59] = "Neodymium";
        valueList[59] = "Nd";
        atomicList[60] = "Promethium";
        valueList[60] = "Pm";
        atomicList[61] = "Samarium";
        valueList[61] = "Sm";
        atomicList[62] = "Europium";
        valueList[62] = "Eu";
        atomicList[63] = "Gadolinium";
        valueList[63] = "Gd";
        atomicList[64] = "Terbium";
        valueList[64] = "Tb";
        atomicList[65] = "Dysprosium";
        valueList[65] = "Dy";
        atomicList[66] = "Holmium";
        valueList[66] = "Ho";
        atomicList[67] = "Erbium";
        valueList[67] = "Er";
        atomicList[68] = "Thulium";
        valueList[68] = "Tm";
        atomicList[69] = "Ytterbium";
        valueList[69] = "Yb";
        atomicList[70] = "Lutetium";
        valueList[70] = "Lu";
        atomicList[71] = "Hafnium";
        valueList[71] = "Hf";
        atomicList[72] = "Tantalum";
        valueList[72] = "Ta";
        atomicList[73] = "Tungsten";
        valueList[73] = "W";
        atomicList[74] = "Rhenium";
        valueList[74] = "Re";
        atomicList[75] = "Osmium";
        valueList[75] = "Os";
        atomicList[76] = "Iridium";
        valueList[76] = "Ir";
        atomicList[77] = "Platinum";
        valueList[77] = "Pt";
        atomicList[78] = "Gold";
        valueList[78] = "Au";
        atomicList[79] = "Mercury";
        valueList[79] = "Hg";
        atomicList[80] = "Thallium";
        valueList[80] = "Tl";
        atomicList[81] = "Lead";
        valueList[81] = "Pb";
        atomicList[82] = "Bismuth";
        valueList[82] = "Bi";
        atomicList[83] = "Polonium";
        valueList[83] = "Po";
        atomicList[84] = "Astatine";
        valueList[84] = "At";
        atomicList[85] = "Radon";
        valueList[85] = "Rn";
        atomicList[86] = "Francium";
        valueList[86] = "Fr";
        atomicList[87] = "Radium";
        valueList[87] = "Ra";
        atomicList[88] = "Actinium";
        valueList[88] = "Ac";
        atomicList[89] = "Thorium";
        valueList[89] = "Th";
        atomicList[90] = "Protactinium";
        valueList[90] = "Pa";
        atomicList[91] = "Uranium";
        valueList[91] = "U";
        atomicList[92] = "Neptunium";
        valueList[92] = "Np";
        atomicList[93] = "Plutonium";
        valueList[93] = "Pu";
        atomicList[94] = "Americium";
        valueList[94] = "Am";
        atomicList[95] = "Curium";
        valueList[95] = "Cm";
        atomicList[96] = "Berkelium";
        valueList[96] = "Bk";
        atomicList[97] = "Californium";
        valueList[97] = "Cf";
        atomicList[98] = "Einsteinium";
        valueList[98] = "Es";
        atomicList[99] = "Fermium";
        valueList[99] = "Fm";
        atomicList[100] = "Mendelevium";
        valueList[100] = "Md";
        atomicList[101] = "Nobelium";
        valueList[101] = "No";
        atomicList[102] = "Lawrencium";
        valueList[102] = "Lr";
        atomicList[103] = "Rutherfordium";
        valueList[103] = "Rf";
        atomicList[104] = "Dubnium";
        valueList[104] = "Db";
        atomicList[105] = "Seaborgium";
        valueList[105] = "Sg";
        atomicList[106] = "Bohrium";
        valueList[106] = "Bh";
        atomicList[107] = "Hassium";
        valueList[107] = "Hs";
        atomicList[108] = "Meltnerium";
        valueList[108] = "Mt";
        atomicList[109] = "Darmstadtium";
        valueList[109] = "Ds";
        atomicList[110] = "Roentgenium";
        valueList[110] = "Rg";
        atomicList[111] = "Copernicium";
        valueList[111] = "Cn";
        atomicList[112] = "Nihonium";
        valueList[112] = "Nh";
        atomicList[113] = "Flerovium";
        valueList[113] = "Fl";
        atomicList[114] = "Moscovium";
        valueList[114] = "Mc";
        atomicList[115] = "Livermorium";
        valueList[115] = "Lv";
        atomicList[116] = "Tennessine";
        valueList[116] = "Ts";
        atomicList[117] = "Oganesson";
        valueList[117] = "Og";
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题