I have a JFrame with dimension 500 X 500: it contains a JComponent with size 1000 X 1000 and this JComponent is contained into a JScrollBar with scrollbar always present.
The screenshot
The code
/**component */
public class LinesComponent extends JPanel
....
/***other class: CommandPanel contains the LinesComponents*/
public class CommandPanel extends JPanel{
....
private LinesComponent panel;
/*JScrollPanel*/
private void buildScrollPanel(Container container) {
JScrollPane scroll = new JScrollPane(panel);
scroll.setSize(1000,1000);
scroll.setBorder(new LineBorder(Color.BLACK));
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
container.add(scroll);
}
/**JFrame*/
private void buildFrame(String title) {
this.testFrame = new JFrame(title);
this.testFrame.setLayout(new FlowLayout());
this.testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.testFrame.setLocationRelativeTo(null);
this.testFrame.setSize(500,500);
this.testFrame.setBackground(EnumColor.BACKGROUND_PANEL.getValue());
}
//end CommandPanel
As showed into screenshot, the window has the scrollbar but it isn't working.
I tried to change the dimensions of JPanel and JFrame but the situation doesn't change.
I know that the scroll appears when the dimension of JComponent are greater of dimensione of JPanel container, but in this moment I see that I lose some information but I don't understand what is.
Do you have any suggest, please?
PS into screenshot I show that the scroll doesn't work, but in my desiderata the scroll has to work in vertical and horizontal senses
PS PS all my code in only one class
package testDebug;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.LineBorder;
public class AllClasses {
public static void main(String[] a){
CommandPanel commandPanel=new CommandPanel("test", new ArrayList<MyPoint>());
}
}
class LinesComponent extends JPanel{
private static final long serialVersionUID = 1L;
private final LinkedList<Line> lines = new LinkedList<Line>();
private static class Line {
final int x1;//x del primo punto
final int y1;//y del primo punto
final int x2;//x del secondo punto
final int y2;//y del secondo punto
final Color color;
final int pressure;
public Line(int x1, int y1, int x2, int y2, Color color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
this.pressure=3;
}
public Line(int x1, int y1, int x2, int y2, int newPressure, Color color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.pressure=newPressure;
this.color = color;
}
}//Line
public LinesComponent(){
setBorder(new LineBorder(Color.BLACK));
setBackground(Color.WHITE);
setPreferredSize(new Dimension(400, 400));
}
public void clearLines() {
this.lines.clear();
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Line line : this.lines) {
g.setColor(line.color);
g.fillOval(line.x1,line.y1, line.pressure, line.pressure);
}
}
public void addPoint(MyPoint p, Color randomColor) {
//call addLine(double x, double y, double xtilt, double ytilt, Color randomColor);
}
private void addLine(double x, double y, double xtilt, double ytilt, Color randomColor) {
this.lines.add(new Line((int) x, (int) y, (int) xtilt, (int) ytilt, randomColor));
repaint();
}
}
enum EnumButton {
DECREASE_X("-"),
INCREASE_X("+"),
DECREASE_Y("-"),
INCREASE_Y("+"),
DECREASE_ZOOM("-"),
INCREASE_ZOOM("+");
private JButton button;
public JButton button(){
return this.button;
}
private EnumButton(String s){
this.button=new JButton(s);
}
public void addActionListener(ActionListener a){
this.button().addActionListener(a);
}
}
class CommandPanel extends JPanel {
private static final long serialVersionUID = 1L;
private static final String INITIAL_POSITION="0";
private JFrame testFrame = null;
private LinesComponent panelSignature;
private JTextField positionX=new JTextField(5);
private JTextField positionY=new JTextField(5);
public CommandPanel(String title, List<MyPoint> newPoints) {
super();
positionX.setText(INITIAL_POSITION);
positionY.setText(INITIAL_POSITION);
buildFrame(title);
Container container = testFrame.getContentPane();
this.panelSignature=initPanel();
buildScrollPanel(container);
paintLine(newPoints);
allignButtons();
defineFrame();
}
private void buildScrollPanel(Container container) {
JScrollPane scroll = new JScrollPane(panelSignature);
scroll.setSize(300,300);
scroll.setBorder(new LineBorder(Color.BLACK));
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
container.add(scroll);
}
private void defineFrame() {
this.testFrame.pack();
this.testFrame.setVisible(true);
}
private void paintLine(List<MyPoint> points) {
Iterator<MyPoint> iterator = points.iterator();
while (iterator.hasNext()) {
MyPoint point = iterator.next();
this.panelSignature.addPoint(point,Color.BLUE);
}
}//disegnaLinea
private void allignButtons() {
JPanel buttonPanel = new JPanel(new FlowLayout());
sectionHorizontalMovement(buttonPanel);
sectionVerticalMovement(buttonPanel);
sectionZoom(buttonPanel);
this.testFrame.add(buttonPanel);
}
private void sectionZoom(JPanel buttonPanel) {
buttonPanel.add(EnumLabel.ZOOM.label());
buttonPanel.add(EnumButton.INCREASE_ZOOM.button());
buttonPanel.add(EnumButton.DECREASE_ZOOM.button());
buttonPanel.add(EnumLabel.ZOOM_DIRECTION.label());
buttonPanel.add(EnumLabel.EMPTY.label());
}//sezioneZoom
private void sectionVerticalMovement(JPanel pannelloPulsanti) {
pannelloPulsanti.add(EnumLabel.MOVE_UP_DOWN.label());
pannelloPulsanti.add(EnumButton.INCREASE_Y.button());
pannelloPulsanti.add(EnumButton.DECREASE_Y.button());
pannelloPulsanti.add(EnumLabel.Y_DIRECTION.label());
pannelloPulsanti.add(EnumLabel.MAX_Y_ALLOWED.label());
}//sezioneSpostamentoVerticale
private void sectionHorizontalMovement(JPanel pannelloPulsanti) {
pannelloPulsanti.add(EnumLabel.MOVE_RIGHT_LEFT.label());
pannelloPulsanti.add(EnumButton.INCREASE_X.button());
pannelloPulsanti.add(EnumButton.DECREASE_X.button());
pannelloPulsanti.add(EnumLabel.X_DIRECTION.label());
pannelloPulsanti.add(EnumLabel.MAX_X_ALLOWED.label());
}//sezioneSpostamentoOrizzontale
public LinesComponent initPanel() {
LinesComponent linesComponent= new LinesComponent();
linesComponent.setBorder(new LineBorder(Color.GRAY));
return linesComponent;
}
private void buildFrame(String titolo) {
this.testFrame = new JFrame(titolo);
this.testFrame.setLayout(new FlowLayout());
this.testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.testFrame.setLocationRelativeTo(null);
this.testFrame.setSize(700,700);
this.testFrame.setBackground(Color.WHITE);
}
}
enum EnumLayout {
FRAME(new FlowLayout()), BUTTON_PANEL(new GridLayout(3, 5));
private LayoutManager value;
private EnumLayout(LayoutManager manager){
this.value=manager;
}
public LayoutManager layout() {
return value;
}
}
enum EnumLabel {
MOVE_RIGHT_LEFT("Sposta in orizzontale"),
MOVE_UP_DOWN("Sposta in verticale"),
ZOOM("Zoom"), EMPTY(""),
MAX_X_ALLOWED("LARGHEZZA: 600"), MAX_Y_ALLOWED("ALTEZZA: 600"),
X_DIRECTION("0"), Y_DIRECTION("0"), ZOOM_DIRECTION("1")
;
private JLabel label;
private EnumLabel(String lab){
this.label=new JLabel(lab);
}
public JLabel label (){
return this.label;
}
}
class MyPoint {
// class with the data: useless for position of panel
}
Scrollpane and its scrollbars entirely rely on the size of the component in the viewport, which, by default, depends of the preferred size of that component.
Check this code which shows you a basic example of how this all works:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class TestScrollPane {
public static class CustomComponent extends JPanel {
private static final int RADIUS = 20;
private int x = 0;
private int y = 0;
private double speed = 18;
private double dx;
private double dy;
public CustomComponent() {
dx = speed;
dy = speed;
Timer t = new Timer(20, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
x += dx;
y += dy;
if (x + RADIUS > getWidth()) {
x = getWidth() - RADIUS;
dx = -speed;
} else if (x < 0) {
x = 0;
dx = speed;
}
if (y + RADIUS > getHeight()) {
y = getHeight() - RADIUS;
dy = -speed;
} else if (y < 0) {
y = 0;
dy = speed;
}
repaint();
}
});
t.start();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillOval(x, y, RADIUS, RADIUS);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}
}
protected void initUI() {
JFrame window = new JFrame(TestScrollPane.class.getSimpleName());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scroll = new JScrollPane(new CustomComponent(), JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
window.add(scroll);
window.setSize(600, 500);
window.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestScrollPane().initUI();
}
});
}
}
Actually your JScrollPane is 1000x1000px, so if there are scroll bars, you won't see them, because your frame is 500x500. Note that the JScrollPane acts on its content, that means that it shows scrollbars if the panel you are adding to it is bigger than the JScrollPane itself, but doesn't add scrollbars if the container is smaller.
The viewable area/space is defined (in most cases) by the view's preferred size. This suggests that your problem lies with your scroll panes view and not the scroll pane or anything else you've provided us
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestScrollPane02 {
public static void main(String[] args) {
new TestScrollPane02();
}
public TestScrollPane02() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(new LargePane()));
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class LargePane extends JPanel {
public LargePane() {
setLayout(new GridBagLayout());
add(new JLabel("I'm a large panel"));
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawLine(0, 0, getWidth(), getHeight());
g.drawLine(getWidth(), 0, 0, getHeight());
}
}
}
You can also have a look the Scrollable interface which provides additional hints back to the scroll pane...
Related
I want to plot many sinusoids with different frequencies on a panel, but because painting is destructive, I cannot achieve this. I can only see the last sinusoid plotted.
In the code, the generateSinus() method is called only three times, but it might be called many times and I don't want to call g.fillOval() method for each sinusoid in the paintComponent() method.
Is there a way to achieve non-destructive painting and see all sinusioids with different frequencies?
Please see the code below:
package testdrawsinus;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestDrawSinus extends JPanel
{
private static double[] x;
private static double[] y;
private static boolean buttonClicked = false;
private static JPanel panel = new TestDrawSinus();
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.GREEN);
if (buttonClicked)
{
for (int i=0; i<x.length; i++)
{
g.fillOval((int)x[i] + panel.getWidth()/2, -1*((int)y[i]) + panel.getHeight()/2, 10, 10);
}
buttonClicked = false;
}
}
private static void generateSinus(int freq)
{
x = new double[200];
y = new double[200];
for (int i=0; i<=199; i++)
{
x[i]= (double)i;
y[i] = 100*Math.sin(2*Math.PI*freq*i/200);
}
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
panel.setLayout(null);
panel.setBounds(20,20, 700,400);
panel.setBackground(Color.BLACK);
frame.add(panel);
JButton button1 = new JButton();
button1.setText("plot");
button1.setBounds(300, 500, 150, 50);
frame.add(button1);
button1.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
buttonClicked = true;
generateSinus(1);
panel.repaint();
generateSinus(2);
panel.repaint();
generateSinus(3);
panel.repaint();
}
});
}
}
Thanks for your help.
There are a number of ways you might do this, one way would be to seperate each series into its own model, then have the panel paint each model, for example...
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.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
new Main();
}
private Color[] masterColors = new Color[]{
Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY,
Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.PINK,
Color.RED, Color.WHITE, Color.YELLOW
};
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
TestDrawSinus sinusPane = new TestDrawSinus();
JFrame frame = new JFrame();
frame.add(sinusPane);
JButton button1 = new JButton();
button1.setText("plot");
frame.add(button1, BorderLayout.SOUTH);
button1.addActionListener(new ActionListener() {
private List<Color> colors = new ArrayList<>();
private int freq = 0;
#Override
public void actionPerformed(ActionEvent e) {
sinusPane.addSinusPlot(generateSinus(++freq, nextColor()));
sinusPane.addSinusPlot(generateSinus(++freq, nextColor()));
sinusPane.addSinusPlot(generateSinus(++freq, nextColor()));
}
protected Color nextColor() {
if (colors.isEmpty()) {
colors.addAll(Arrays.asList(masterColors));
Collections.shuffle(colors);
}
return colors.remove(0);
}
});
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
private Random rnd = new Random();
private SinusPlot generateSinus(int freq, Color color) {
double[] x = new double[200];
double[] y = new double[200];
for (int i = 0; i < 200; i++) {
x[i] = (double) i;
y[i] = 100 * Math.sin(2d * Math.PI * freq * i / 200d);
}
return new SinusPlot(x, y, color);
}
public class SinusPlot {
private double[] x;
private double[] y;
private Color color;
public SinusPlot(double[] x, double[] y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}
public int getSize() {
return x.length;
}
public double getXAt(int index) {
return x[index];
}
public double getYAt(int index) {
return y[index];
}
public Color getColor() {
return color;
}
}
public class TestDrawSinus extends JPanel {
private List<SinusPlot> plots = new ArrayList<>(8);
public void addSinusPlot(SinusPlot plot) {
plots.add(plot);
repaint();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(800, 600);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (SinusPlot plot : plots) {
System.out.println(plot.getColor());
g2d.setColor(plot.getColor());
for (int i = 0; i < plot.getSize(); i++) {
Ellipse2D dot = new Ellipse2D.Double(((getWidth() - plot.getSize()) / 2) + plot.getXAt(i), plot.getYAt(i) + getHeight() / 2, 10, 10);
g2d.fill(dot);
}
}
g2d.dispose();
}
}
}
null layouts aren't going to help you, take the time to learn understand how the layout management system works, it will save you lot of time and effort. See Laying Out Components Within a Container for more details.
static is not your friend (especially in this context), make the effort to understand how to live without it (and when to use it)
I am attempting to use jsliders to allow a user to pinpoint the origin of a circle to be drawn on a canvas. I am using a button to show and hide the circle. I am using paint on an inner jpanel so that paint will not write over components. However, the coordinates inside the jpanel are different than the coordinates for the entire frame. So, it is very difficult for me to get the coordinates of the jslider and then translate it to the jpanel to draw the circle. Is there an easy way to figure this out without a ton of guess and check? I am also using the custom layout miglayout. I have included the code for my GUI class as well as my custom JPanel I made so I could mess with the paint method.
public class CircleGUI extends JFrame implements ActionListener {
private MigLayout layout = new MigLayout();
private CustomPanel innerpanel;
private JSlider x,y;
private JColorChooser colorpick;
private JButton state;
private boolean bstate;
CircleGUI() {
initialize();
}
private void initialize() {
Border blackline = BorderFactory.createLineBorder(Color.black);
bstate = false;
x = new JSlider(JSlider.HORIZONTAL,650,325);
x.setPaintTicks(true);
x.setPaintLabels(true);
x.setPreferredSize(new Dimension(650,0));
y = new JSlider(JSlider.HORIZONTAL,650,325);
y.setPaintTicks(true);
y.setPaintLabels(true);
y.setInverted(true);
y.setOrientation(JSlider.VERTICAL);
y.setPreferredSize(new Dimension (0,600));
colorpick = new JColorChooser();
state = new JButton("Show");
state.addActionListener(e -> {
if(!bstate) {
int positionx = x.getValue() - 80;
int positiony = y.getValue();
Color c = colorpick.getColor();
innerpanel.setColor(c);
innerpanel.setX(positionx);
innerpanel.setY(positiony);
innerpanel.repaint();
state.setText("Hide");
bstate = true;
} else {
Color transparent = new Color(0,0,0,0);
innerpanel.setColor(transparent);
innerpanel.repaint();
state.setText("Show");
bstate = false;
}
});
JPanel outerpanel = new JPanel(layout);
innerpanel = new CustomPanel();
innerpanel.setPreferredSize(new Dimension(600,600));
innerpanel.setBorder(blackline);
outerpanel.add(x,"wrap");
outerpanel.add(y,"split 2");
outerpanel.add(innerpanel);
outerpanel.add(state,"wrap");
outerpanel.add(colorpick);
this.setSize(1000, 1000);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(outerpanel);
}
#Override
public void actionPerformed(ActionEvent e) {
}
}
public class CustomPanel extends JPanel implements ActionListener {
private Color c;
private int x;
private int y;
public CustomPanel() {
c = null;
}
#Override
public void actionPerformed (ActionEvent e) {
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(c);
g2.fill(new Ellipse2D.Double(x, y, 100, 100));
}
public void setColor(Color c) {
this.c = c;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
Your problem is you are trying to make a one-to-one mapping between the value of the JSlider and the coordinate in your CustomPanel. You should use the JSlider value as a percentage, i.e. minimum value zero and maximum value 100. If you want the circle to appear in the middle of the CustomPanel so you place both JSliders in their mid-points, i.e. both at 50%. Then you calculate 50% of the corresponding dimension to get the coordinate. If the width of CustomPanel is 600, then 50% of 600 is 300 so positionx needs to be 300.
The only thing I changed in your code is the calculation of positionx and positiony.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.border.Border;
import net.miginfocom.swing.MigLayout;
public class CircleGUI extends JFrame implements ActionListener {
private MigLayout layout = new MigLayout();
private CustomPanel innerpanel;
private JSlider x,y;
private JColorChooser colorpick;
private JButton state;
private boolean bstate;
CircleGUI() {
initialize();
}
private void initialize() {
Border blackline = BorderFactory.createLineBorder(Color.black);
bstate = false;
// x = new JSlider(JSlider.HORIZONTAL, 650, 325);
x = new JSlider(0, 100, 10);
x.setPaintTicks(true);
x.setPaintLabels(true);
x.setPreferredSize(new Dimension(650, 0));
// y = new JSlider(JSlider.HORIZONTAL, 650, 325);
y = new JSlider(0, 100, 10);
y.setPaintTicks(true);
y.setPaintLabels(true);
y.setInverted(true);
y.setOrientation(JSlider.VERTICAL);
y.setPreferredSize(new Dimension(0, 600));
colorpick = new JColorChooser();
state = new JButton("Show");
state.addActionListener(e -> {
if (!bstate) {
int positionx = Math.round(x.getValue() / 100.0f * innerpanel.getSize().width) - 50;
int positiony = Math.round(y.getValue() / 100.0f * innerpanel.getSize().height) - 50;
Color c = colorpick.getColor();
innerpanel.setColor(c);
innerpanel.setX(positionx);
innerpanel.setY(positiony);
innerpanel.repaint();
state.setText("Hide");
bstate = true;
}
else {
Color transparent = new Color(0, 0, 0, 0);
innerpanel.setColor(transparent);
innerpanel.repaint();
state.setText("Show");
bstate = false;
}
});
JPanel outerpanel = new JPanel(layout);
innerpanel = new CustomPanel();
innerpanel.setPreferredSize(new Dimension(600, 600));
innerpanel.setBorder(blackline);
outerpanel.add(x, "wrap");
outerpanel.add(y, "split 2");
outerpanel.add(innerpanel);
outerpanel.add(state, "wrap");
outerpanel.add(colorpick);
this.setSize(1000, 1000);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(outerpanel);
}
#Override
public void actionPerformed(ActionEvent e) {
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
CircleGUI cg = new CircleGUI();
cg.setVisible(true);
});
}
}
class CustomPanel extends JPanel implements ActionListener {
private Color c;
private int x;
private int y;
public CustomPanel() {
c = null;
}
#Override
public void actionPerformed(ActionEvent e) {
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(c);
g2.fill(new Ellipse2D.Double(x, y, 100, 100));
}
public void setColor(Color c) {
this.c = c;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
I have a Jframe with two buttons: 'A' and 'B'. Clicking the button 'A' should display the capital letter A in the JPanel. On mouse hover only, any 'A' letter within the canvas should be displayed in red. When the mouse leaves, the text color should be back to black.
I've coded for this and it works only once. The letter 'A' changes to red but does not change back to black. Also, it does not work for multiple 'A's
Code for JFrame:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawFrame extends JFrame{
private final int WIDTH = 500;
private final int HEIGHT = 300;
private GUIModel model;
private JButton number1;
private JButton number2;
private JPanel numberPanel;
private DrawPanel graphicsPanel;
public DrawFrame()
{
this.model = new GUIModel(" ");
createSelectionPanel();
createGraphicsPanel();
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private void createSelectionPanel()
{
numberPanel = new JPanel();
ButtonListener listener = new ButtonListener();
number1 = new JButton("A");
number1.addActionListener(listener);
number2 = new JButton("B");
number2.addActionListener(listener);
numberPanel.setLayout(new GridLayout(2,2));
numberPanel.add(number1);
numberPanel.add(number2);
this.add(numberPanel, BorderLayout.WEST);
}
private void createGraphicsPanel()
{
//instantiate drawing panel
graphicsPanel = new DrawPanel(WIDTH, HEIGHT, model);
//add drawing panel to right
add(graphicsPanel);
}
private class ButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
model.setDisplayString(event.getActionCommand());
}
}
//creates a drawing frame
public static void main(String[] args)
{
DrawFrame draw = new DrawFrame();
}
}
Code for JPanel:
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;
public class DrawPanel extends JPanel{
private static final long serialVersionUID = 3443814601865936618L;
private Font font = new Font("Default", Font.BOLD, 30);
private static final Color DEFAULT_TEXT_COLOR = Color.BLACK;
private static final Color HOVER_TEXT_COLOR = Color.RED;
private Color color = DEFAULT_TEXT_COLOR;
private List<GUIModel> numberList = new ArrayList<GUIModel>();
private GUIModel model;
boolean mouseHover = false;
public DrawPanel(int width, int height, GUIModel model){
this.setPreferredSize(new Dimension(width, height));
this.model = model;
//set white background for drawing panel
setBackground(Color.WHITE);
//add mouse listeners
MouseHandler mouseHandler = new MouseHandler();
this.addMouseListener(mouseHandler);
this.addMouseMotionListener(mouseHandler);
}
void checkForHover(MouseEvent event) {
FontMetrics metrics = getFontMetrics(font);
Graphics g = getGraphics();
Rectangle textBounds = metrics.getStringBounds("A", g).getBounds();
g.dispose();
int index = 0;
while (index < numberList.size()) {
Double x = numberList.get(index).getCoordinate().getX();
Double y = numberList.get(index).getCoordinate().getY();
textBounds.translate(x.intValue(), y.intValue());
if (textBounds.contains(event.getPoint())) {
color = HOVER_TEXT_COLOR;
}
else {
color = DEFAULT_TEXT_COLOR;
}
index++;
}
repaint(textBounds);
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setFont(font);
g.setColor(color);
int index = 0;
while (index < numberList.size()) {
Double x = numberList.get(index).getCoordinate().getX();
Double y = numberList.get(index).getCoordinate().getY();
String display = numberList.get(index).getDisplayString();
g.drawString(display, x.intValue(), y.intValue());
index++;
}
if (model.getCoordinate() != null) {
Point p = model.getCoordinate();
g.drawString(model.getDisplayString(), p.x, p.y);
GUIModel number = new GUIModel();
number.setCoordinate(p);
number.setDisplayString(model.getDisplayString());
numberList.add(number);
}
}
//class to handle all mouse events
private class MouseHandler extends MouseAdapter implements MouseMotionListener
{
#Override
public void mousePressed(MouseEvent event)
{
model.setCoordinate(event.getPoint());
}
#Override
public void mouseReleased(MouseEvent event)
{
DrawPanel.this.repaint();
}
#Override
public void mouseEntered(MouseEvent event) {
checkForHover(event);
}
#Override
public void mouseMoved(MouseEvent event) {
checkForHover(event);
}
}
}
Code for GUIModel:
import java.awt.Point;
public class GUIModel {
private String displayString;
private Point coordinate;
public GUIModel() {}
public GUIModel(String displayString) {
this.displayString = displayString;
}
public void setDisplayString(String displayString) {
this.displayString = displayString;
}
public String getDisplayString() {
return displayString;
}
public Point getCoordinate() {
return coordinate;
}
public void setCoordinate(int x, int y) {
this.coordinate = new Point(x, y);
}
public void setCoordinate(Point coordinate) {
this.coordinate = coordinate;
}
}
Any help would be much appreciated. Thanks!
There's several misconceptions.
Graphics#drawString doesn't paint the text at the x/y position, so that the x/y is the top left corner of the String, but instead, the x/y position is the baseline of the font, this means that much of the text is draw above the y position, see Font Concepts for more details. This means that when you try and calculate the the Rectangle of the text, it's actually lower then where you painting it. Instead, you need to use y + ascent to get the text to position properly.
paintComponent can be called at any time for any number of reasons, many of which you don't control. To this end, paintComponent should only be used to paint the current state of the component and should never update or modify the state of the component. So adding a new GUIModel within the method is the wrong thing to do, instead, it should be added in the mouseClicked event of the MouseListener.
You're relying to much on the GUIModel variables. You should create a model only when you actually need it
Conceptually, this example address most of the issues mentioned above
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawFrame extends JFrame {
private final int WIDTH = 500;
private final int HEIGHT = 300;
// private GUIModel model;
private JButton number1;
private JButton number2;
private JPanel numberPanel;
private DrawPanel graphicsPanel;
public DrawFrame() {
// this.model = new GUIModel(" ");
createSelectionPanel();
createGraphicsPanel();
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private void createSelectionPanel() {
numberPanel = new JPanel();
ButtonListener listener = new ButtonListener();
number1 = new JButton("A");
number1.addActionListener(listener);
number2 = new JButton("B");
number2.addActionListener(listener);
numberPanel.setLayout(new GridLayout(2, 2));
numberPanel.add(number1);
numberPanel.add(number2);
this.add(numberPanel, BorderLayout.WEST);
}
private void createGraphicsPanel() {
//instantiate drawing panel
graphicsPanel = new DrawPanel(WIDTH, HEIGHT);
//add drawing panel to right
add(graphicsPanel);
}
private class ButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
graphicsPanel.setDisplayString(event.getActionCommand());
}
}
//creates a drawing frame
public static void main(String[] args) {
DrawFrame draw = new DrawFrame();
}
public static class DrawPanel extends JPanel {
private static final long serialVersionUID = 3443814601865936618L;
private Font font = new Font("Default", Font.BOLD, 30);
private static final Color DEFAULT_TEXT_COLOR = Color.BLACK;
private static final Color HOVER_TEXT_COLOR = Color.RED;
private Color color = DEFAULT_TEXT_COLOR;
private List<GUIModel> numberList = new ArrayList<GUIModel>();
boolean mouseHover = false;
private String displayString;
private GUIModel hoverModel;
public DrawPanel(int width, int height) {
this.setPreferredSize(new Dimension(width, height));
//set white background for drawing panel
setBackground(Color.WHITE);
//add mouse listeners
MouseHandler mouseHandler = new MouseHandler();
this.addMouseListener(mouseHandler);
this.addMouseMotionListener(mouseHandler);
}
protected Rectangle getBounds(GUIModel model) {
FontMetrics metrics = getFontMetrics(font);
Double x = model.getCoordinate().getX();
Double y = model.getCoordinate().getY();
Rectangle textBounds = new Rectangle(
x.intValue(),
y.intValue(),
metrics.stringWidth(model.getDisplayString()),
metrics.getHeight());
return textBounds;
}
void checkForHover(MouseEvent event) {
Rectangle textBounds = null;
if (hoverModel != null) {
textBounds = getBounds(hoverModel);
}
hoverModel = null;
if (textBounds != null) {
repaint(textBounds);
}
for (GUIModel model : numberList) {
textBounds = getBounds(model);
if (textBounds.contains(event.getPoint())) {
hoverModel = model;
repaint(textBounds);
break;
}
}
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setFont(font);
g.setColor(DEFAULT_TEXT_COLOR);
FontMetrics fm = g.getFontMetrics();
for (GUIModel model : numberList) {
if (model != hoverModel) {
Double x = model.getCoordinate().getX();
Double y = model.getCoordinate().getY();
String display = model.getDisplayString();
g.drawString(display, x.intValue(), y.intValue() + fm.getAscent());
}
}
if (hoverModel != null) {
g.setColor(HOVER_TEXT_COLOR);
Double x = hoverModel.getCoordinate().getX();
Double y = hoverModel.getCoordinate().getY();
String display = hoverModel.getDisplayString();
g.drawString(display, x.intValue(), y.intValue() + fm.getAscent());
}
// if (model.getCoordinate() != null) {
// Point p = model.getCoordinate();
// g.drawString(model.getDisplayString(), p.x, p.y);
//// GUIModel number = new GUIModel();
//// number.setCoordinate(p);
//// number.setDisplayString(model.getDisplayString());
//// numberList.add(number);
// }
}
public void setDisplayString(String text) {
displayString = text;
}
//class to handle all mouse events
private class MouseHandler extends MouseAdapter implements MouseMotionListener {
#Override
public void mouseClicked(MouseEvent e) {
GUIModel model = new GUIModel(displayString);
model.setCoordinate(e.getPoint());
numberList.add(model);
repaint();
}
#Override
public void mouseMoved(MouseEvent event) {
checkForHover(event);
}
}
}
public static class GUIModel {
private String displayString;
private Point coordinate;
public GUIModel() {
}
public GUIModel(String displayString) {
this.displayString = displayString;
}
public void setDisplayString(String displayString) {
this.displayString = displayString;
}
public String getDisplayString() {
return displayString;
}
public Point getCoordinate() {
return coordinate;
}
public void setCoordinate(int x, int y) {
this.coordinate = new Point(x, y);
}
public void setCoordinate(Point coordinate) {
this.coordinate = coordinate;
}
}
}
I have an image I am rotating when the user clicks on a button. But it is not working.
I would like to see the image rotating gradually to 90 degrees till it stops but it doesn't. The image must rotate 90 degrees gradually when the button is clicked.
I have created an SSCCE to demonstrate the problem. Please replace the image in the CrossingPanelSSCE class with any image of your choice. Just put the image in your images folder and name it images/railCrossing.JPG.
RotateButtonSSCE
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
public class RotateButtonSSCE extends JPanel implements ActionListener{
private JButton rotate = new JButton("Rotate");
private VisualizationPanelSSCE vis = new VisualizationPanelSSCE();
public RotateButtonSSCE() {
this.setBorder(BorderFactory.createTitledBorder("Rotate Button "));
this.rotate.addActionListener(this);
this.add(rotate);
}
public void actionPerformed(ActionEvent ev) {
vis.rotatetheCrossing();
}
}
CrossingPanelSSCE
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class CrossingPanelSSCE extends JPanel{
private static final long serialVersionUID = 1L;
// private data members
private Image crossingImage;
private int currentRotationAngle;
private int imageWidth;
private int imageHeight;
private AffineTransform affineTransform;
private boolean clockwise;
private static int ROTATE_ANGLE_OFFSET = 2;
private int xCoordinate;
private int yCoordinate;
private static javax.swing.Timer timer;
private void initialize(){
this.crossingImage = Toolkit.getDefaultToolkit().getImage("images/railCrossing.JPG");
this.imageWidth = this.getCrossingImage().getWidth(this);
this.imageHeight = this.getCrossingImage().getHeight(this);
this.affineTransform = new AffineTransform();
currentRotationAngle = 90;
timer = new javax.swing.Timer(20, new MoveListener());
}
public CrossingPanelSSCE(int x, int y) {
this.setxCoordinate(x);
this.setyCoordinate(y);
this.setPreferredSize(new Dimension(50, 50));
this.setBackground(Color.red);
TitledBorder border = BorderFactory.createTitledBorder("image");
this.setLayout(new FlowLayout());
this.initialize();
}
public void paintComponent(Graphics grp){
Rectangle rect = this.getBounds();
Graphics2D g2d = (Graphics2D)grp;
g2d.setColor(Color.BLACK);
this.getAffineTransform().setToTranslation(this.getxCoordinate(), this.getyCoordinate());
//rotate with the rotation point as the mid of the image
this.getAffineTransform().rotate(Math.toRadians(this.getCurrentRotationAngle()), this.getCrossingImage().getWidth(this) /2,
this.getCrossingImage().getHeight(this)/2);
//draw the image using the AffineTransform
g2d.drawImage(this.getCrossingImage(), this.getAffineTransform(), this);
}
public void rotateCrossing(){
System.out.println("CurrentRotationAngle: " + currentRotationAngle);
this.currentRotationAngle += ROTATE_ANGLE_OFFSET;
//int test = currentRotationAngle % 90;
if(currentRotationAngle % 90 == 0){
setCurrentRotationAngle(currentRotationAngle);
timer.stop();
}
//repaint the image panel
repaint();
}
void start() {
if (timer != null) {
timer.start();
}
}
private class MoveListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
rotateCrossing();
}
}
public Image getCrossingImage() {
return crossingImage;
}
public void setCrossingImage(Image crossingImage) {
this.crossingImage = crossingImage;
}
public int getCurrentRotationAngle() {
return currentRotationAngle;
}
public void setCurrentRotationAngle(int currentRotationAngle) {
this.currentRotationAngle = currentRotationAngle;
}
public int getImageWidth() {
return imageWidth;
}
public void setImageWidth(int imageWidth) {
this.imageWidth = imageWidth;
}
public int getImageHeight() {
return imageHeight;
}
public void setImageHeight(int imageHeight) {
this.imageHeight = imageHeight;
}
public AffineTransform getAffineTransform() {
return affineTransform;
}
public void setAffineTransform(AffineTransform affineTransform) {
this.affineTransform = affineTransform;
}
public boolean isClockwise() {
return clockwise;
}
public void setClockwise(boolean clockwise) {
this.clockwise = clockwise;
}
public int getxCoordinate() {
return xCoordinate;
}
public void setxCoordinate(int xCoordinate) {
this.xCoordinate = xCoordinate;
}
public int getyCoordinate() {
return yCoordinate;
}
public void setyCoordinate(int yCoordinate) {
this.yCoordinate = yCoordinate;
}
public javax.swing.Timer getTimer() {
return timer;
}
public void setTimer(javax.swing.Timer timer) {
this.timer = timer;
}
}
VisualizationPanelSSCE
import gui.CrossingPanel;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.GeneralPath;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
import application.Robot2;
public class VisualizationPanelSSCE extends JPanel{
//private data members
private GeneralPath path;
private Shape horizontalRail;
private Shape verticalRail;
private static int LENGTH = 350;
private CrossingPanelSSCE crossingP;
private void initializeComponents(){
this.path = new GeneralPath();
this.horizontalRail = this.createHorizontalRail();
this.verticalRail = this.createVerticalRail();
this.crossingP = new CrossingPanelSSCE(328,334);
}
public VisualizationPanelSSCE(){
this.initializeComponents();
this.setPreferredSize(new Dimension(400,400));
TitledBorder border = BorderFactory.createTitledBorder("Rotation");
this.setBorder(border);
}
public GeneralPath getPath() {
return path;
}
public void setPath(GeneralPath path) {
this.path = path;
}
private Shape createHorizontalRail(){
this.getPath().moveTo(5, LENGTH);
this.getPath().lineTo(330, 350);
this.getPath().closePath();
return this.getPath();
}
private Shape createVerticalRail(){
this.getPath().moveTo(350, 330);
this.getPath().lineTo(350,10);
this.getPath().closePath();
return this.getPath();
}
public void paintComponent(Graphics comp){
super.paintComponent(comp);
Graphics2D comp2D = (Graphics2D)comp;
BasicStroke pen = new BasicStroke(15.0F, BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND);
comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
comp2D.setPaint(Color.black);
comp2D.setBackground(Color.WHITE);
comp2D.draw(this.horizontalRail);
this.crossingP.paintComponent(comp2D);
}
public CrossingPanelSSCE getCrossingP() {
return crossingP;
}
public void setCrossingP(CrossingPanelSSCE crossingP) {
this.crossingP = crossingP;
}
public void rotatetheCrossing(){
Runnable rotateCrossing1 = new Runnable(){
public void run() {
crossingP.start();
}
};
SwingUtilities.invokeLater(rotateCrossing1);
}
}
TestGUISSCE it contains the main method.
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.*;
public class TestGUISSCE{
private RotateButtonSSCE rotate = new RotateButtonSSCE();
private VisualizationPanelSSCE vision = new VisualizationPanelSSCE();
public void createGui(){
JFrame frame = new JFrame("Example");
frame.setSize(new Dimension(500, 500));
JPanel pane = new JPanel();
pane.add(this.vision);
pane.add(rotate);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(pane);
frame.setVisible(true);
}
public static void main(String[] args) {
new TestGUISSCE().createGui();
}
}
In addition to #tulskiy's helpful observations, I would add two points:
Always construct your GUI on the event dispatch thread, as shown below.
An sscce should be a Short, Self Contained, Correct (Compilable), Example. As a convenience, don't require others to recreate multiple public classes; use top-level (package-private) or nested classes. As this is a graphics problem, use a public or synthetic image that reflects your problem.
In the example below, paintComponent() alters the graphics context's transform to effect the rotation. Note that the operations are performed in the (apparent) reverse of the declaration order: First, the image's center is translated to the origin; second, the image is rotated; third, the image's center is translated to the center of the panel. You can see the effect by resizing the panel.
Addendum: See also this alternative approach using AffineTransform.
package overflow;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.*;
/**
* #see https://stackoverflow.com/questions/3371227
* #see https://stackoverflow.com/questions/3405799
*/
public class RotateApp {
private static final int N = 3;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(N, N, N, N));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for (int i = 0; i < N * N; i++) {
frame.add(new RotatePanel());
}
frame.pack();
frame.setVisible(true);
}
});
}
}
class RotatePanel extends JPanel implements ActionListener {
private static final int SIZE = 256;
private static double DELTA_THETA = Math.PI / 90;
private final Timer timer = new Timer(25, this);
private Image image = RotatableImage.getImage(SIZE);
private double dt = DELTA_THETA;
private double theta;
public RotatePanel() {
this.setBackground(Color.lightGray);
this.setPreferredSize(new Dimension(
image.getWidth(null), image.getHeight(null)));
this.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
image = RotatableImage.getImage(SIZE);
dt = -dt;
}
});
timer.start();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
g2d.rotate(theta);
g2d.translate(-image.getWidth(this) / 2, -image.getHeight(this) / 2);
g2d.drawImage(image, 0, 0, null);
}
#Override
public void actionPerformed(ActionEvent e) {
theta += dt;
repaint();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(SIZE, SIZE);
}
}
class RotatableImage {
private static final Random r = new Random();
static public Image getImage(int size) {
BufferedImage bi = new BufferedImage(
size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.getHSBColor(r.nextFloat(), 1, 1));
g2d.setStroke(new BasicStroke(size / 8));
g2d.drawLine(0, size / 2, size, size / 2);
g2d.drawLine(size / 2, 0, size / 2, size);
g2d.dispose();
return bi;
}
}
The code for Rotated Icon uses the AffineTransform to rotate about its center.
this.crossingP.paintComponent(comp2D);
Never do this! Your CrossingPane is not added to any component, so repaint() doesn't have any effect. You can check it by adding prints in the paintComponent() method. SO you need to add CrossingPane to the VisualizationPane:
setLayout(new BorderLayout());
add(crossingP, BorderLayout.CENTER);
There are some issues with centering the image, but this shouldn't be hard to fix.
PS. Read again about layouts and painting.
I have an image I am rotating when the user clicks on a button. But it is not working.
I would like to see the image rotating gradually to 90 degrees till it stops but it doesn't. The image must rotate 90 degrees gradually when the button is clicked.
I have created an SSCCE to demonstrate the problem. Please replace the image in the CrossingPanelSSCE class with any image of your choice. Just put the image in your images folder and name it images/railCrossing.JPG.
RotateButtonSSCE
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
public class RotateButtonSSCE extends JPanel implements ActionListener{
private JButton rotate = new JButton("Rotate");
private VisualizationPanelSSCE vis = new VisualizationPanelSSCE();
public RotateButtonSSCE() {
this.setBorder(BorderFactory.createTitledBorder("Rotate Button "));
this.rotate.addActionListener(this);
this.add(rotate);
}
public void actionPerformed(ActionEvent ev) {
vis.rotatetheCrossing();
}
}
CrossingPanelSSCE
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class CrossingPanelSSCE extends JPanel{
private static final long serialVersionUID = 1L;
// private data members
private Image crossingImage;
private int currentRotationAngle;
private int imageWidth;
private int imageHeight;
private AffineTransform affineTransform;
private boolean clockwise;
private static int ROTATE_ANGLE_OFFSET = 2;
private int xCoordinate;
private int yCoordinate;
private static javax.swing.Timer timer;
private void initialize(){
this.crossingImage = Toolkit.getDefaultToolkit().getImage("images/railCrossing.JPG");
this.imageWidth = this.getCrossingImage().getWidth(this);
this.imageHeight = this.getCrossingImage().getHeight(this);
this.affineTransform = new AffineTransform();
currentRotationAngle = 90;
timer = new javax.swing.Timer(20, new MoveListener());
}
public CrossingPanelSSCE(int x, int y) {
this.setxCoordinate(x);
this.setyCoordinate(y);
this.setPreferredSize(new Dimension(50, 50));
this.setBackground(Color.red);
TitledBorder border = BorderFactory.createTitledBorder("image");
this.setLayout(new FlowLayout());
this.initialize();
}
public void paintComponent(Graphics grp){
Rectangle rect = this.getBounds();
Graphics2D g2d = (Graphics2D)grp;
g2d.setColor(Color.BLACK);
this.getAffineTransform().setToTranslation(this.getxCoordinate(), this.getyCoordinate());
//rotate with the rotation point as the mid of the image
this.getAffineTransform().rotate(Math.toRadians(this.getCurrentRotationAngle()), this.getCrossingImage().getWidth(this) /2,
this.getCrossingImage().getHeight(this)/2);
//draw the image using the AffineTransform
g2d.drawImage(this.getCrossingImage(), this.getAffineTransform(), this);
}
public void rotateCrossing(){
System.out.println("CurrentRotationAngle: " + currentRotationAngle);
this.currentRotationAngle += ROTATE_ANGLE_OFFSET;
//int test = currentRotationAngle % 90;
if(currentRotationAngle % 90 == 0){
setCurrentRotationAngle(currentRotationAngle);
timer.stop();
}
//repaint the image panel
repaint();
}
void start() {
if (timer != null) {
timer.start();
}
}
private class MoveListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
rotateCrossing();
}
}
public Image getCrossingImage() {
return crossingImage;
}
public void setCrossingImage(Image crossingImage) {
this.crossingImage = crossingImage;
}
public int getCurrentRotationAngle() {
return currentRotationAngle;
}
public void setCurrentRotationAngle(int currentRotationAngle) {
this.currentRotationAngle = currentRotationAngle;
}
public int getImageWidth() {
return imageWidth;
}
public void setImageWidth(int imageWidth) {
this.imageWidth = imageWidth;
}
public int getImageHeight() {
return imageHeight;
}
public void setImageHeight(int imageHeight) {
this.imageHeight = imageHeight;
}
public AffineTransform getAffineTransform() {
return affineTransform;
}
public void setAffineTransform(AffineTransform affineTransform) {
this.affineTransform = affineTransform;
}
public boolean isClockwise() {
return clockwise;
}
public void setClockwise(boolean clockwise) {
this.clockwise = clockwise;
}
public int getxCoordinate() {
return xCoordinate;
}
public void setxCoordinate(int xCoordinate) {
this.xCoordinate = xCoordinate;
}
public int getyCoordinate() {
return yCoordinate;
}
public void setyCoordinate(int yCoordinate) {
this.yCoordinate = yCoordinate;
}
public javax.swing.Timer getTimer() {
return timer;
}
public void setTimer(javax.swing.Timer timer) {
this.timer = timer;
}
}
VisualizationPanelSSCE
import gui.CrossingPanel;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.GeneralPath;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
import application.Robot2;
public class VisualizationPanelSSCE extends JPanel{
//private data members
private GeneralPath path;
private Shape horizontalRail;
private Shape verticalRail;
private static int LENGTH = 350;
private CrossingPanelSSCE crossingP;
private void initializeComponents(){
this.path = new GeneralPath();
this.horizontalRail = this.createHorizontalRail();
this.verticalRail = this.createVerticalRail();
this.crossingP = new CrossingPanelSSCE(328,334);
}
public VisualizationPanelSSCE(){
this.initializeComponents();
this.setPreferredSize(new Dimension(400,400));
TitledBorder border = BorderFactory.createTitledBorder("Rotation");
this.setBorder(border);
}
public GeneralPath getPath() {
return path;
}
public void setPath(GeneralPath path) {
this.path = path;
}
private Shape createHorizontalRail(){
this.getPath().moveTo(5, LENGTH);
this.getPath().lineTo(330, 350);
this.getPath().closePath();
return this.getPath();
}
private Shape createVerticalRail(){
this.getPath().moveTo(350, 330);
this.getPath().lineTo(350,10);
this.getPath().closePath();
return this.getPath();
}
public void paintComponent(Graphics comp){
super.paintComponent(comp);
Graphics2D comp2D = (Graphics2D)comp;
BasicStroke pen = new BasicStroke(15.0F, BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND);
comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
comp2D.setPaint(Color.black);
comp2D.setBackground(Color.WHITE);
comp2D.draw(this.horizontalRail);
this.crossingP.paintComponent(comp2D);
}
public CrossingPanelSSCE getCrossingP() {
return crossingP;
}
public void setCrossingP(CrossingPanelSSCE crossingP) {
this.crossingP = crossingP;
}
public void rotatetheCrossing(){
Runnable rotateCrossing1 = new Runnable(){
public void run() {
crossingP.start();
}
};
SwingUtilities.invokeLater(rotateCrossing1);
}
}
TestGUISSCE it contains the main method.
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.*;
public class TestGUISSCE{
private RotateButtonSSCE rotate = new RotateButtonSSCE();
private VisualizationPanelSSCE vision = new VisualizationPanelSSCE();
public void createGui(){
JFrame frame = new JFrame("Example");
frame.setSize(new Dimension(500, 500));
JPanel pane = new JPanel();
pane.add(this.vision);
pane.add(rotate);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(pane);
frame.setVisible(true);
}
public static void main(String[] args) {
new TestGUISSCE().createGui();
}
}
In addition to #tulskiy's helpful observations, I would add two points:
Always construct your GUI on the event dispatch thread, as shown below.
An sscce should be a Short, Self Contained, Correct (Compilable), Example. As a convenience, don't require others to recreate multiple public classes; use top-level (package-private) or nested classes. As this is a graphics problem, use a public or synthetic image that reflects your problem.
In the example below, paintComponent() alters the graphics context's transform to effect the rotation. Note that the operations are performed in the (apparent) reverse of the declaration order: First, the image's center is translated to the origin; second, the image is rotated; third, the image's center is translated to the center of the panel. You can see the effect by resizing the panel.
Addendum: See also this alternative approach using AffineTransform.
package overflow;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.*;
/**
* #see https://stackoverflow.com/questions/3371227
* #see https://stackoverflow.com/questions/3405799
*/
public class RotateApp {
private static final int N = 3;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(N, N, N, N));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for (int i = 0; i < N * N; i++) {
frame.add(new RotatePanel());
}
frame.pack();
frame.setVisible(true);
}
});
}
}
class RotatePanel extends JPanel implements ActionListener {
private static final int SIZE = 256;
private static double DELTA_THETA = Math.PI / 90;
private final Timer timer = new Timer(25, this);
private Image image = RotatableImage.getImage(SIZE);
private double dt = DELTA_THETA;
private double theta;
public RotatePanel() {
this.setBackground(Color.lightGray);
this.setPreferredSize(new Dimension(
image.getWidth(null), image.getHeight(null)));
this.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
image = RotatableImage.getImage(SIZE);
dt = -dt;
}
});
timer.start();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
g2d.rotate(theta);
g2d.translate(-image.getWidth(this) / 2, -image.getHeight(this) / 2);
g2d.drawImage(image, 0, 0, null);
}
#Override
public void actionPerformed(ActionEvent e) {
theta += dt;
repaint();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(SIZE, SIZE);
}
}
class RotatableImage {
private static final Random r = new Random();
static public Image getImage(int size) {
BufferedImage bi = new BufferedImage(
size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.getHSBColor(r.nextFloat(), 1, 1));
g2d.setStroke(new BasicStroke(size / 8));
g2d.drawLine(0, size / 2, size, size / 2);
g2d.drawLine(size / 2, 0, size / 2, size);
g2d.dispose();
return bi;
}
}
The code for Rotated Icon uses the AffineTransform to rotate about its center.
this.crossingP.paintComponent(comp2D);
Never do this! Your CrossingPane is not added to any component, so repaint() doesn't have any effect. You can check it by adding prints in the paintComponent() method. SO you need to add CrossingPane to the VisualizationPane:
setLayout(new BorderLayout());
add(crossingP, BorderLayout.CENTER);
There are some issues with centering the image, but this shouldn't be hard to fix.
PS. Read again about layouts and painting.