标签: paintcomponent

如何在填充矩形内绘制字符串?

我只是想在一个充满黑色的矩形中显示我的String.谢谢您的帮助!

java drawstring drawrect paintcomponent

3
推荐指数
1
解决办法
9217
查看次数

图像加载的路径

我想在框架中添加图像但是我无法理解应该保留图像文件的位置以及如何给出图像的路径?

我已将图像放在My Document文件夹中并给出该路径,但它给出了错误消息.

我有以下代码.

import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.io.*; 
import javax.imageio.ImageIO; 
import javax.swing.*;   

public class Test extends JPanel {
    BufferedImage image;

    public Test(BufferedImage image) {
        this.image = image;
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // Draw image centered.
        int x = (getWidth() - image.getWidth())/2;
        int y = (getHeight() - image.getHeight())/2;
        g.drawImage(image, x, y, this);
    }

    public static void main(String[] args) throws IOException {
        String path = "images.jpeg";
        BufferedImage image = ImageIO.read(new File(path));
        Test contentPane = new …
Run Code Online (Sandbox Code Playgroud)

java swing image paintcomponent application-resource

3
推荐指数
2
解决办法
2万
查看次数

弹簧行为模拟

基本上,我想模拟绘制图像上的弹簧行为.我想让它通过几次迭代来上下缩放(就像在弹簧上修复一样).

我在网上找到的所有例子都导致了这个类 - FloatSpring.java

它应该提供所需的计算,将A点移动到B点,应用类似弹簧的效果,这取决于各种FloatSpring类设置.问题是我没有找到一个明确的例子如何正确使用它.

我做了这个小例子来测试FloatSpring:

public static void main ( String[] args )
{
    // Some image to bounce
    final ImageIcon icon =
            new ImageIcon ( WebProgressOverlayExample.class.getResource ( "icons/ava1.jpg" ) );

    // Component to paint image on
    JComponent spring = new JComponent ()
    {
        // Zoom value (1f = 100% = normal size)
        float zoom = 1f;

        {
            // Basic spring settings
            final FloatSpring fs = new FloatSpring ( 100 );
            fs.setPosition ( zoom );

            // Animation …
Run Code Online (Sandbox Code Playgroud)

java swing timer paintcomponent imageicon

3
推荐指数
1
解决办法
844
查看次数

重画不被召唤

嗨,我已经google了,无法弄清楚为什么我的paintComp方法没有被调用

我有以下代码包com.vf.zepto.view;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.vf.zepto.view.interfaces.ProcessorPanel;

public class CountryDetailsPanel extends JPanel implements ProcessorPanel, Runnable {
    private GridBagConstraints c = new GridBagConstraints();
    private String countryName;
    private Properties prop = new Properties();
    private BufferedImage image;

    public CountryDetailsPanel() {
        try {
            prop.load(new FileInputStream("country.props"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace(); …
Run Code Online (Sandbox Code Playgroud)

java swing repaint paintcomponent event-dispatch-thread

3
推荐指数
2
解决办法
663
查看次数

Java swing绘制对象超过其他组件

我正在尝试制作一个小型油漆程序.我绘制在对象JPanel这是对的顶部JFrame(我使用的Netbeans 6.9).我有一些基本功能,如font,line和fillRectangle.我正在使用标准方法绘制哪个要覆盖paintComponent().

class .... extends JPanel
{
@Override
void paintComponents(Graphics g)
{
.......
}
}
Run Code Online (Sandbox Code Playgroud)

问题是当我绘制一个文本时,在一个区域上划线然后它被绘制在它后面而不是它上面.基本上我想在之前绘制的所有其他对象之上绘制对象JPanel.我真的不想切换到其他类型的分层窗格.一个非常天真的方法是撤消每个对象并以相反的顺序绘制它们(最后一个).

java swing jpanel paintcomponent

3
推荐指数
1
解决办法
5498
查看次数

Java:为什么我的JButton被绘制在背景图像后面?

这是存在问题的JPanel类:

public class EntryDialog extends JPanel{

private static final long serialVersionUID = 1L;

MainWindow         mainWindow;
Image              background;
JButton            continueresume;
JButton            newresume;
JButton            settings;
JButton            exit;

public EntryDialog(MainWindow mainWindow){

    continueresume            = new JButton("Continue resume");
    newresume                 = new JButton("New resume");
    settings                  = new JButton("Settings");
    exit                      = new JButton("Exit");
    this.mainWindow           = mainWindow;
    this.background           = Utilities.getImage("images\\entryDialogBackground.jpg", true);

    this.setLayout(null);

    //continueresume button
    continueresume.setBounds(Utilities.getScaledWidth(1150), 
            Utilities.getScaledHeight(150), 
            Utilities.getScaledWidth(500), 
            Utilities.getScaledHeight(50));

    //newresume button
    newresume.setBounds(Utilities.getScaledWidth(1150), 
            Utilities.getScaledHeight(220), 
            Utilities.getScaledWidth(500), 
            Utilities.getScaledHeight(50));

    //settings button
    settings.setBounds(Utilities.getScaledWidth(1150), 
            Utilities.getScaledHeight(290), 
            Utilities.getScaledWidth(500), 
            Utilities.getScaledHeight(50));
    //exit button
    exit.setBounds(Utilities.getScaledWidth(1150), 
            Utilities.getScaledHeight(360), 
            Utilities.getScaledWidth(500), 
            Utilities.getScaledHeight(50));

    this.add(continueresume);
    this.add(newresume); …
Run Code Online (Sandbox Code Playgroud)

java swing button paintcomponent

3
推荐指数
1
解决办法
3963
查看次数

在Java中绘制一条线 - 线是不可见的

我自己学习Java.尝试创建一个包含线条的框架.它看起来很基本,但我看不出这条线.代码编译,我似乎无法理解为什么我看不到这条线.我在框架中看到了其他组件.我正在使用2个java文件.一个文件是容器文件,另一个文件是绘制线代码.它们是包a1的一部分.这是我的代码(请帮助):

容器文件:

package a1;
import javax.swing.*;
import java.awt.*;

public class gameContainer {

    public static void addComponentsToPane(Container pane) {
        pane.setLayout(null);

        //add button to the pane
        JButton b3 = new JButton("B1");
        pane.add(b3);

        //add line to the pane
        drawingLine line1 = new drawingLine();
        pane.add(line1);

        //size and position all relatively          
        Insets insets = pane.getInsets();
        Dimension size;
        size = b3.getPreferredSize();        
        b3.setBounds(350+insets.left,15+insets.top,size.width+50,size.height+20);
        size = line1.getPreferredSize();
        line1.setBounds(350+insets.left,75+insets.top,size.width+50,size.height+20);    

    }

    private static void createAndShowGUI() {
        int l = 200, w = 80;

        //Create and set up the window.
        JFrame …
Run Code Online (Sandbox Code Playgroud)

java swing css-position jpanel paintcomponent

3
推荐指数
1
解决办法
6590
查看次数

我想让球逐渐移动

当你按下其中一个方向键时,我试图让球逐渐移动,现在它只是传送.我想要它,以便你可以看到它移动.基于这个例子,我使用的是键绑定,并且有一个名为delta的变量会导致球移动50个像素,但就像我说的那样,只要你按下箭头键,球就会出现50个像素,我想要它就像你要踢球一样,你可以看到它从a点到b点.转到我觉得问题所在的第89行.

package game;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

/**
 * @see https://stackoverflow.com/questions/6991648
 * @see https://stackoverflow.com/questions/6887296
 * @see https://stackoverflow.com/questions/5797965
 */
public class LinePanel extends JPanel {
myObject ball;

private Point b1 = new Point(0,0);


private MouseHandler mouseHandler = new MouseHandler();
private Point p1 = new Point(100, 100);
private …
Run Code Online (Sandbox Code Playgroud)

java swing keylistener graphic paintcomponent

3
推荐指数
1
解决办法
1334
查看次数

消除锯齿的JLabel

我试图创建一个自定义消除锯齿JLabel,但我的文字仍然僵硬.为什么这不起作用?

类似问题的答案包括更详细的解决方案,但我想知道为什么这不起作用.

编辑:

这是一个SSCCE:

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

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

public class Tester {

    public static void main(String[] args) {
        SmoothLabel label = new SmoothLabel("Hello");
        label.setFont(new Font("Times New Roman", Font.PLAIN, 100));
        JFrame frame = new JFrame("SmoothLabel test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);

        frame.add(label);
        frame.setVisible(true);
    }
}

class SmoothLabel extends JLabel {

    String text;

    public SmoothLabel (String text) {
        super(text);

        this.text = text;
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g; …
Run Code Online (Sandbox Code Playgroud)

java swing jlabel antialiasing paintcomponent

3
推荐指数
1
解决办法
2695
查看次数

在图像上绘制JPanel中的形状

我需要在JPanel的JLabel中显示的图像上绘制形状(圆形或自由线).

我的代码基于如何在拖动光标时绘制没有间隙的细线?使用2次鼠标点击绘制一个圆圈.

代码如下.问题是,当我开始绘制时,图像消失,只有在我停止后才会重新出现.如果我评论行super.paintComponent(g); 但这并不会发生,但是当我绘制圆圈时,它会保留以前位置的路径.

public static void main(String args[]) {
try {
    URL url = new URL("http://www.senhoritatours.com/wp-content/uploads/2014/05/Porto-.jpg");
  backgroundImage = ImageIO.read(url);
} catch (Exception e) {
  e.printStackTrace();
}

loadAnnotation();
loadBackground();

JFrame f;
f = new JFrame();
f.setLayout(new BorderLayout());
f.add(mp);
f.pack();
f.setVisible(true);
Run Code Online (Sandbox Code Playgroud)

}

/*第0层:*加载背景图片*/

public static void loadBackground() {

  JLabel lbImg = new JLabel();
    lbImg.setBounds(0, 0, new ImageIcon(backgroundImage).getIconWidth(), new ImageIcon(backgroundImage).getIconHeight());
    lbImg.setIcon(new ImageIcon(backgroundImage));

  mp = new JPanel(new BorderLayout());


btnCircle.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent ae) {

      if(btnCircle.isEnabled()) …
Run Code Online (Sandbox Code Playgroud)

java swing image jframe paintcomponent

3
推荐指数
1
解决办法
3342
查看次数