当前位置:  开发笔记 > 编程语言 > 正文

用Swing绘制的旋钮很难看.为什么?

如何解决《用Swing绘制的旋钮很难看.为什么?》经验,为你挑选了1个好方法。

我正在画一个旋钮作为组件而我正在使用摇摆.结果似乎没问题但是当我点击旋钮并移动我的鼠标(向上或向下)来改变旋钮位置时,重新绘制的旋钮很难看,好像有两层重叠.更重要的是,我的结果仅仅是针对旋钮的"小尺寸".如果我放大框架,这种丑陋的效果就会消失.有人能解释我发生了什么以及如何改善我的旋钮吗?

非常感谢那些能帮助我的人.

这是我的代码:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Line2D;

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


public class Knob extends JPanel implements MouseListener {
int length, originX, originY, centerX, centerY, width, height, diameter, squareLength;
int minorTick, majorTick, xTick, yTick, xCursor, yCursor;
int xMouse, yMouse, xMouseOrigin, yMouseOrigin;
float yDeltaMouse;
double angleOrigin, angleRange, angle;
double cursorValue;
double initialCursorValue;
Color backgroundColor, knobColor;
boolean mousePressed;
Thread t;
private double knobValue;
private String title = new String("");
private int titleWidth;

Knob (double initialValue, Color c, int majorTick, int minorTick) {
    //System.out.println("Knob");
    cursorValue=initialCursorValue=initialValue;
    knobColor =c;       
    this.majorTick=majorTick;
    this.minorTick=minorTick;       

    this.addMouseListener(this);
}

Knob (double initialValue, Color c, int majorTick, int minorTick, String title) {
    //System.out.println("Knob");
    cursorValue=initialCursorValue=initialValue;
    knobColor =c;       
    this.majorTick=majorTick;
    this.minorTick=minorTick;
    this.title=title;

    this.addMouseListener(this);
}

public void paintComponent (Graphics g) {
    width=this.getWidth()/10*10;
    height=this.getHeight()/10*10;

    Graphics2D g2D = (Graphics2D)g;
    System.setProperty("awt.useSystemAAFontSettings", "on");
    System.setProperty("swing.aatext", "true");
    g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    if (width>height) {
        length=height;
    } else {
        length=width;
    }
    centerX=width/2;
    centerY=height/2;

    g.setColor(Color.BLACK);
    squareLength = (int )(length*0.9);
    originX=(width-squareLength)/2;
    originY=(height-squareLength)/2;

    /*
    //-45 = (-(Math.PI)/4)
    angleOrigin= -45;
    // 270 = (3*(Math.PI)/2)
    angleRange=270;
    //g.drawRect(originX, originY, squareLength, squareLength);
    //g.fillArc(originX, originY, squareLength, squareLength, (int)angleOrigin, (int)angleRange);
    for (int i=0; i=1) {
                cursorValue=1;
            } else if (cursorValue <= 0) {
                cursorValue=0;
            }
            //This repaint is a problem if I "uncomment" it
            repaint();
            knobValue=cursorValue;
        }           
    }       
}

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame frame = new JFrame();
    frame.setSize(300,70);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new Knob(0.5, new Color(0,255,0,255), 3, 9, "Gain"));
    frame.setVisible(true);
}

}



1> SubOptimal..:

你错过了调用paintComponent(Graphics g)方法JComponent.

如下更改代码将解决问题.

public void paintComponent(Graphics g) {
    super.paintComponent(g); 

推荐阅读
重庆制造漫画社
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有